Submission #1038967

# Submission time Handle Problem Language Result Execution time Memory
1038967 2024-07-30T10:10:28 Z TrinhKhanhDung Growing Trees (BOI11_grow) C++14
100 / 100
128 ms 9092 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

struct SegmentTree{
    vector<ll> val, lazy;
    int n;

    SegmentTree(int _n = 0){
        n = _n;
        val.resize(4 * n + 3, 0);
        lazy.resize(4 * n + 3, 0);
    }

    void push(int i){
        lazy[i * 2] += lazy[i];
        lazy[i * 2 + 1] += lazy[i];
        val[i * 2] += lazy[i];
        val[i * 2 + 1] += lazy[i];
        lazy[i] = 0;
    }

    void update(int i, int l, int r, int u, int v, ll c){
        if(v < l || r < u) return;
        if(u <= l && r <= v){
            val[i] += c;
            lazy[i] += c;
            return;
        }
        push(i);
        int m = (l + r) >> 1;
        update(i * 2, l, m, u, v, c);
        update(i * 2 + 1, m + 1, r, u, v, c);
        val[i] = max(val[i * 2], val[i * 2 + 1]);
    }

    void update(int u, int v, ll c){
        update(1, 1, n, u, v, c);
    }

    int getPos(ll c){
        if(val[1] < c) return n + 1;
        int i = 1, l = 1, r = n;
        while(l < r){
            push(i);
            int m = (l + r) >> 1;
            if(val[i * 2] >= c){
                i = i * 2;
                r = m;
            }
            else{
                i = i * 2 + 1;
                l = m + 1;
            }
        }
        return l;
    }

    ll getVal(int p){
        int i = 1, l = 1, r = n;
        while(l < r){
            push(i);
            int m = (l + r) >> 1;
            if(p <= m){
                i = i * 2;
                r = m;
            }
            else{
                i = i * 2 + 1;
                l = m + 1;
            }
        }
        return val[i];
    }
};

void solve(){
    int n, q;
    cin >> n >> q;
    SegmentTree it(n);

    vector<int> a(n + 3);
    for(int i=1; i<=n; i++){
        cin >> a[i];
    }
    sort(a.begin() + 1, a.begin() + n + 1);
    for(int i=1; i<=n; i++){
        it.update(i, i, a[i]);
    }
//    for(int i=1; i<=n; i++){
//        cout << it.getVal(i) << ' ';
//    }
//    cout << '\n';
    while(q--){
        char t; cin >> t;
        ll u, v; cin >> u >> v;
        if(t == 'F'){
            int l = it.getPos(v);
//            cout << l << '\n';
            if(l > n) continue;
            minimize(u, n - l + 1);
            ll c = it.getVal(l + u - 1);
//            cout << c << '\n';
            int r = it.getPos(c) - 1;
//            cout << r << '\n';
            it.update(l, r, 1);

            int R = it.getPos(c + 1) - 1;
//            cout << R << '\n';
            int L = R - (u - (r - l + 1)) + 1;
//            cout << L << '\n';
            it.update(L, R, 1);
        }
        else{
            int l = it.getPos(u);
            int r = it.getPos(v + 1);
            cout << r - l << '\n';
        }
//        for(int i=1; i<=n; i++){
//            cout << it.getVal(i) << ' ';
//        }
//        cout << '\n';
    }
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("task.inp", "r", stdin);
//    freopen("task.out", "w", stdout);

    int t = 1;
//    cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 77 ms 7568 KB Output is correct
2 Correct 105 ms 7800 KB Output is correct
3 Correct 128 ms 7448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 608 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 29 ms 1628 KB Output is correct
6 Correct 34 ms 1884 KB Output is correct
7 Correct 4 ms 856 KB Output is correct
8 Correct 20 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 2140 KB Output is correct
2 Correct 36 ms 2128 KB Output is correct
3 Correct 2 ms 860 KB Output is correct
4 Correct 23 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2244 KB Output is correct
2 Correct 34 ms 2128 KB Output is correct
3 Correct 7 ms 860 KB Output is correct
4 Correct 33 ms 2132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 5908 KB Output is correct
2 Correct 85 ms 6504 KB Output is correct
3 Correct 12 ms 1884 KB Output is correct
4 Correct 93 ms 6340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 6484 KB Output is correct
2 Correct 87 ms 7024 KB Output is correct
3 Correct 112 ms 6640 KB Output is correct
4 Correct 12 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 6928 KB Output is correct
2 Correct 62 ms 7516 KB Output is correct
3 Correct 125 ms 7416 KB Output is correct
4 Correct 12 ms 1880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 7764 KB Output is correct
2 Correct 84 ms 6744 KB Output is correct
3 Correct 36 ms 7772 KB Output is correct
4 Correct 91 ms 7504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 7684 KB Output is correct
2 Correct 88 ms 7764 KB Output is correct
3 Correct 122 ms 8792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 9092 KB Output is correct