标签:
题意:给个二维矩阵,矩阵有0或者1两个值,然后有三个操作,Q问区间和,剩下两个是更新点的值
思路:更新点的值直接更新就行了,然后询问区间和的时候就处理一下,每次问的是X1,Y1到X2,Y2的区间和,而树状数组的和是从1,1开始的,所以总的减去多于的在加上多减去的就OK了
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; const int inf=0x3f3f3f3f; const ll INF=0x3f3f3f3f3f3f3f3fll; const int maxn=1010; int sum[maxn][maxn],n; bool vis[maxn][maxn]; int lowbit(int x){return x&(-x);} void update(int x,int y,int val){ for(int i=x;i<maxn;i+=lowbit(i)){ for(int j=y;j<maxn;j+=lowbit(j)){ sum[i][j]+=val; } } } int msum(int x,int y){ int res=0; for(int i=x;i>0;i-=lowbit(i)){ for(int j=y;j>0;j-=lowbit(j)){ res+=sum[i][j]; } } return res; } int main(){ int x1,x2,y1,y2; char ch[10]; while(scanf("%d",&n)!=-1){ memset(sum,0,sizeof(sum)); memset(vis,0,sizeof(vis)); while(n--){ scanf("%s",ch); if(ch[0]=='B'){ scanf("%d%d",&x1,&y1); x1++;y1++; if(vis[x1][y1]) continue; vis[x1][y1]=1;update(x1,y1,1); }else if(ch[0]=='D'){ scanf("%d%d",&x1,&y1); x1++;y1++; if(vis[x1][y1]) update(x1,y1,-1); vis[x1][y1]=0; }else if(ch[0]=='Q'){ scanf("%d%d%d%d",&x1,&x2,&y1,&y2); x1++;x2++;y1++;y2++; if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2); int ans=msum(x2,y2)-msum(x1-1,y2)-msum(x2,y1-1)+msum(x1-1,y1-1); printf("%d\n",ans); } } } return 0; }
标签:
原文地址:http://blog.csdn.net/dan__ge/article/details/51792928