Advertisement
a53

AIB2D

a53
Jun 11th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <ctype.h>
  4. #ifdef __unix__
  5. #define getchar getchar_unlocked
  6. #define putchar putchar_unlocked
  7. #endif // __unix__
  8.  
  9. int read()
  10. {
  11. int ch,ans=0;
  12. while(!isdigit(ch=getchar()));
  13. do
  14. ans=ans*10+ch-'0';
  15. while (isdigit(ch=getchar()));
  16. return ans;
  17. }
  18.  
  19. int n,**aib;
  20. int sum(int i,int jj)
  21. {
  22. int ans=0,j;
  23. for(;i;i-=i&-i)
  24. for(j=jj;j;j-=j&-j)
  25. ans+=aib[i][j];
  26. return ans;
  27. }
  28.  
  29. void update(int i,int jj,int x)
  30. {
  31. int j;
  32. for(;i<=n;i+=i&-i)
  33. for(j=jj;j<=n;j+=j&-j)
  34. aib[i][j]+=x;
  35. }
  36.  
  37. int query(int i,int j,int x,int y)
  38. {
  39. return sum(x,y)-sum(i-1,y)-sum(x,j-1)+sum(i-1,j-1);
  40. }
  41.  
  42. int main(void)
  43. {
  44. freopen("aib2d.in","r",stdin);
  45. freopen("aib2d.out","w",stdout);
  46. int i,j;
  47. n=read();
  48. aib=(int**)calloc((n+1),sizeof(int*));
  49. for(i=1;i<=n;++i)
  50. aib[i]=(int*)calloc((n+1),sizeof (int));
  51. for(i=1;i<=n;++i)
  52. for(j=1;j<=n;++j)
  53. update(i,j,read());
  54. int q,x,y;
  55. for(q=read();q;--q)
  56. switch(read())
  57. {
  58. case 1:
  59. i=read();
  60. j=read();
  61. x=read();
  62. update(i,j,x);
  63. break;
  64. case 2:
  65. i=read();
  66. j=read();
  67. x=read();
  68. y=read();
  69. printf("%d\n",query(i,j,x,y));
  70. break;
  71. }
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement