Raslboyy

3325

Dec 21st, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <math.h>
  6. #include <climits>
  7.  
  8. //#pragma GCC optimize("O3, unroll-loops");
  9.  
  10. #include <list>
  11. #include <map>
  12. #include <set>
  13. #include <queue>
  14. #include <unordered_map>
  15. #include <unordered_set>
  16.  
  17. //#include <chrono>
  18. //#include <fstream>
  19. //#include <bits/stdc++.h>
  20.  
  21. #define mp make_pair
  22. #define pb push_back
  23. #define eb emplace_back
  24. #define x first
  25. #define y second
  26. #define sz(x) (int)x.size()
  27. #define all(x) begin(x), end(x)
  28. #define rall(x) rbegin(x), rend(x)
  29. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  30. #define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
  31.  
  32. using namespace std;
  33.  
  34. typedef unsigned long long ull;
  35. typedef long long ll;
  36. typedef long double ld;
  37. typedef pair<int, int> pi;
  38. typedef pair<ll, ll> pl;
  39. typedef vector<int> veci;
  40. typedef vector<bool> vecb;
  41. typedef vector<vector<int>> vvi;
  42. typedef vector<vector<bool>> vvb;
  43.  
  44. const int INF_I = 1e9;
  45. const ll INF_LL = 1e18;
  46.  
  47. int n;
  48. veci a;
  49. veci t;
  50.  
  51. void build(int c = 1, int tl = 0, int tr = n - 1) {
  52.     if (tl == tr) {
  53.         t[c] = int(a[tl] == 0);
  54.         return;
  55.     }
  56.     int tm = (tl + tr) >> 1;
  57.     build(c << 1, tl, tm);
  58.     build((c << 1) + 1, tm + 1, tr);
  59.     t[c] = t[c << 1] + t[(c << 1) + 1];
  60. }
  61.  
  62. int Get(int l, int r, int c = 1, int tl = 0, int tr = n - 1) {
  63.     if (l > r)
  64.         return 0;
  65.     if (tl == l && tr == r)
  66.         return t[c];
  67.     int tm = (tr + tl) >> 1;
  68.     return
  69.         Get(l, min(tm, r), c << 1, tl, tm)
  70.         +
  71.         Get(max(tm + 1, l), r, (c << 1) + 1, tm + 1, tr);
  72. }
  73.  
  74. void Set(int val, int i, int c = 1, int tl = 0, int tr = n - 1) {
  75.     if (tl == tr) {
  76.         t[c] = val==0;
  77.         return;
  78.     }
  79.     int tm = (tr + tl) >> 1;
  80.     if (i <= tm)
  81.         Set(val, i, c << 1, tl, tm);
  82.     else
  83.         Set(val, i, (c << 1) + 1, tm + 1, tr);
  84.     t[c] = t[c << 1] + t[(c << 1) + 1];
  85. }
  86.  
  87. int main() {
  88.     /*ifstream cin("in.txt");
  89.     ofstream cout("out.txt");
  90.     auto start_time = chrono::high_resolution_clock::now();*/
  91.  
  92.     ios::sync_with_stdio(false);
  93.     cin.tie(0);
  94.     cout.tie(0);
  95.  
  96.    
  97.     cin >> n;
  98.     a.assign(n, 0);
  99.     t.assign(n << 2, 0);
  100.     FOR(i, 0, n)
  101.         cin >> a[i];
  102.  
  103.     build();
  104.  
  105.     int q;
  106.     cin >> q;
  107.     while (q--) {
  108.         char ch; int l, r;
  109.         cin >> ch >> l >> r;
  110.         if (ch == 'u')
  111.             Set(r, --l);
  112.         else
  113.             cout << Get(--l, --r) << " ";
  114.     }
  115.  
  116.    
  117.     /*auto end_time = chrono::high_resolution_clock::now();
  118.     chrono::duration<double> duration = end_time - start_time;
  119.     cout << duration.count() << "\n";
  120.     cin.close();
  121.     cout.close();*/
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment