题意:
矩阵上的单点更新,范围求和
#include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <string> #include <cctype> #include <complex> #include <cassert> #include <utility> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; typedef pair<int,int> PII; typedef long long ll; #define lson l,m,rt<<1 #define pi acos(-1.0) #define rson m+1,r,rt<<11 #define All 1,N,1 #define N 1100 #define read freopen("in.txt", "r", stdin) const ll INFll = 0x3f3f3f3f3f3f3f3fLL; const int INF= 0x7ffffff; const int mod = 1000000007; int bit[N][N],n,m,a[N][N]; int lowbit(int x){ return x&(-x); } void add(int x,int y,int d){ for(int i=x;i<=n;i+=lowbit(i)) for(int j=y;j<=n;j+=lowbit(j)) bit[i][j]+=d; } int sum(int x,int y){ int num=0; for(int i=x;i>0;i-=lowbit(i)) for(int j=y;j>0;j-=lowbit(j)) num+=bit[i][j]; return num; } int main() { int op,x1,x2,y1,y2,d; while(~scanf("%d",&op)){ if(op==3)break; else if(op==0){ scanf("%d",&n); memset(bit,0,sizeof(bit)); memset(a,0,sizeof(a)); } else if(op==1){ scanf("%d%d%d",&x1,&y1,&d); x1++; y1++; if(a[x1][y1]+d<0) d=-a[x1][y1]; add(x1,y1,d); a[x1][y1]+=d; } else{ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); int tmp=sum(x2+1,y2+1)-sum(x2+1,y1)-sum(x1,y2+1)+sum(x1,y1); printf("%d\n",tmp); } } return 0; }
时间: 2024-10-08 19:37:29