代码如下:
#include <iostream> using namespace std; int n,m; int sum[1005][1005],vis[1005][1005]; int lowbit(int x) { return x & (-x); } void up(int x,int y,int c) { for(int i=x;i<=n;i+=lowbit(i)) { for(int j=y;j<=n;j+=lowbit(j)) { sum[i][j]+=c; } } } int getsum(int x,int y) { int ans = 0; for(int i=x;i>0;i-=lowbit(i)) { for(int j=y;j>0;j-=lowbit(j)) { ans += sum[i][j]; } } return ans; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { int x; cin>>vis[i][j]; if(vis[i][j])up(i,j,1); } } for(int i=1;i<=m;i++) { int tag,a,b; cin>>tag>>a>>b; if(tag==1) { if(vis[a][b]){ up(a,b,-1); vis[a][b]=0; } else { up(a,b,1); vis[a][b]=1; } } else { int x,y; cin>>x>>y; cout<<getsum(x,y)+getsum(a-1,b-1)-getsum(x,b-1)-getsum(a-1,y)<<endl; } } return 0; }