fork download
  1. /*
  2. * @Author: hungeazy
  3. * @Date: 2025-11-16 00:24:36
  4. * @Last Modified by: hungeazy
  5. * @Last Modified time: 2025-11-16 00:45:45
  6. */
  7. #include <bits/stdc++.h>
  8. #include <ext/pb_ds/assoc_container.hpp>
  9. #include <ext/pb_ds/tree_policy.hpp>
  10. // #pragma GCC optimize("O3")
  11. // #pragma GCC optimize("unroll-loops")
  12. // #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
  13. using namespace std;
  14. using namespace __gnu_pbds;
  15. bool M1;
  16. #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  17. #define int long long
  18. #define ll long long
  19. #define ull unsigned long long
  20. #define sz(x) x.size()
  21. #define sqr(x) (1LL * (x) * (x))
  22. #define all(x) x.begin(), x.end()
  23. #define fill(f,x) memset(f,x,sizeof(f))
  24. #define FOR(i,l,r) for(int i=l;i<=r;i++)
  25. #define FOD(i,r,l) for(int i=r;i>=l;i--)
  26. #define debug(x) cout << #x << " = " << x << '\n'
  27. #define ii pair<int,int>
  28. #define iii pair<int,ii>
  29. #define di pair<ii,ii>
  30. #define vi vector<int>
  31. #define vii vector<ii>
  32. #define mii map<int,int>
  33. #define fi first
  34. #define se second
  35. #define pb push_back
  36. #define MOD 1000000007
  37. #define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
  38. #define YES cout << "YES\n"
  39. #define NO cout << "NO\n"
  40. #define MASK(i) (1LL << (i))
  41. #define c_bit(i) __builtin_popcountll(i)
  42. #define BIT(x,i) ((x) & MASK(i))
  43. #define SET_ON(x,i) ((x) | MASK(i))
  44. #define SET_OFF(x,i) ((x) & ~MASK(i))
  45. #define oo 1e18
  46. #define name ""
  47. #define endl '\n'
  48. #define memory() cerr << abs(&M2-&M1)/1024.0/1024 << " MB" << endl
  49. #define time() cerr << endl << "-------------Time:" << 1000.0 * clock() / CLOCKS_PER_SEC << "ms." << endl
  50. template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
  51. template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
  52. template <class T> using ordered_set = tree <T, null_type, less_equal <T>, rb_tree_tag,tree_order_statistics_node_update>;
  53. const int N = (int)5e3+10;
  54. int n,q;
  55. char a[N][N];
  56.  
  57. namespace sub3 {
  58.  
  59. int h[N],pre[N][N],L[N],R[N];
  60.  
  61. bool approved() {
  62. return n <= 400 and q <= 400;
  63. }
  64.  
  65. int calc()
  66. {
  67. FOR(i,1,n)
  68. FOR(j,1,n)
  69. if (a[i][j] == '1') pre[i][j] = 0;
  70. else pre[i][j] = pre[i-1][j]+1;
  71. int ans = 0;
  72. FOR(i,1,n)
  73. {
  74. FOR(j,1,n) h[j] = pre[i][j];
  75. deque<int> dq;
  76. FOR(j,1,n)
  77. {
  78. while (!dq.empty() and h[j] <= h[dq.back()])
  79. dq.pop_back();
  80. L[j] = (!dq.empty() ? dq.back()+1 : 1);
  81. dq.pb(j);
  82. }
  83. dq.clear();
  84. FOD(j,n,1)
  85. {
  86. while (!dq.empty() and h[j] < h[dq.back()])
  87. dq.pop_back();
  88. R[j] = (!dq.empty() ? dq.back()-1 : n);
  89. dq.pb(j);
  90. }
  91. FOR(j,1,n)
  92. {
  93. int x = j-L[j]+1, y = R[j]-j+1;
  94. ans += x*y*pre[i][j];
  95. }
  96. }
  97. return ans;
  98. }
  99.  
  100. void solve(void)
  101. {
  102. cout << calc() << endl;
  103. while (q--)
  104. {
  105. int x,y;
  106. cin >> x >> y;
  107. a[x][y] = (a[x][y] == '0' ? '1' : '0');
  108. cout << calc() << endl;
  109. }
  110. }
  111.  
  112. }
  113.  
  114. bool M2;
  115. signed main()
  116. {
  117. fast;
  118. if (fopen(name".inp","r"))
  119. {
  120. freopen(name".inp","r",stdin);
  121. freopen(name".out","w",stdout);
  122. }
  123. cin >> n >> q;
  124. FOR(i,1,n)
  125. FOR(j,1,n) cin >> a[i][j];
  126. if (sub3::approved()) return sub3::solve(), time(), memory(), 0;
  127. time();
  128. memory();
  129. return 0;
  130. }
  131. // ██░ ██ █ ██ ███▄ █ ▄████
  132. //▓██░ ██▒ ██ ▓██▒ ██ ▀█ █ ██▒ ▀█▒
  133. //▒██▀▀██░▓██ ▒██░▓██ ▀█ ██▒▒██░▄▄▄░
  134. //░▓█ ░██ ▓▓█ ░██░▓██▒ ▐▌██▒░▓█ ██▓
  135. //░▓█▒░██▓▒▒█████▓ ▒██░ ▓██░░▒▓███▀▒
  136. // ▒ ░░▒░▒░▒▓▒ ▒ ▒ ░ ▒░ ▒ ▒ ░▒ ▒
  137. // ▒ ░▒░ ░░░▒░ ░ ░ ░ ░░ ░ ▒░ ░ ░
  138. // ░ ░░ ░ ░░░ ░ ░ ░ ░ ░ ░ ░ ░
  139. // ░ ░ ░ ░ ░ ░
Success #stdin #stdout #stderr 0.01s 5316KB
stdin
Standard input is empty
stdout
0
stderr
-------------Time:6.072ms.
215.551 MB