#133. 二維樹狀陣列 1:單點修改,區間查詢
阿新 • • 發佈:2018-12-28
題目描述
這是一道模板題。
給出一個 n\times mn×m 的零矩陣 AA,你需要完成如下操作:
1 x y k
:表示元素 A_{x,y}Ax,y 自增 kk;2 a b c d
:表示詢問左上角為 (a,b)(a,b),右下角為 (c,d)(c,d) 的子矩陣內所有數的和。
輸入格式
輸入的第一行有兩個正整數 n,mn,m;
接下來若干行,每行一個操作,直到檔案結束。
輸出格式
對於每個 2
操作,輸出一個整數,表示對於這個操作的回答。
樣例
樣例輸入
2 2
1 1 1 3
1 2 2 4
2 1 1 2 2
樣例輸出
7
資料範圍與提示
對於 10\%10% 的資料,n=1n=1;
對於另 10\%10% 的資料,m=1m=1;
對於全部資料,1\le n,m\le 2^{12},1\le x,a,c\le n,1\le y,b,d\le m,|k|\le 10^51≤n,m≤212,1≤x,a,c≤n,1≤y,b,d≤m,∣k∣≤105,保證運算元目不超過 3\times 10^53×105,且詢問的子矩陣存在。
#include<bits/stdc++.h> using namespace std; int n,m; const int N=5000; long long s[N][N]; int lowbit(int x) { return x&(-x); } void updata(int x,int y,int z) { for(int i=x;i<=n;i+=lowbit(i)){ for(int j=y;j<=m;j+=lowbit(j)){ s[i][j]+=z; } } } long long sum(int x,int y) { long long res=0; for(int i=x;i>0;i-=lowbit(i)){ for(int j=y;j>0;j-=lowbit(j)){ res+=s[i][j]; } } return res; } int main() { memset(s,0,sizeof(s)); scanf("%d %d",&n,&m); int k; while(scanf("%d",&k)==1){ if(k==1){ int x,y,z; scanf("%d %d %d",&x,&y,&z); updata(x,y,z); } else{ int x1,y1,x2,y2; scanf("%d %d %d %d",&x1,&y1,&x2,&y2); printf("%lld\n",sum(x2,y2)+sum(x1-1,y1-1)-sum(x1-1,y2)-sum(x2,y1-1)); } } return 0; }