1, HDU 1556 Color the ball zone update, single-point query
2, test instructions: n balloons, each time to (a, b) balloons painted a color, asked the last each balloon painted several times.
(1) tree-like array
Summary: a tree-like array is a data structure that queries and modifies the complexity of both log (n). It is used primarily to query the sum of all elements between any two bits, but only one element can be modified at a time.
here You can use a tree-like array to change your thinking. At update (A, b), update up, A~n plus 1,b+1~n minus 1. When you query a point, sum it down.
#include <iostream>#include<cstring>#include<cmath>#include<queue>#include<algorithm>#include<cstdio>#defineF (I,A,B) for (int i=a;i<b;i++)#defineFF (I,A,B) for (int i=a;i<=b;i++)#defineMes (A, b) memset (A,b,sizeof (a))#defineINF 0x3f3f3f3f#defineLL Long Longusing namespacestd;Const intn=10010, max=100100;intN,c[max];intLowbit (intx) {//Calculate 2^k returnx&-x;}voidUpdateintXintval) { //update it up so that all the bands that contain x are updated while(x<=N) {c[x]+=Val; X+=lowbit (x); }}intSum (intx) { //Query down intsum=0; while(x>0) {sum+=C[x]; X-=lowbit (x); } returnsum;}intMain () { while(~SCANF ("%d",&N) {mes (c,0); intb; FF (i,1, N) {scanf ("%d%d",&a,&b); Update (A,1); Update (b+1,-1); } F (I,1, N) printf ("%d", Sum (i)); printf ("%d\n", Sum (n)); } return 0;}
View Code
(2) Line tree +lazy thought
Summary: Lazy, updated only to the interval, you can save a lot of time.
#include <iostream>#include<cstring>#include<cmath>#include<queue>#include<algorithm>#include<cstdio>#defineF (I,A,B) for (int i=a;i<b;i++)#defineFF (I,A,B) for (int i=a;i<=b;i++)#defineMes (A, b) memset (A,b,sizeof (a))#defineINF 0x3f3f3f3f#defineLL Long Longusing namespacestd;Const intn=10010, max=100010;intn,c[max<<2];voidBuildintOintLintR) {C[o]=0; if(L==R)return ; intMid= (l+r) >>1; Build (o<<1, L,mid); Build (o<<1|1, mid+1, R);}voidUpdateintOintLintRintLintR) { if(L<=L&&R<=R) {c[o]++;return; }//key: Only update to the interval, beginning to update to each individual point, decisive T,//then with L==l&&r==r, and decisive Mle intMid= (l+r) >>1; if(mid<l) Update (o<<1|1, l,r,mid+1, R); Else if(r<=mid) Update (o<<1, L,r,l,mid); Else{update (o<<1, L,r,l,mid); Update (o<<1|1, l,r,mid+1, R); }}intAns[max];voidQueryintOintLintR) { if(C[o]) { for(intI=l; i<=r; i++) Ans[i]+=c[o];//is also lazy thought, marked by the interval is added } if(L==R)return;//It's still a point to ask . intMid= (l+r) >>1; Query (o<<1, L,mid); Query (o<<1|1, mid+1, R);}intMain () { while(~SCANF ("%d",&N) {build (1,1, N); Fill (ans+1, ans+1+n,0);//fil function intb; FF (i,1, N) {scanf ("%d%d",&a,&b); Update (1, A, B,1, N); } query (1,1, N); F (i,1, N) printf ("%d", Ans[i]); printf ("%d\n", Ans[n]); } return 0;}
View Code
HDU 1556 segment tree or tree array, insert segment to find points