#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<vector>#include<map>#include<Set>#include<stack>#defineMP Make_pair#definePA pair<int,int>#definePB Push_back#defineFi first#defineSe Secondusing namespaceStd;inlinevoidGet_int (int&y) {X=0;CharCh=getchar ();intf=1; while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=getchar ();} x*=F;} InlinevoidPut_int (intX) { Charch[ -];inttop=0; if(x==0) ch[++top]='0'; while(X) ch[++top]=x%Ten+'0', x/=Ten; while(top) Putchar (ch[top--]); Putchar ('\ n');}//=======================================Const intmaxn=4010;intX1,x2,y1,y2,ans;BOOLKEY[MAXN][MAXN];intn,q,kase;voidModify_y (intOxintOyintLintRintPintq) { if(L==p && r==q) {Key[ox][oy]^=1; return; } intMid= (l+r) >>1; if(Q<=mid) Modify_y (ox,oy<<1, l,mid,p,q); if(p>=mid+1) Modify_y (ox,oy<<1|1, mid+1, r,p,q); if(P<=mid && q>=mid+1) Modify_y (ox,oy<<1, L,mid,p,mid), Modify_y (ox,oy<<1|1, mid+1, r,mid+1, q);}voidModify_x (intOxintLintRintPintq) { if(L==p && r==q) {modify_y (ox,1,1, N,y1,y2); return; } intMid= (l+r) >>1; if(Q<=mid) Modify_x (ox<<1, l,mid,p,q); if(p>=mid+1) modify_x (ox<<1|1, mid+1, r,p,q); if(P<=mid && q>=mid+1) modify_x (ox<<1, L,mid,p,mid), modify_x (ox<<1|1, mid+1, r,mid+1, q);}voidQuery_y (intOxintOyintLintR) {Ans^=Key[ox][oy]; if(L==R)return; intMid= (l+r) >>1; if(Y1<=mid) Query_y (ox,oy<<1, L,mid); if(y1>=mid+1) Query_y (ox,oy<<1|1, mid+1, R);}voidQuery_x (intOxintLintR) {query_y (ox,1,1, N); if(L==R)return; intMid= (l+r) >>1; if(X1<=mid) Query_x (ox<<1, L,mid); if(x1>=mid+1) query_x (ox<<1|1, mid+1, R);}intMain () {get_int (Kase); for(intKase=1; kase<=kase;kase++) {get_int (n), Get_int (Q); memset (Key,false,sizeof(Key)); for(intI=1; i<=q;i++) { CharCh=getchar (); while(ch!='C'&& ch!='Q') ch=GetChar (); if(ch=='C') {get_int (x1), Get_int (y1), Get_int (x2), get_int (y2); Modify_x (1,1, N,X1,X2); } if(ch=='Q') {get_int (x1), get_int (y1); Ans=0; Query_x (1,1, N); Put_int (Ans); }} putchar ('\ n'); } return 0;}
Segment Tree
1#include <cstring>2#include <cstdio>3#include <algorithm>4#include <iostream>5 using namespacestd;6 Const intmaxn= .;7 intN,q,kase,c[maxn][maxn],x1,x2,y1,y2;8InlineintLowbit (intx) {returnx& (-x);}9InlinevoidModify (intXinty)Ten { One for(inti=x;i<=n;i+=lowbit (i)) A for(intJ=y;j<=n;j+=lowbit (j)) c[i][j]^=1; - } -InlineintQuery (intXinty) the { - intret=0; - for(inti=x;i;i-=lowbit (i)) - for(intJ=y;j;j-=lowbit (j)) ret^=C[i][j]; + returnret; - } + intMain () A { at //freopen ("c.in", "R", stdin); -scanf"%d",&Kase); - for(intKase=1; kase<=kase;kase++) - { -scanf"%d%d",&n,&Q); -Memset (c,0,sizeof(c)); in for(intI=1; i<=q;i++) - { to CharCh=getchar (); while(ch!='C'&& ch!='Q') ch=GetChar (); + if(ch=='C') - { thescanf"%d%d%d%d",&x1,&y1,&x2,&y2); *Modify (X1,y1), Modify (x1,y2+1), Modify (x2+1, y1), Modify (x2+1, y2+1); $ }Panax Notoginseng if(ch=='Q') - { thescanf"%d%d",&x1,&y1); +printf"%d\n", Query (x1,y1)); A } the } +Puts""); - } $ return 0; $}
BIT
POJ 2155 2-D line segment Tree | | 2-D bit