Submission #478790

# Submission time Handle Problem Language Result Execution time Memory
478790 2021-10-08T11:40:50 Z MohammadAghil Growing Trees (BOI11_grow) C++14
100 / 100
257 ms 4552 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll const mod = 1e9 + 7, maxn = 1e5 + 5, inf = 1e18 + 5;

int seg[maxn << 2], n, h[maxn], lazy[maxn << 2];

void pull(int x, int k){
    seg[x] += k;
    lazy[x] += k;
}

void shift(int x){
    pull(x << 1, lazy[x]);
    pull(x << 1 | 1, lazy[x]);
    lazy[x] = 0;
}

void upd(int l, int r, int x = 1, int lx = 0, int rx = n){
    if(l <= lx && r >= rx){
        pull(x, 1);
        return;
    } if(l >= rx || r <= lx) return;
    shift(x);
    int mid = (lx + rx) >> 1;
    upd(l, r, x << 1, lx, mid);
    upd(l, r, x << 1 | 1, mid, rx);
    seg[x] = max(seg[x << 1], seg[x << 1 | 1]);
}

int query(int k, int x = 1, int lx = 0, int rx = n){
    if(lx + 1 == rx) {
        if(seg[x] > k) return lx;
        else return rx;
    }
    shift(x);
    int mid = (lx + rx) >> 1;
    if(seg[x << 1] > k) return query(k, x << 1, lx, mid);
    return query(k, x << 1 | 1, mid, rx);
}

int get(int i, int x = 1, int lx = 0, int rx = n){
    if(lx + 1 == rx) return seg[x];
    shift(x);
    int mid = (lx + rx) >> 1;
    if(i < mid) return get(i, x << 1, lx, mid);
    return get(i, x << 1 | 1, mid, rx);
}

void build(int x = 1, int lx = 0, int rx = n){
    if(lx + 1 == rx){
        seg[x] = h[lx];
        return;
    }
    int mid = (lx + rx) >> 1;
    build(x << 1, lx, mid);
    build(x << 1 | 1, mid, rx);
    seg[x] = max(seg[x << 1], seg[x << 1 | 1]);
}

int main(){
    int q; cin >> n >> q;
    for(int i = 0; i < n; i++) cin >> h[i];
    sort(h, h + n);
    build();
    while(q--){
        char t; cin >> t;
        if(t == 'F'){
            int c, h; cin >> c >> h;
            int fst = query(h - 1);
            if(fst == n) continue;
            int en = min(fst + c - 1, n - 1);
            int val = get(en);
            int l_val = query(val-1), r_val = query(val);
            if(fst < l_val) upd(fst, l_val);
            int k = en - l_val + 1;
            upd(r_val - k, r_val);
        }else{
            int l, r; cin >> l >> r;
            cout << query(r) - query(l - 1) << '\n';
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 186 ms 2892 KB Output is correct
2 Correct 244 ms 4424 KB Output is correct
3 Correct 188 ms 4296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 8 ms 332 KB Output is correct
3 Correct 8 ms 332 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 171 ms 608 KB Output is correct
6 Correct 232 ms 760 KB Output is correct
7 Correct 11 ms 464 KB Output is correct
8 Correct 159 ms 1224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 800 KB Output is correct
2 Correct 221 ms 1816 KB Output is correct
3 Correct 4 ms 336 KB Output is correct
4 Correct 174 ms 1328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 848 KB Output is correct
2 Correct 217 ms 1756 KB Output is correct
3 Correct 15 ms 464 KB Output is correct
4 Correct 198 ms 1792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 1676 KB Output is correct
2 Correct 218 ms 3916 KB Output is correct
3 Correct 49 ms 1356 KB Output is correct
4 Correct 134 ms 4000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 176 ms 2652 KB Output is correct
2 Correct 208 ms 4088 KB Output is correct
3 Correct 166 ms 4268 KB Output is correct
4 Correct 55 ms 1252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 2844 KB Output is correct
2 Correct 168 ms 3960 KB Output is correct
3 Correct 169 ms 4420 KB Output is correct
4 Correct 51 ms 1232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 2756 KB Output is correct
2 Correct 216 ms 2844 KB Output is correct
3 Correct 58 ms 3428 KB Output is correct
4 Correct 163 ms 3844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 214 ms 2884 KB Output is correct
2 Correct 231 ms 4372 KB Output is correct
3 Correct 212 ms 4552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 257 ms 3312 KB Output is correct