Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int n, q;
- const long long INF = -40000000000;
- vector<pair<long long, int>> tree;
- vector<long long> a, add;
- void build(int v, int tl, int tr)
- {
- if (tl == tr)
- tree[v] = (tl < a.size() ? make_pair(a[tl], tl) : make_pair(INF, -1));
- else
- {
- int tm = (tl + tr) / 2;
- build(v * 2, tl, tm);
- build(v * 2 + 1, tm + 1, tr);
- tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
- }
- }
- void push(int v)
- {
- if (2 * v < add.size()){
- add[2 * v] += add[v];
- tree[2 * v].first += add[v];
- }
- if (2 * v + 1 < add.size()){
- add[2 * v + 1] += add[v];
- tree[2 * v + 1].first += add[v];
- }
- add[v] = 0;
- }
- void modify(int vl, int vr, int ql, int qr, int v, long long x)
- {
- push(v);
- if (ql > qr)
- return;
- if (ql == vl && qr == vr)
- {
- add[v] += x;
- tree[v].first += x;
- push(v);
- }
- else
- {
- int m = (vl + vr) / 2;
- if (ql <= min(qr, m))
- modify(vl, m, ql, min(qr, m), v * 2, x);
- if (qr >= max(ql, m + 1))
- modify(m + 1, vr, max(ql, m + 1), qr, v * 2 + 1, x);
- tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
- }
- }
- pair<long long, int> get_max(int vl, int vr, int ql, int qr, int v)
- {
- push(v);
- if (ql > qr)
- return make_pair(INF, -1);
- if (ql == vl && qr == vr)
- return tree[v];
- else
- {
- int m = (vl + vr) / 2;
- pair<long long, int> a = make_pair(INF, -1), b = make_pair(INF, -1);
- if (ql <= min(qr, m))
- a = get_max(vl, m, ql, min(qr, m), v * 2);
- if (qr >= max(ql, m + 1))
- b = get_max(m + 1, vr, max(ql, m + 1), qr, v * 2 + 1);
- return max(a, b);
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- // freopen("0", "r", stdin);
- cin >> n;
- a.resize(n * 2);
- for (int i = 0; i < n; ++i)
- cin >> a[i];
- long long bin = 1;
- while (bin < n)
- bin *= 2;
- n = bin;
- add.assign(n * 2, 0);
- tree.resize(n * 2);
- build(1, 0, n - 1);
- cin >> q;
- for (int i = 0; i < q; ++i)
- {
- string query;
- int l, r;
- long long num;
- cin >> query;
- if (query[0] == 'm')
- {
- cin >> l >> r;
- pair<long long, int> p = get_max(0, n - 1, --l, --r, 1);
- cout << p.first << "\n";
- }
- else
- {
- cin >> l >> r >> num;
- modify(0, n - 1, --l, --r, 1, num);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement