Submission #364276

# Submission time Handle Problem Language Result Execution time Memory
364276 2021-02-08T18:20:45 Z Falcon Growing Trees (BOI11_grow) C++17
100 / 100
107 ms 3052 KB
#pragma GCC optimize("O2")

#include <bits/stdc++.h>

#ifdef DEBUG
#include "debug.hpp"
#endif

using namespace std;

#define all(c)              (c).begin(), (c).end()
#define rall(c)             (c).rbegin(), (c).rend()
#define traverse(c, it)     for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N)           for(int i = 0; i < (N); ++i)
#define rrep(i, N)          for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N)          for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e)       for(int i = (s); i <= (e); ++i)
#define rep3(i, s, e, d)    for(int i = (s); (d) >= 0 ? i <= (e) : i >= (e); i += (d))

#ifdef DEBUG
#define debug(x...)         { \
                            ++dbg::depth; \
                            string dbg_vals = dbg::to_string(x); \
                            --dbg::depth; \
                            dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
                            }

#define light_debug(x)      { \
                            dbg::light = true; \
                            dbg::dout << __func__ << ":" << __LINE__; \
                            dbg::dout << "  " << #x << " = " << x << endl; \
                            dbg::light = false; \
                            }
#else
#define debug(x...)         42
#define light_debug(x)      42
#endif


using ll = long long;

template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }

template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }

template<typename T>
class fenwick_tree {
    int n;
    vector<T> a;

public:

    fenwick_tree(int n_) : n{n_}, a(n + 1) {}

    void update(int i, T v) {
        for(++i; i <= n; i += i & -i) a[i] += v;
    }

    T query(int i) const {
        T s{};
        for(++i; i > 0; i -= i & -i) s += a[i];
        return s;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    #ifdef DEBUG
    freopen("debug", "w", stderr);
    #endif

    int N, M; cin >> N >> M;
    fenwick_tree<int> bit(N);
    {
        vector<int> h(N); for(int& x : h) cin >> x;
        sort(all(h));
        rep(i, N) bit.update(i, h[i]), bit.update(i + 1, -h[i]); 
    }

    auto lower_b = [&](int h) -> int {
        int i{-1};
        for(int k{1 << __lg(N)}; k; k >>= 1)
            if(i + k < N && bit.query(i + k) < h)
                i += k;
        return i += 1;
    };

    auto upd = [&](int l, int r) -> void {
        bit.update(l, 1), bit.update(r + 1, -1);
    };

    rep(_, M) {
        char t; cin >> t;
        if(t == 'F') {
            int c, h; cin >> c >> h;
            int i{lower_b(h)};
            if(i + c >= N)
                upd(i, N - 1);
            else {
                int x = bit.query(i + c - 1);
                int j{lower_b(x)}, k{lower_b(x + 1) - 1};
                upd(i, j - 1);
                upd(k - (c - (j - i)) + 1, k);
            }

        } else if(t == 'C') {
            int mn, mx; cin >> mn >> mx;
            cout << lower_b(mx + 1) - lower_b(mn) << '\n';
        }
    }
    

    #ifdef DEBUG
    dbg::dout << "\nExecution time: " << clock() * 1000 / CLOCKS_PER_SEC  << "ms" << endl;
    #endif

    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 65 ms 2072 KB Output is correct
2 Correct 95 ms 2588 KB Output is correct
3 Correct 46 ms 2344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 3 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 48 ms 1516 KB Output is correct
6 Correct 59 ms 1784 KB Output is correct
7 Correct 4 ms 492 KB Output is correct
8 Correct 31 ms 1132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 1772 KB Output is correct
2 Correct 52 ms 1772 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 38 ms 1388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 1948 KB Output is correct
2 Correct 58 ms 1772 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 52 ms 1772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 1896 KB Output is correct
2 Correct 84 ms 2000 KB Output is correct
3 Correct 16 ms 1004 KB Output is correct
4 Correct 43 ms 2004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 1736 KB Output is correct
2 Correct 90 ms 2232 KB Output is correct
3 Correct 48 ms 2264 KB Output is correct
4 Correct 16 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 1840 KB Output is correct
2 Correct 67 ms 2068 KB Output is correct
3 Correct 46 ms 2480 KB Output is correct
4 Correct 17 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 2460 KB Output is correct
2 Correct 88 ms 1984 KB Output is correct
3 Correct 30 ms 1772 KB Output is correct
4 Correct 73 ms 1932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 2204 KB Output is correct
2 Correct 93 ms 2324 KB Output is correct
3 Correct 107 ms 2664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 3052 KB Output is correct