https://nanti.jisuanke.com/t/31458html
創建兩個樹狀數組,第一個是,a[1]*n+a[2]*(n-1)....+a[n]*1;第二個是正常的a[1],a[2],a[3]...a[n]c++
#include "bits/stdc++.h" using namespace std; #define ll long long const int MAXN=1e5+10; ll sum[MAXN],ans[MAXN]; ll num[MAXN]; ll n,q; int lowbit(int x) { return x&(-x); } void update(int i , ll x) { ll t=x*(n-i+1); while(i<=n) { sum[i]+=x; ans[i]+=t; i+=lowbit(i); } } ll query1(int x) { ll Sum=0; while(x) { Sum+=sum[x]; x-=lowbit(x); } return Sum; } ll query2(int x) { ll Sum=0; while(x) { Sum+=ans[x]; x-=lowbit(x); } return Sum; } int main() { while(scanf("%lld%lld",&n,&q)!=EOF) { for(int i=1;i<=n;i++) { scanf("%lld",&num[i]); update(i,num[i]); } while(q--) { int a,b,c; scanf("%d",&a); if(a==1) { scanf("%d%d",&b,&c); ll A1=query1(c)-query1(b-1); ll A2=query2(c)-query2(b-1); printf("%lld\n",A2-A1*((n-b+1)-(c-b+1))); } else { scanf("%d%d",&b,&c); ll k=c-num[b]; num[b]=c; update(b,k); } } } return 0; }
Morgana is learning computer vision, and he likes cats, too. One day he wants to find the cat movement from a cat video. To do this, he extracts cat features in each frame. A cat feature is a two-dimension vector <x, y>. If xi= xj and yi = yj, then <xi, yi> <xj, yj> are same features.數組
So if cat features are moving, we can think the cat is moving. If feature <a, b> is appeared in continuous frames, it will form features movement. For example, feature <a , b > is appeared in frame 2,3,4,7,8, then it forms two features movement 2−3−4 and 7−8 .app
Now given the features in each frames, the number of features may be different, Morgana wants to find the longest features movement.ide
First line contains one integer T(1≤T≤10) , giving the test cases.this
Then the first line of each cases contains one integer n (number of frames),spa
In The next n lines, each line contains one integer ki ( the number of features) and 2ki intergers describe kifeatures in ith frame.(The first two integers describe the first feature, the 3rd and 4th integer describe the second feature, and so on).orm
In each test case the sum number of features N will satisfy N≤100000 .htm
For each cases, output one line with one integers represents the longest length of features movement.blog
1 8 2 1 1 2 2 2 1 1 1 4 2 1 1 2 2 2 2 2 1 4 0 0 1 1 1 1 1 1
3