Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <iomanip>
- #include <utility>
- #include <vector>
- #include <string>
- #include <cmath>
- #include <set>
- #include <map>
- using namespace std;
- typedef long long ll;
- typedef pair <int, int> ii;
- typedef vector <int> vi;
- typedef vector <ii> vii;
- class SegmentTree
- {
- private: vii st; int n;
- void build (int p, int L, int R)
- {
- if (L == R) st[p] = { a[L], 1 };
- else
- {
- build(2*p, L, (L + R) / 2);
- build(2*p + 1, (L + R) / 2 + 1, R);
- ii p1 = st[2*p], p2 = st[2*p + 1];
- if (p1.first == p2.first) st[p] = { p1.first, p1.second + p2.second };
- else if (p1.first > p2.first) st[p] = p1;
- else st[p] = p2;
- }
- }
- void replace (int p, int L, int R, int i, int num)
- {
- if (L == R && R == i) st[p] = {num, 1};
- else if (L <= i && i <= R)
- {
- replace(2*p, L, (L+R) / 2, i, num);
- replace(2*p + 1, (L+R) / 2 + 1, R, i, num);
- ii p1 = st[2*p], p2 = st[2*p + 1];
- if (p1.first == p2.first) st[p] = { p1.first, p1.second + p2.second };
- else if (p1.first > p2.first) st[p] = p1;
- else st[p] = p2;
- }
- }
- ii task (int p, int L, int R, int i, int j)
- {
- if (i > R || j < L) return {0, -1};
- if (L >= i && R <= j) return st[p];
- ii p1 = task(2*p, L, (L + R) / 2, i, j);
- ii p2 = task(2*p + 1, (L + R) / 2 + 1, R, i, j);
- if (p1.second == -1) return p2;
- if (p2.second == -1) return p1;
- if (p1.first == p2.first) return { p1.first, p1.second + p2.second };
- else if (p1.first > p2.first) return p1;
- else return p2;
- }
- public: vi a;
- SegmentTree (int N)
- {
- n = N; a.assign(n, 0); st.assign(4*n, {0,0});
- for (int i = 0; i < n; ++i) cin >> a[i];
- build(1, 0, n - 1);
- }
- ii task(int i, int j) { return task(1, 0, n - 1, i, j); }
- void replace (int i, int num) { replace(1, 0, n - 1, i, num); }
- };
- int main()
- {
- ios_base :: sync_with_stdio(false);
- cin.tie(nullptr);
- int n; cin >> n;
- SegmentTree ST(n);
- int k; cin >> k;
- for (int i = 0; i < k; ++i)
- {
- char c; cin >> c; int a, b; cin >> a >> b;
- if (c == 's') cout << ST.task(a - 1, b - 1).second << " ";
- else ST.replace(a - 1, b);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement