답안 #1084533

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084533 2024-09-06T11:18:38 Z BLOBVISGOD Growing Trees (BOI11_grow) C++17
100 / 100
83 ms 3284 KB
#include "bits/stdc++.h"
#pragma GCC optimize("O3")
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 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 = 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 = lowerbound(col);
            int numleft = c - (R1-L);
            // get first index larger than col
            int R = 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 lowerbound(hi+1)-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';
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 2300 KB Output is correct
2 Correct 77 ms 2836 KB Output is correct
3 Correct 34 ms 2608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 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 33 ms 1532 KB Output is correct
6 Correct 38 ms 1772 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 22 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 1620 KB Output is correct
2 Correct 37 ms 1932 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 28 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 1880 KB Output is correct
2 Correct 39 ms 1616 KB Output is correct
3 Correct 4 ms 600 KB Output is correct
4 Correct 37 ms 1916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 2132 KB Output is correct
2 Correct 68 ms 2360 KB Output is correct
3 Correct 12 ms 860 KB Output is correct
4 Correct 29 ms 2372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 1956 KB Output is correct
2 Correct 68 ms 2496 KB Output is correct
3 Correct 34 ms 2672 KB Output is correct
4 Correct 11 ms 888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 2072 KB Output is correct
2 Correct 51 ms 2444 KB Output is correct
3 Correct 33 ms 2580 KB Output is correct
4 Correct 11 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 2716 KB Output is correct
2 Correct 70 ms 2344 KB Output is correct
3 Correct 20 ms 2140 KB Output is correct
4 Correct 56 ms 2232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 2564 KB Output is correct
2 Correct 76 ms 2808 KB Output is correct
3 Correct 83 ms 3024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 3284 KB Output is correct