Advertisement
tiom4eg

day 4, D 100

Jan 12th, 2023
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // tiom4eg's precompiler options
  3. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  4. // IO settings
  5. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
  6. // Quick types
  7. #define ll long long
  8. #define ld long double
  9. #define ull unsigned long long
  10. #define pii pair <int, int>
  11. #define vi vector <int>
  12. #define mi vector <vector <int>>
  13. // Quick functions
  14. #define endl "\n"
  15. #define F first
  16. #define S second
  17. #define all(a) a.begin(), a.end()
  18. //#define sz(a) (int)(a.size())
  19. #define pb push_back
  20. #define mp make_pair
  21. // Quick fors
  22. #define FOR(i, a, b) for (int i = a; i < b; ++i)
  23. #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
  24. #define RFOR(i, a, b) for (int i = a; i >= b; --i)
  25. #define EACH(e, a) for (auto& e : a)
  26. // Pragmas
  27. #ifndef TIOM4EG
  28. #pragma GCC optimize("O3") // let the chaos begin!
  29. #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
  30. #pragma GCC comment(linker, "/stack:200000000")
  31. #endif
  32. // PBDS
  33. #include <ext/pb_ds/assoc_container.hpp>
  34. #include <ext/pb_ds/tree_policy.hpp>
  35. #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  36. #define ook order_of_key
  37. #define fbo find_by_order
  38. using namespace __gnu_pbds;
  39. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  40. using namespace std;
  41. mt19937 rng(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
  42. #define int long long
  43. const int INF = 1e9 + 7, MD = 998244353, MAX = 300005, R = 1 << 19, MOD = 1e9 + 7, MOD2 = 1e9 + 9, MOD12 = MOD * MOD, MOD22 = MOD2 * MOD2, LG = 20, B1 = 300007, B2 = 107, S = 10008;
  44. const ll INFLL = 1e18 + 7;
  45.  
  46. struct Fenwick {
  47.     vi t;
  48.     int s;
  49.     void init(int n) {
  50.         s = n + 3;
  51.         t.resize(s);
  52.     }
  53.     void upd(int p, int x) { for (; p < s; p += p & -p) t[p] += x; }
  54.     int get(int p) {
  55.         int r = 0;
  56.         for (; p; p -= p & -p) r += t[p];
  57.         return r;
  58.     }
  59. };
  60.  
  61. int p[MAX], w[MAX], sz[MAX], o[MAX], re[MAX];
  62. Fenwick t, v[MAX];
  63.  
  64. signed main() {
  65.     fastIO;
  66.     int n, q, s = 1, c = 1; cin >> n >> q;
  67.     FOR(i, 1, n + 1) {
  68.         char x; cin >> x;
  69.         if (x == '(') o[s] = ++w[p[++s] = c], c = re[i] = s;
  70.         else if (c > 1) c = p[re[i] = c];
  71.     }
  72.     t.init(s);
  73.     FOR(i, 1, s + 1) v[i].init(w[i]), t.upd(i, w[i] * (w[i] + 1) / 2);
  74.     RFOR(i, s, 1) sz[p[i]] += ++sz[i];
  75.     while (q--) {
  76.         int op, l, r; cin >> op >> l >> r, l = re[l], r = re[r];
  77.         if (op == 1) v[p[l]].upd(o[l], 1), t.upd(p[l], -w[p[l]]--);
  78.         else {
  79.             int x = o[r] - o[l] + 1 - v[p[l]].get(o[r]) + v[p[l]].get(o[l] - 1);
  80.             cout << t.get(r - 1 + sz[r]) - t.get(l - 1) + x * (x + 1) / 2 << endl;
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement