Submission #1038940

# Submission time Handle Problem Language Result Execution time Memory
1038940 2024-07-30T09:47:02 Z TrinhKhanhDung Growing Trees (BOI11_grow) C++14
10 / 100
97 ms 9040 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]);
    }
    while(q--){
        char t; cin >> t;
        ll u, v; cin >> u >> v;
        if(t == 'F'){
            int l = it.getPos(v);
            minimize(u, n - l + 1);
            ll c = it.getVal(l + u - 1);
            int r = it.getPos(c) - 1;
            it.update(l, r, 1);

            int R = it.getPos(c + 1) - 1;
            int L = R - (u - (r - l + 1)) + 1;
            it.update(L, R, 1);
        }
        else{
            int l = it.getPos(u);
            int r = it.getPos(v + 1);
            cout << r - l << '\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 Incorrect 71 ms 7504 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 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 604 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 28 ms 1724 KB Output is correct
6 Incorrect 30 ms 1884 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 1884 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 2132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 56 ms 5716 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 77 ms 6480 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 77 ms 6736 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 97 ms 7884 KB Output is correct
2 Incorrect 82 ms 6900 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 66 ms 7460 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 66 ms 9040 KB Output is correct