Channel
Test instructions: Interval coloring, asking for the same number of colors in the interval
Idea: Divide the original interval by sqrt (n) intervals, each violent query and the new two sides of the interval, the middle of the interval directly with a hash of the number of nodes in each color. In this case, the lazy idea of a line-like tree is used, and the interval is changed directly to mark the interval, and then the mark is passed until it is divided, and then the update
Code:
#include <stdio.h>#include<math.h>#include<string.h>#include<map>#defineMAXN 100005intN,M,BSIZE,BNUM,X[MAXN];structhash_block{intcls,size; Std::map<int,int>MP;} b[ -];voidPushdown (intID) {Hash_block&hb=B[id]; if(hb.cls!=-1){ for(inti=id*bsize;i<id*bsize+hb.size;i++) x[i]=Hb.cls; Hb.mp.clear (), Hb.mp[hb.cls]=hb.size; Hb.cls=-1; }}voidUpdateintLintRintc) { intlb=l/bsize,rb=r/bsize,ans=0; for(inti=lb+1; i<rb;i++) b[i].cls=C; if(lb!=RB) {pushdown (lb);p Ushdown (RB); for(inti=l;i<lb*bsize+b[lb].size;i++) B[lb].mp[x[i]]--, b[lb].mp[c]++,x[i]=C; for(inti=rb*bsize;i<=r;i++) B[rb].mp[x[i]]--, b[rb].mp[c]++,x[i]=C; }Else{pushdown (lb); for(inti=l;i<=r;i++) B[lb].mp[x[i]]--, b[lb].mp[c]++,x[i]=C; }}intQueryintLintRintc) { intlb=l/bsize,rb=r/bsize,ans=0; for(inti=lb+1; i<rb;i++){ if(b[i].cls==c) ans+=b[i].size; Else if(b[i].cls==-1&&b[i].mp.find (c)!=b[i].mp.end ()) ans+=B[i].mp[c]; } if(lb!=RB) {pushdown (lb);p Ushdown (RB); for(inti=l;i<lb*bsize+b[lb].size;i++) ans+= (x[i]==c); for(inti=rb*bsize;i<=r;i++) ans+= (x[i]==c); }Else{pushdown (lb); for(inti=l;i<=r;i++) ans+= (x[i]==c); } returnans;}voidInitBlock () {bsize=(int) sqrt (n+1e-8); Bnum= (n1)/bsize+1; for(intI=0; i<bnum;i++) {b[i].mp.clear (); B[i].cls=-1; B[i].size=std::min (i*bsize+bsize,n)-i*bsize; } for(intI=0; i<n;i++) {scanf ("%d",&X[i]); B[i/bsize].mp[x[i]]++; }}intq,l,r,z;intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {InitBlock (); while(m--) {scanf ("%d%d%d%d",&q,&l,&r,&z); if(q==1) update (L,R,Z); Elseprintf"%d\n", Query (l,r,z)); } } return 0;}
View Code
"Chunking" HDU 4391 Paint the Wall