fork(2) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int N;
  4. int X=1025, Y=1025;
  5. int btree[1030][1030], matrix[1030][1030];
  6. int mod (int a, int b) {
  7. int ans = a%b;
  8. if(ans < 0)
  9. ans+=b;
  10. return ans;
  11. }
  12.  
  13. void update(int x, int y, int val) {
  14. while(x<=X) {
  15. int yy= y;
  16. while(yy <= Y) {
  17. btree[x][yy]+=val;
  18. yy+= yy & -yy;
  19. }
  20. x+= x & -x;
  21. }
  22. }
  23.  
  24. int query(int x, int y) {
  25. int res=0;
  26. while(x>0) {
  27. int yy =y;
  28. while(yy>0) {
  29. res+=btree[x][yy];
  30. yy-=yy& -yy;
  31. }
  32. x-=x& -x;
  33. }
  34. return res;
  35. }
  36.  
  37. map<char,int> mp;
  38. int main() {
  39. int n, m;
  40. cin >> n >> m;
  41. string s;
  42. mp['N']=0;
  43. mp['E']=1;
  44. mp['W']=2;
  45. mp['S'] = 3;
  46. char x;
  47. map<int, char> mp1;
  48. mp1[0]='N';
  49. mp1[1]='E';
  50. mp1[2]='W';
  51. mp1[3]='S';
  52. memset(btree,0,sizeof(btree));
  53. for(int i=1;i<=n;i++) {
  54. for(int j=1;j<=m;j++) {
  55. scanf("\n%c",&x);
  56. update(i,j,mp[x]);
  57. update(i+1,j,-mp[x]);
  58. update(i,j+1,-mp[x]);
  59. update(i+1,j+1,mp[x]);
  60.  
  61. }
  62. }
  63. int q;
  64. scanf("%d",&q);
  65. for(int i=0;i<q;i++) {
  66. char cmd;
  67. scanf("\n%c",&cmd);
  68. if(cmd=='C') {
  69. int x1,x2,y1,y2,d;
  70. scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&d);
  71. int val;
  72. if(d==0) {
  73. val=1;
  74. }
  75. else
  76. val=-1;
  77.  
  78. update(x1,y1,val);
  79. update(x2+1,y1,-val);
  80. update(x1,y2+1,-val);
  81. update(x2+1,y2+1,val);
  82. }
  83. else
  84. {
  85. int x,y;
  86. scanf("%d %d",&x,&y);
  87. int res= query(x,y);
  88. printf("%c\n",mp1[mod(res,4)]);
  89. }
  90. }
  91. return 0;
  92. }
  93.  
  94.  
Success #stdin #stdout 0.01s 11760KB
stdin
5 5
ESWNE
NWWWN
EEESE
SSWSN
SNWEN
5
C 2 1 4 1 1
D 3 1
D 3 3
C 2 1 5 2 0
D 3 1
stdout
N
E
E