fork download
  1. using namespace std;
  2. #include <bits/stdc++.h>
  3.  
  4. #define fori(a, b) for(int i = a;i <= b;i++)
  5. #define forj(a, b) for(int j = a;j <= b;j++)
  6. #define forl(a, b) for(int l = a;l <= b;l++)
  7. #define fordi(b, a) for(int i = b;i >= a;i--)
  8. #define fordj(b, a) for(int j = b;j >= a;j--)
  9. #define fordl(b, a) for(int l = b;l >= a;l--)
  10.  
  11. #define MAX 100100
  12. #define MAGIC 29
  13. #define MOD 1000000007
  14. #define F first
  15. #define S second
  16. #define pb push_back
  17. #define mp make_pair
  18.  
  19. const double PI = acos(-1);
  20. typedef long long ll;
  21.  
  22. int n, m, q, res[MAX], cont, acm[MAX];
  23. vector<int> adj[MAX];
  24. pair<int, int> rm[MAX];
  25. pair<int, int> add[MAX];
  26. int swp[MAX];
  27. bool shelf[1010][1010];
  28. bool rev[1010];
  29.  
  30. bool fadd(int a, int b){
  31.  
  32. if(!rev[a]){
  33. if(shelf[a][b]) return false;
  34. else{
  35. cont++;
  36. acm[a]++;
  37. return shelf[a][b] = true;
  38. }
  39. }
  40. else{
  41. if(shelf[a][b]){
  42. cont++;
  43. acm[a]++;
  44. shelf[a][b] = false;
  45. return true;
  46. }
  47. else return false;
  48. }
  49. }
  50.  
  51. bool frm(int a, int b){
  52.  
  53. //cout << "rm " << a << " " << b << " " << rev[a] << endl;
  54.  
  55. if(!rev[a]){
  56. if(shelf[a][b]){
  57. cont--;
  58. acm[a]--;
  59. shelf[a][b] = false;
  60. return true;
  61. }
  62. else return false;
  63. }
  64. else{
  65. if(shelf[a][b]) return false;
  66. else{
  67. cont--;
  68. acm[a]--;
  69. shelf[a][b] = true;
  70. return true;
  71. }
  72. }
  73. }
  74.  
  75. bool frv(int a){
  76.  
  77. cont += m - 2*acm[a];
  78. acm[a] = m - acm[a];
  79. rev[a] = !rev[a];
  80.  
  81. return true;
  82. }
  83.  
  84. void f(int pos){
  85. bool done = false;
  86.  
  87. if(add[pos].F != -1) done = fadd(add[pos].F, add[pos].S);
  88. if(rm[pos].F != -1) done = frm(rm[pos].F, rm[pos].S);
  89. if(swp[pos] != -1) done = frv(swp[pos]);
  90.  
  91. //cout << "f " << pos << " " << done << endl;
  92.  
  93. res[pos] = cont;
  94.  
  95. fori(0, (int)adj[pos].size() - 1) f(adj[pos][i]);
  96.  
  97. if(done){
  98. if(add[pos].F != -1) frm(add[pos].F, add[pos].S);
  99. if(rm[pos].F != -1) fadd(rm[pos].F, rm[pos].S);
  100. if(swp[pos] != -1) frv(swp[pos]);
  101. }
  102. }
  103.  
  104. int main(){
  105. //freopen("in", "r", stdin);
  106. int a, b, c;
  107.  
  108. memset(shelf, false, sizeof(shelf));
  109. memset(rev, false, sizeof(rev));
  110. memset(acm, 0, sizeof(acm));
  111. cont = 0;
  112.  
  113. add[0] = mp(-1, -1);
  114. rm[0] = mp(-1, -1);
  115. swp[0] = -1;
  116.  
  117. cin >> n >> m >> q;
  118. fori(1, q){
  119. scanf("%d", &a);
  120.  
  121. if(a == 1 || a == 2){
  122. scanf("%d %d", &b, &c);
  123. }
  124. else scanf("%d", &b);
  125.  
  126. add[i] = mp(-1, -1);
  127. rm[i] = mp(-1, -1);
  128. swp[i] = -1;
  129.  
  130. if(a == 1) add[i] = mp(b, c);
  131. else if(a == 2) rm[i] = mp(b, c);
  132. else if(a == 3) swp[i] = b;
  133. else adj[b].pb(i);
  134.  
  135. if(a != 4) adj[i-1].pb(i);
  136. }
  137.  
  138. f(0);
  139.  
  140. fori(1, q) printf("%d\n", res[i]);
  141.  
  142. return 0;
  143. }
  144.  
Success #stdin #stdout 0s 8376KB
stdin
Standard input is empty
stdout
Standard output is empty