Test instructions: N balloons, n operations (color each time a balloon is applied to the interval [a, a]), and the number of times each balloon is painted in the last color.
n can be taken to 100000, so if you want to for,for, then explode! Burst! Burst!
1. Line-segment tree practices:
A simple line of the tree, did not do it for a long time, sure enough himself or too food ...
Create a line segment tree to initialize the value of the number of times it is painted to 0.
When updating, find the area to be painted, coloring number of +1.
Ask the time from the top down to find that interval, each time the value is handed out.
1 #defineLC (a) ((a<<1) +1)2 #defineRC (a) ((a<<1) +2)3 #defineMID (A, B) ((A+B) >>1)4#include <stdio.h>5 6 structnode{7 intL,r;8 intmid;9 intVal;Ten }; One A Const intmaxn=111111; -Node t[maxn*4]; - the voidCreateintPintLintR) { -T[p].l=l,t[p].r=r,t[p].mid=mid (l,r), t[p].val=0;//Initialize all Val to be 0 - if(L==R)return;//If you find the bottom, quit . - Create (LC (p), l,t[p].mid); +Create (RC (p), t[p].mid+1, R); - } + A voidUpdateintPintLintR) { at if(T[P].L==L&&T[P].R==R) {//Find this node, +1, recursive exit -t[p].val++; - return ; - } - if(T[P].L==T[P].R)return;//not found this node, recursive exit, special case exit - if(t[p].mid>=r) Update (LC (p), l,r);//go to the left subtree . in Else if(t[p].mid<l) Update (RC (p), l,r);//right sub-tree to find - Else{//if it's a separate interval, look both ways. to Update (LC (p), l,t[p].mid); +Update (RC (p), t[p].mid+1, R); - } the } * $ intQueryintPintLintR) {Panax Notoginseng if(T[P].L==L&&T[P].R==R)returnT[p].val;//If this interval is found, it returns. Recursion, and the previous values are added. - if(T[P].L==T[P].R)return 0;//Export of special cases the if(R<=t[p].mid)returnQuery (LC (p), l,r) +T[p].val; + Else if(L>t[p].mid)returnQuery (RC (p), l,r) +T[p].val; A Else returnQuery (LC (p), L,t[p].mid) +query (RC (p), t[p].mid+1, R) +T[p].val; the } + - intMain () { $ intn,a,b; $ while(SCANF ("%d", &n)!=eof&&n!=0){ -Create0,1, n); - for(intI=1; i<=n;i++){ thescanf"%d%d",&a,&b); -Update0, A, b);Wuyi } the for(intI=1; i<=n;i++){ - intSum=query (0, i,i); Wuprintf"%d", sum); - if(i!=n) printf (" "); About } $printf"\ n"); - } - return 0; -}
2. Tree-like array practices:
HDU-1556 Color the ball (segment tree and tree-like array)