1452: [Jsoi2009]count time limit:10 Sec Memory limit:64 MB
submit:1769 solved:1059
[Submit] [Status] [Discuss] Description
Input
Output
Sample Input
Sample Output1
2HINT
Source Solution
Ignore the title of the saying ... A wall of ....
Simple question, bare tree array nested tree array
The number of colors $c<=100$ is small, consider handling each color separately
So for each color, open a two-dimensional tree array to maintain
Another two-dimensional array to record the current color of each position
1 action, set the $ (x, y) $ point in the original color tree array to 0, place 1 in the new color tree array, and update the state of the color
2 operation, interval query, apply prefix and, add and subtract to get the current sub-matrix and
Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}intN,m,q;intzt[310][310];structtreenode{inttree[310][310]; Treenode () {memset (tree,0,sizeof(tree)); intLowbit (intx) {returnx&-x;} voidChangeintXintYintdel) { for(intI=x; i<=n; i+=lowbit (i)) for(intJ=y; j<=m; j+=Lowbit (j)) Tree[i][j]+=del; } intQueryintXinty) {intans=0; for(intI=x; I i-=lowbit (i)) for(intJ=y; J j-=Lowbit (j)) ans+=Tree[i][j]; returnans; } intAskintX1,intY1,intX2,inty2) { returnQuery (x2,y2)-query (x2,y1-1)-query (x1-1, y2) +query (x1-1, y1-1); }}color[ the];intMain () {n=read (); m=read (); for(intI=1; i<=n; i++) for(intcol,j=1; j<=m; J + +) Col=read (), Color[col].change (I,j,1), zt[i][j]=Col; Q=read (); for(intI=1; i<=q; i++) { intopt=read (), X1,y1,x2,y2,col; if(opt==1) {x1=read (), Y1=read (), col=read (); Color[zt[x1][y1]].change (X1,y1,-1); ZT[X1][Y1]=Col; Color[col].change (X1,y1,1); Continue; } x1=read (), X2=read (), Y1=read (), Y2=read (), col=read (); printf ("%d\n", Color[col].ask (x1,y1,x2,y2)); } return 0;}
Less than 5 minutes, see, Think, Write, 1A series ....
"BZOJ-1452" Count tree array nested tree array