fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  6. #define gold 0
  7.  
  8. #define fi first
  9. #define se second
  10. #define pb push_back
  11. #define mp make_pair
  12. #define sz(x) int(x.size())
  13. #define all(x) x.begin(), x.end()
  14. #define freopen(x) freopen(x".in", "r", stdin), freopen (x".out", "w", stdout);
  15. #define ex exit(0)
  16.  
  17. int dx[] = {1, 0, -1, 0};
  18. int dy[] = {0, 1, 0, -1};
  19.  
  20. typedef long long ll;
  21. typedef pair<int, int> pii;
  22.  
  23. const int N = (int) 2e5;
  24. const ll M = (ll) 1e8;
  25. const int INF = 2e9;
  26. const ll BIG = 7e17;
  27.  
  28. int t[N * 4], z[N * 4];
  29.  
  30. void push(int v, int tl, int tr) {
  31. if (v + v + 1 < N * 4 && z[v] == 1) {
  32. int tm = (tl + tr) >> 1;
  33. z[v + v] = z[v];
  34. z[v + v + 1] = z[v];
  35. t[v + v] = tm - tl + 1 - t[v + v];
  36. t[v + v + 1] = tr - tm - t[v + v + 1];
  37. z[v] = 0;
  38. }
  39. }
  40.  
  41. int get(int v, int tl, int tr, int l, int r) {
  42. if (l <= tl && r >= tr)
  43. return t[v];
  44. if (l > tr || r < tl)
  45. return 0;
  46. int tm = (tl + tr) >> 1;
  47. push(v, tl, tr);
  48. return get(v + v, tl, tm, l, r) + get(v + v + 1, tm + 1, tr, l, r);
  49. }
  50.  
  51. void upd(int v, int tl, int tr, int l, int r) {
  52. if (l <= tl && r >= tr) {
  53. z[v] = 1;
  54. t[v] = tr - tl + 1 - t[v];
  55. return;
  56. }
  57. if (l > tr || r < tl) {
  58. return;
  59. }
  60. int tm = (tl + tr) >> 1;
  61. push(v, tl, tr);
  62. upd(v + v, tl, tm, l, r);
  63. upd(v + v + 1, tm + 1, tr, l, r);
  64. t[v] = t[v + v] + t[v + v + 1];
  65. }
  66.  
  67. int main() {
  68. //fast_io;
  69. int n, m;
  70. cin >> n >> m;
  71. while(m--) {
  72. int t, l, r;
  73. cin >> t >> l >> r;
  74. if (t == 0)
  75. upd(1, 1, n, l, r);
  76. else
  77. cout << get(1, 1, n, l, r) << '\n';
  78. }
  79. return 0;
  80. }
Success #stdin #stdout 0s 9720KB
stdin
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4
stdout
1
2