Submission #1084548

# Submission time Handle Problem Language Result Execution time Memory
1084548 2024-09-06T11:29:07 Z BLOBVISGOD Growing Trees (BOI11_grow) C++17
100 / 100
50 ms 3280 KB
#include "bits/stdc++.h"
using namespace std;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;

struct fentree{
    vi a;
    void add(int at, int val){
        for(at++; at<sz(a); at+=at&(-at)) a[at] += val;
    }void addrange(int l, int r, int val){ // [  )
        add(r,-val);
        add(l,val);
    }
    fentree(vi b){
        a.resize(sz(b)+1);
        rep(i,0,sz(b)) addrange(i,i+1,b[i]);
    }int get(int at){
        int ans = 0;
        for(at++; at>0; at-=at&(-at)) ans += a[at];
        return ans;
    }

    int lowerbound(int v){
        int at = 0, sm=0;
        for(int pw  = 1<<__lg(sz(a)-1); pw>0; pw/=2){
            if (at+pw < sz(a) and sm + a[at+pw] < v){
                at += pw;
                sm += a[at];
            }
        }return at;
    }
};

int main(){
    cin.tie(NULL),cin.sync_with_stdio(false);
    
    int n,m; cin >> n >> m;

    vi a(n);
    rep(i,0,n) cin >> a[i];
    sort(all(a));
    fentree fen(a);

    // auto lowerbound = [&](int v) -> int {
    //     int lo = 0, hi = sz(a);
    //     while (lo<hi){
    //         int mid = (lo+hi)/2;
    //         if (fen.get(mid) < v) lo = mid+1;
    //         else hi = mid;
    //     }return lo;
    // };

    auto update = [&](int c, int h) -> void {
        int L = fen.lowerbound(h);
        if (sz(a)-L <= c) fen.addrange(L,sz(a),1); 
        else{
            int col = fen.get(L+c-1);
            // find first index of this col
            int R1 = fen.lowerbound(col);
            int numleft = c - (R1-L);
            // get first index larger than col
            int R = fen.lowerbound(col+1);
            int L1 = R - numleft;

            // update everything
            fen.addrange(L,R1,1);
            fen.addrange(L1,R,1);
        }
    };

    auto query = [&](int lo, int hi) -> int {
        return fen.lowerbound(hi+1)-fen.lowerbound(lo);
    };

    while(m--){
        char t; cin >> t;
        if (t=='F'){
            int c,h; cin>> c >> h;
            update(c,h);
        }else{
            int lo,hi; cin >> lo >> hi;
            cout << query(lo,hi) << '\n';
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 30 ms 2304 KB Output is correct
2 Correct 41 ms 2872 KB Output is correct
3 Correct 28 ms 2756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 18 ms 1532 KB Output is correct
6 Correct 21 ms 1720 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 15 ms 1176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 1628 KB Output is correct
2 Correct 21 ms 1880 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 17 ms 1448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 1884 KB Output is correct
2 Correct 28 ms 1744 KB Output is correct
3 Correct 3 ms 604 KB Output is correct
4 Correct 20 ms 1828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 2136 KB Output is correct
2 Correct 34 ms 2356 KB Output is correct
3 Correct 7 ms 860 KB Output is correct
4 Correct 23 ms 2156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 2096 KB Output is correct
2 Correct 40 ms 2356 KB Output is correct
3 Correct 28 ms 2492 KB Output is correct
4 Correct 9 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 2080 KB Output is correct
2 Correct 30 ms 2376 KB Output is correct
3 Correct 28 ms 2628 KB Output is correct
4 Correct 7 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 2732 KB Output is correct
2 Correct 35 ms 2348 KB Output is correct
3 Correct 17 ms 2140 KB Output is correct
4 Correct 32 ms 2300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 2560 KB Output is correct
2 Correct 39 ms 2808 KB Output is correct
3 Correct 50 ms 3016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 3280 KB Output is correct