Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- const int maxn = 1e5 + 100;
- const int inf = 1e9;
- struct Node {
- int mx;
- int push;
- Node() { mx = -inf; push = 0; }
- Node(int a) { mx = a; push = 0; }
- };
- Node combine(const Node &a, const Node &b) {
- return Node(max(a.mx, b.mx));
- }
- int a[maxn];
- Node t[4 * maxn];
- void build(int id, int l, int r) {
- if (r - l == 1) {
- t[id].mx = a[l];
- return;
- }
- int m = (l + r) / 2;
- build(id * 2 + 1, l, m);
- build(id * 2 + 2, m, r);
- t[id] = combine(t[id * 2 + 1], t[id * 2 + 2]);
- }
- void push(int id, int l, int r) {
- t[id].mx += t[id].push;
- if (r - l > 1) {
- t[2 * id + 1].push += t[id].push;
- t[2 * id + 2].push += t[id].push;
- }
- t[id].push = 0;
- }
- Node get(int id, int l, int r, int lq, int rq) {
- push(id, l, r);
- if (r <= lq || l >= rq)
- return Node();
- if (lq <= l && r <= rq)
- return t[id];
- int m = (l + r) / 2;
- Node x = get(id * 2 + 1, l, m, lq, rq);
- Node y = get(id * 2 + 2, m, r, lq, rq);
- return combine(x, y);
- }
- void upd(int id, int l, int r, int lq, int rq, int x) {
- push(id, l, r);
- if (r <= lq || l >= rq)
- return;
- if (lq <= l && r <= rq) {
- t[id].push += x;
- push(id, l, r);
- return;
- }
- int m = (l + r) / 2;
- upd(id * 2 + 1, l, m, lq, rq, x);
- upd(id * 2 + 2, m, r, lq, rq, x);
- t[id] = combine(t[id * 2 + 1], t[id * 2 + 2]);
- }
- void solve() {
- int n, q;
- cin >> n;
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- }
- build(0, 0, n);
- cin >> q;
- int l, r, v;
- char c;
- while (q--) {
- cin >> c >> l >> r;
- l -= 1;
- if (c == 'a') {
- cin >> v;
- upd(0, 0, n, l, r, v);
- }
- if (c == 'm') {
- cout << get(0, 0, n, l, r).mx << ' ';
- }
- }
- cout << '\n';
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.setf(ios_base::boolalpha);
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement