Submission #1000262

# Submission time Handle Problem Language Result Execution time Memory
1000262 2024-06-17T07:28:59 Z Whisper Growing Trees (BOI11_grow) C++17
100 / 100
50 ms 3152 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 2684 KB Output is correct
2 Correct 50 ms 2396 KB Output is correct
3 Correct 23 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 20 ms 760 KB Output is correct
6 Correct 23 ms 828 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 12 ms 672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 856 KB Output is correct
2 Correct 25 ms 856 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 15 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 860 KB Output is correct
2 Correct 25 ms 860 KB Output is correct
3 Correct 3 ms 600 KB Output is correct
4 Correct 26 ms 992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2136 KB Output is correct
2 Correct 47 ms 2140 KB Output is correct
3 Correct 8 ms 856 KB Output is correct
4 Correct 19 ms 2156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 2268 KB Output is correct
2 Correct 42 ms 2360 KB Output is correct
3 Correct 25 ms 1880 KB Output is correct
4 Correct 8 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2492 KB Output is correct
2 Correct 29 ms 2652 KB Output is correct
3 Correct 22 ms 2140 KB Output is correct
4 Correct 8 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 2428 KB Output is correct
2 Correct 42 ms 2408 KB Output is correct
3 Correct 16 ms 2652 KB Output is correct
4 Correct 27 ms 2640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 2652 KB Output is correct
2 Correct 48 ms 2588 KB Output is correct
3 Correct 44 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 3152 KB Output is correct