Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define mp make_pair
- #define sz size
- #define ll long long
- #define ld long double
- #define fs first
- #define sc second
- #define forn(i, f, t) for(int i = f; i < t; i++)
- #define all(x) (x).begin(), (x).end()
- #define ins insert
- const int INF = 2147483647;
- const int MOD = 1000000007;
- const ll INF64 = 9223372036854775807;
- const ld EPS = 1e-7;
- using namespace std;
- int a[100001], n;
- vector<ll> t, upd;
- void build(int v, int l, int r){
- if (l == r - 1){
- t[v] = (l < n ? a[l] : -INF64);
- upd[v] = 0ll;
- return;
- }
- upd[v] = 0ll;
- int m = (l + r) / 2;
- build(v * 2, l, m);
- build(v * 2 + 1, m, r);
- t[v] = max(t[v * 2], t[v * 2 + 1]);
- }
- void push(int v){
- if (v * 2 >= t.sz()){
- upd[v] = 0ll;
- return;
- }
- upd[v * 2] += upd[v];
- upd[v * 2 + 1] += upd[v];
- t[v * 2] += upd[v];
- t[v * 2 + 1] += upd[v];
- upd[v] = 0ll;
- }
- ll getMax(int v, int l, int r, int fr, int to){
- push(v);
- if (l >= r || fr >= to) return 0ll;
- if (l == fr && r == to) return t[v];
- int m = (l + r) / 2;
- return max(getMax(v * 2, l, m, fr, min(to, m)), getMax(v * 2 + 1, m, r, max(fr, m), to));
- }
- void modify(int v, int l, int r, int fr, int to, int val){
- push(v);
- if (l >= r || fr >= to) return;
- if (l == fr && r == to){
- t[v] += (ll)val;
- upd[v] += (ll)val;
- return;
- }
- int m = (l + r) / 2;
- modify(v * 2, l, m, fr, min(to, m), val);
- modify(v * 2 + 1, m, r, max(m, fr), to, val);
- t[v] = max(t[v * 2], t[v * 2 + 1]);
- }
- void printTree(int p){
- forn(i, 0, p * 2) printf("%lld ", t[i]); printf("\n");
- }
- int main(){
- int to, fr, val, m;
- scanf("%d", &n);
- forn(i, 0, n) scanf("%d", a + i);
- int p = 1 << (int)(ceil(log(n) / log(2)) + 1);
- t.assign(4 * p, 0);
- upd.assign(4 * p, 0);
- build(1, 0, p * 2);
- scanf("%d", &m);
- char c;
- forn(i, 0, m){
- scanf("\n%c %d %d ", &c, &fr, &to);
- // cout << fr << " " << to << "\n";
- if (c == 'a'){
- scanf("%d", &val);
- modify(1, 0, p * 2, fr - 1, to, val);
- }
- else if (c == 'm') printf("%lld\n", getMax(1, 0, p * 2, fr - 1, to));
- // printTree(p);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement