Submission #1000260

# Submission time Handle Problem Language Result Execution time Memory
1000260 2024-06-17T07:28:22 Z vjudge1 Growing Trees (BOI11_grow) C++17
100 / 100
54 ms 4948 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX_TREE        100005
int numTree, numQuery;
int h[MAX_TREE];
void init(void){
    cin >> numTree >> numQuery;
    for (int i = 1; i <= numTree; ++i) cin >> h[i];
    sort(h + 1, h + numTree + 1);
}

template<class T = int> struct FenwickTree{
    T *F = nullptr;
    int n;
    FenwickTree(int _n = 0){
        resize(_n);
    }
    void resize(int _n = 0){
        this -> n = _n;
        if (F != nullptr) delete[] F;
        F = new T[n + 5];
        clear();
    }
    void clear(void){
        fill(F, F + 1 + n, 0);
    }

    void upd(int p, T val){
        for (; p <= n; p += p & (-p)) F[p] += val;
    }
    T query(int p){
        T res = 0;
        for (; p > 0; p -= p & (-p)) res += F[p];
        return res;
    }

    int lower_bound(int val){
        int res = 0;
        for (int i = __lg(n); i >= 0; --i){
            if ((res | MASK(i)) <= n && val > F[res | MASK(i)]){
                val -= F[res | MASK(i)];
                res |= MASK(i);
            }
        }
        return res + 1;
    }
    int upper_bound(T val){
        int res = 0;
        for (int i = __lg(n); i >= 0; --i){
            if ((res | MASK(i)) <= n && val >= F[res | MASK(i)]){
                val -= F[res | MASK(i)];
                res |= MASK(i);
            }
        }
        return res + 1;
    }
};

int sub[MAX_TREE];
void process(void){
    FenwickTree <int> fen(numTree);
    for (int i = 1; i <= numTree; ++i){
        sub[i] = h[i] - h[i - 1];
        fen.upd(i, sub[i]);
    }

    for (int i = 1; i <= numQuery; ++i){
        char c; cin >> c;
        if(c == 'F'){
            int k, lim; cin >> k >> lim;
            int l = fen.lower_bound(lim);
            int r = min(l + k - 1, numTree);

            if (r == numTree) fen.upd(l, 1);
            else{
                int last = fen.query(r);

                int last_pos = fen.upper_bound(last) - 1;
                int first_pos = fen.lower_bound(last);

                if (last_pos == r){
                    fen.upd(l, 1); fen.upd(r + 1, -1);
                }
                else{
                    fen.upd(l, 1); fen.upd(first_pos, -1);
                    int nw_pos = last_pos - (k - (first_pos - l)) + 1;
                    fen.upd(nw_pos, 1); fen.upd(last_pos + 1, -1);
                }
            }
//            FOR(j, 1, numTree) cout << fen.query(j) << " \n"[j == numTree];
        }
        else{
            int l, r; cin >> l >> r;
            l = fen.lower_bound(l);
            r = fen.upper_bound(r);
            cout << r - l << '\n';
        }
    }
}

signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    init();
    process();
}



# Verdict Execution time Memory Grader output
1 Correct 29 ms 3896 KB Output is correct
2 Correct 50 ms 4176 KB Output is correct
3 Correct 23 ms 3932 KB Output is correct
# Verdict Execution time Memory 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 21 ms 1648 KB Output is correct
6 Correct 23 ms 1724 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 12 ms 1248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 1884 KB Output is correct
2 Correct 25 ms 1844 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 15 ms 1448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 2140 KB Output is correct
2 Correct 25 ms 1876 KB Output is correct
3 Correct 3 ms 604 KB Output is correct
4 Correct 29 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 3156 KB Output is correct
2 Correct 42 ms 3412 KB Output is correct
3 Correct 8 ms 1224 KB Output is correct
4 Correct 21 ms 3416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 3160 KB Output is correct
2 Correct 44 ms 3740 KB Output is correct
3 Correct 22 ms 3672 KB Output is correct
4 Correct 8 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 3420 KB Output is correct
2 Correct 29 ms 3668 KB Output is correct
3 Correct 23 ms 3928 KB Output is correct
4 Correct 8 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 4168 KB Output is correct
2 Correct 43 ms 3388 KB Output is correct
3 Correct 17 ms 3420 KB Output is correct
4 Correct 26 ms 3676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 3920 KB Output is correct
2 Correct 48 ms 4096 KB Output is correct
3 Correct 54 ms 4436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 4948 KB Output is correct