Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <ctype.h>
- #ifdef __unix__
- #define getchar getchar_unlocked
- #define putchar putchar_unlocked
- #endif // __unix__
- int read()
- {
- int ch,ans=0;
- while(!isdigit(ch=getchar()));
- do
- ans=ans*10+ch-'0';
- while (isdigit(ch=getchar()));
- return ans;
- }
- int n,**aib;
- int sum(int i,int jj)
- {
- int ans=0,j;
- for(;i;i-=i&-i)
- for(j=jj;j;j-=j&-j)
- ans+=aib[i][j];
- return ans;
- }
- void update(int i,int jj,int x)
- {
- int j;
- for(;i<=n;i+=i&-i)
- for(j=jj;j<=n;j+=j&-j)
- aib[i][j]+=x;
- }
- int query(int i,int j,int x,int y)
- {
- return sum(x,y)-sum(i-1,y)-sum(x,j-1)+sum(i-1,j-1);
- }
- int main(void)
- {
- freopen("aib2d.in","r",stdin);
- freopen("aib2d.out","w",stdout);
- int i,j;
- n=read();
- aib=(int**)calloc((n+1),sizeof(int*));
- for(i=1;i<=n;++i)
- aib[i]=(int*)calloc((n+1),sizeof (int));
- for(i=1;i<=n;++i)
- for(j=1;j<=n;++j)
- update(i,j,read());
- int q,x,y;
- for(q=read();q;--q)
- switch(read())
- {
- case 1:
- i=read();
- j=read();
- x=read();
- update(i,j,x);
- break;
- case 2:
- i=read();
- j=read();
- x=read();
- y=read();
- printf("%d\n",query(i,j,x,y));
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement