fork download
  1. //longlong5a6
  2. #include <bits/stdc++.h>
  3. #define pii pair<int,int>
  4. #define F first
  5. #define S second
  6. #define bit(i, x)((x >> i) & 1)
  7. #define sobit(i) __builtin_popcount((int)(i))
  8. #define pb push_back
  9. #define all(x) x.begin(),x.end()
  10. #define vi vector<int>
  11. #define For(i,x,n) for(int (i)=(int)(x);(i)<=(int)(n);(i)++)
  12. #define round(m,n) setprecision((int)m) << fixed << double(n)
  13. #define down "\n"
  14. #define TASK "biendoi"
  15. #define maxn 1001
  16.  
  17. using namespace std;
  18.  
  19. bool f[maxn][maxn];
  20. bool g[maxn];
  21. int c[maxn];
  22.  
  23. struct query
  24. {
  25. int ty,x,y;
  26. }Q[maxn*100];
  27.  
  28. vector<query> his;
  29. vector<int>line[maxn*100];
  30. int n,m,q;
  31. int cnt=0;
  32. int res[maxn*100];
  33. void up(int i)
  34. {
  35. if (Q[i].ty==1)
  36. {
  37. int x=Q[i].x;
  38. int y=Q[i].y;
  39. if(f[x][y]^g[x]==0)
  40. {
  41. c[x]++;
  42. f[x][y]^=1;
  43. cnt++;
  44. his.pb(Q[i]);
  45. }
  46. else his.pb({4,0,0});
  47. }
  48. if (Q[i].ty==2)
  49. {
  50. int x=Q[i].x;
  51. int y=Q[i].y;
  52. if(f[x][y]^g[x]==1)
  53. {
  54. c[x]--;
  55. f[x][y]^=1;
  56. cnt--;
  57. his.pb(Q[i]);
  58. }
  59. else his.pb({4,0,0});
  60. }
  61. if (Q[i].ty==3)
  62. {
  63. int x=Q[i].x;
  64. cnt+=(m-c[x])-c[x];
  65. c[x]=m-c[x];
  66. g[x]^=1;
  67. his.pb(Q[i]);
  68. }
  69. }
  70. void Back(query a)
  71. {
  72. auto [ty,x,y]=a;
  73. if (ty==1)
  74. {
  75. cnt--;
  76. c[x]--;
  77. f[x][y]^=1;
  78. }
  79. if (ty==2)
  80. {
  81. cnt++;
  82. c[x]++;
  83. f[x][y]^=1;
  84. }
  85. if (ty==3)
  86. {
  87. cnt+=(m-c[x])-c[x];
  88. c[x]=m-c[x];
  89. g[x]^=1;
  90. }
  91. }
  92. void roll(int pos)
  93. {
  94. while ((int) his.size()>pos)
  95. {
  96. Back(his.back());
  97. his.pop_back();
  98. }
  99. }
  100. void dfs(int u)
  101. {
  102. up(u);
  103. res[u]=cnt;
  104. int pos=his.size();
  105. for(auto i:line[u])
  106. {
  107. dfs(i);
  108. roll(pos);
  109. }
  110. }
  111.  
  112.  
  113. int main()
  114. {
  115. ios_base::sync_with_stdio(0);
  116. cin.tie();
  117. cout.tie();
  118. if (fopen("in.txt","r"))
  119. {
  120. freopen("in.txt","r",stdin);
  121.  
  122. }
  123. if (fopen(TASK".INP","r"))
  124. {
  125. freopen(TASK".INP","r",stdin);
  126. freopen(TASK".OUT","w",stdout);
  127. }
  128. cin>>n>>m>>q;
  129. For(i,1,q)
  130. {
  131. int ty;
  132. cin>>ty;
  133. int x,y;
  134. if (ty!=3&&ty!=4)
  135. {
  136. cin>>x>>y;
  137. Q[i]={ty,x,y};
  138. line[i-1].pb(i);
  139. }
  140. else
  141. {
  142. cin>>x;
  143. Q[i]={ty,x,0};
  144. if (ty==4) line[x].pb(i);
  145. else line[i-1].pb(i);
  146. }
  147. }
  148. dfs(0);
  149. For(i,1,q) cout<<res[i]<<down;
  150. }
  151.  
Success #stdin #stdout 0.01s 6740KB
stdin
Standard input is empty
stdout
Standard output is empty