Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // tiom4eg's precompiler options
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- // IO settings
- #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
- // Quick types
- #define ll long long
- #define ld long double
- #define ull unsigned long long
- #define pii pair <int, int>
- #define vi vector <int>
- #define mi vector <vector <int>>
- // Quick functions
- #define endl "\n"
- #define F first
- #define S second
- #define all(a) a.begin(), a.end()
- //#define sz(a) (int)(a.size())
- #define pb push_back
- #define mp make_pair
- // Quick fors
- #define FOR(i, a, b) for (int i = a; i < b; ++i)
- #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
- #define RFOR(i, a, b) for (int i = a; i >= b; --i)
- #define EACH(e, a) for (auto& e : a)
- // Pragmas
- #ifndef TIOM4EG
- #pragma GCC optimize("O3") // let the chaos begin!
- #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
- #pragma GCC comment(linker, "/stack:200000000")
- #endif
- // PBDS
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
- #define ook order_of_key
- #define fbo find_by_order
- using namespace __gnu_pbds;
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- using namespace std;
- mt19937 rng(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
- #define int long long
- 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;
- const ll INFLL = 1e18 + 7;
- struct Fenwick {
- vi t;
- int s;
- void init(int n) {
- s = n + 3;
- t.resize(s);
- }
- void upd(int p, int x) { for (; p < s; p += p & -p) t[p] += x; }
- int get(int p) {
- int r = 0;
- for (; p; p -= p & -p) r += t[p];
- return r;
- }
- };
- int p[MAX], w[MAX], sz[MAX], o[MAX], re[MAX];
- Fenwick t, v[MAX];
- signed main() {
- fastIO;
- int n, q, s = 1, c = 1; cin >> n >> q;
- FOR(i, 1, n + 1) {
- char x; cin >> x;
- if (x == '(') o[s] = ++w[p[++s] = c], c = re[i] = s;
- else if (c > 1) c = p[re[i] = c];
- }
- t.init(s);
- FOR(i, 1, s + 1) v[i].init(w[i]), t.upd(i, w[i] * (w[i] + 1) / 2);
- RFOR(i, s, 1) sz[p[i]] += ++sz[i];
- while (q--) {
- int op, l, r; cin >> op >> l >> r, l = re[l], r = re[r];
- if (op == 1) v[p[l]].upd(o[l], 1), t.upd(p[l], -w[p[l]]--);
- else {
- int x = o[r] - o[l] + 1 - v[p[l]].get(o[r]) + v[p[l]].get(o[l] - 1);
- cout << t.get(r - 1 + sz[r]) - t.get(l - 1) + x * (x + 1) / 2 << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement