#include #include #include #include #define REP(i,a,b) for(i=a;idata);} void intFenwickTreeInit(intFenwickTree *t, int size){int i; t->size=size; rep(i,size) t->data[i]=0;} int intFenwickTreeAdd(intFenwickTree *t,int k,int add){while(ksize)t->data[k]+=add, k|=k+1;} int intFenwickTreeGet(intFenwickTree *t,int k){int res=0; while(k>=0)res+=t->data[k],k=(k&(k+1))-1; return res;} int intFenwickTreeRange(intFenwickTree *t,int a,int b){return intFenwickTreeGet(t,b)-intFenwickTreeGet(t,a-1);} int lastChangeRt[110000], lastChangeCt[110000]; /* last time which row/col is changed*/ int lastChangeRn[110000], lastChangeCn[110000]; /* this row/col are 0 or 1 */ int main(){ int N, Q; int i, j, k, r, c, num, res; int q; char buf[100]; intFenwickTree rt[2], ct[2]; rep(i,2) rt[i] = intFenwickTreeNew(110000); /* rt[0] : sum of [a..b] denotes the number of row which change to 1 in the time (number of queries) from a to b */ rep(i,2) ct[i] = intFenwickTreeNew(110000); assert( scanf("%d%d",&N,&Q)==2 ); assert( 1<=N&&N<=100000 && 1<=Q&&Q<=100000 ); rep(i,2) intFenwickTreeInit(&rt[i], Q+10); rep(i,2) intFenwickTreeInit(&ct[i], Q+10); rep(i,N+1) lastChangeRt[i] = lastChangeCt[i] = 0; rep(i,N+1) lastChangeRn[i] = lastChangeCn[i] = 0; REP(q,1,Q+1){ assert( scanf("%s",buf)==1 ); if(buf[0]=='R' && buf[3]=='S'){ assert( scanf("%d%d",&r,&num)==2 ); assert( 1<=r&&r<=N && 0<=num&&num<=1 ); if(lastChangeRt[r]) intFenwickTreeAdd( &rt[lastChangeRn[r]], lastChangeRt[r], -1 ); /* delete the last change of this row*/ intFenwickTreeAdd( &rt[num], q, 1 ); lastChangeRt[r] = q; lastChangeRn[r] = num; } else if(buf[0]=='C' && buf[3]=='S'){ assert( scanf("%d%d",&c,&num)==2 ); assert( 1<=c&&c<=N && 0<=num&&num<=1 ); if(lastChangeCt[c]) intFenwickTreeAdd( &ct[lastChangeCn[c]], lastChangeCt[c], -1 ); intFenwickTreeAdd( &ct[num], q, 1 ); lastChangeCt[c] = q; lastChangeCn[c] = num; } else if(buf[0]=='R' && buf[3]=='Q'){ assert( scanf("%d",&r)==1 ); assert( 1<=r&&r<=N ); if(lastChangeRn[r]==0){ /* almost(?) 0 */ res = N - intFenwickTreeRange(&ct[1], lastChangeRt[r], q); /* after this row is changed, check the number of columns which change to 1 */ }else{ res = intFenwickTreeRange(&ct[0], lastChangeRt[r], q); } printf("%d\n",res); } else if(buf[0]=='C' && buf[3]=='Q'){ assert( scanf("%d",&c)==1 ); assert( 1<=c&&c<=N ); if(lastChangeCn[c]==0){ res = N - intFenwickTreeRange(&rt[1], lastChangeCt[c], q); }else{ res = intFenwickTreeRange(&rt[0], lastChangeCt[c], q); } printf("%d\n",res); } else { assert(0); } } return 0; }