Submission #1337553

#TimeUsernameProblemLanguageResultExecution timeMemory
1337553vahagngGrowing Trees (BOI11_grow)C++20
10 / 100
530 ms9896 KiB
//----------vahagng----------//
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;

// template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

void print(long long t) { cerr << t; }
void print(int t) { cerr << t; }
void print(string t) { cerr << t; }
void print(char t) { cerr << t; }
void print(double t) { cerr << t; }
void print(long double t) { cerr << t; }
void print(unsigned long long t) { cerr << t; }

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[], V n) { cerr << "["; for (int i = 0; i < n; i++) { cerr << v[i] << " "; } cerr << "]"; }
template <class T, class V> void print(pair <T, V> p) { cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}"; }
template <class T> void print(vector <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(set <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(multiset <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T, class V> void print(map <T, V> v) { cerr << "[ "; for (auto i : v) { print(i); cerr << " "; } cerr << "]"; }

#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ld long double
#define sz(v) v.size()
#define endl '\n'

const ll inf = 2e18, mod = 1e9 + 7, mod2 = 998244353;

void SetIO(string str = "") {
    if (str != "") {
        freopen((str + ".in").c_str(), "r", stdin);
        freopen((str + ".out").c_str(), "w", stdout);
    }
    else {
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    }
}

void FastIO() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const int N = 3e5 + 10, M = 1e7 + 10;

mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());

ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) {
            res = (res * a) % mod;
        }
        b >>= 1;
        a = (a * a) % mod;
    }
    return res;
}

ll n, q, a[N];

struct segtree {
    struct Node {
        ll mx, lazy;
    };

    vector<Node>stree;
    int size = 1;

    void init() {
        while (size <= n) size <<= 1;
        stree.assign(4 * size, { 0, 0 });
        build(0, 0, size);
    }

    void build(int x, int lx, int rx) {
        if (rx - lx == 1) {
            stree[x].mx = a[lx];
            return;
        }
        int m = (lx + rx) / 2;
        build(2 * x + 1, lx, m);
        build(2 * x + 2, m, rx);
        stree[x].mx = max(stree[2 * x + 1].mx, stree[2 * x + 2].mx);
    }

    void push(int x, int lx, int rx) {
        if (rx - lx == 1) return;
        stree[2 * x + 1].lazy += stree[x].lazy;
        stree[2 * x + 2].lazy += stree[x].lazy;
        int m = (lx + rx) / 2;
        stree[2 * x + 1].mx += stree[x].lazy;
        stree[2 * x + 2].mx += stree[x].lazy;
        stree[x].lazy = 0;
    }

    void upd(int l, int r, ll v, int x, int lx, int rx) {
        push(x, lx, rx);
        if (lx >= r || rx <= l) return;
        if (lx >= l && rx <= r) {
            stree[x].mx += v;
            stree[x].lazy += v;
            return;
        }
        int m = (lx + rx) / 2;
        upd(l, r, v, 2 * x + 1, lx, m);
        upd(l, r, v, 2 * x + 2, m, rx);
        stree[x].mx = max(stree[2 * x + 1].mx, stree[2 * x + 2].mx);
    }

    ll qry(int l, int r, int x, int lx, int rx) {
        push(x, lx, rx);
        if (lx >= r || rx <= l) return 0;
        if (lx >= l && rx <= r) return stree[x].mx;
        int m = (lx + rx) / 2;
        return max(qry(l, r, 2 * x + 1, lx, m), qry(l, r, 2 * x + 2, m, rx));
    }

    void upd(int l, int r, ll v) {
        upd(l, r, v, 0, 0, size);
    }

    ll qry(int l, int r) {
        return qry(l, r, 0, 0, size);
    }
}st;

/*
5 7
1 3 2 5 2
F 2 1
C 3 6
F 2 3
C 6 8
F 2 1
F 2 2
C 3 5*/

void solve(int tc) {
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    st.init();
    while (q--) {
        char type;
        cin >> type;
        if (type == 'F') {
            int c, h;
            cin >> c >> h;
            int l = 1, r = n, ans = -1;
            while (l <= r) {
                int m = (l + r) / 2;
                if (st.qry(m, m + 1) >= h) {
                    r = m - 1;
                    ans = m;
                }
                else {
                    l = m + 1;
                }
            }
            // assert(0);
            int L = ans, R = ans + c - 1;
            if (R >= n) {
                R = n;
                st.upd(L, R + 1, 1);
            }
            else {
                if (st.qry(R, R + 1) == st.qry(R + 1, R + 2)) {
                    ll val = st.qry(R, R + 1);
                    int cnt = 0;
                    l = 1, r = n, ans = -1;
                    int PreL = -1;
                    while (l <= r) {
                        int m = (l + r) / 2;
                        if (st.qry(m, m + 1) >= val) {
                            r = m - 1;
                            ans = m;
                        }
                        else {
                            l = m + 1;
                        }
                    }
                    PreL = ans;
                    cnt = R - ans + 1;
                    l = 1, r = n, ans = -1;
                    while (l <= r) {
                        int m = (l + r) / 2;
                        if (st.qry(m, m + 1) <= val) {
                            l = m + 1;
                            ans = m;
                        }
                        else {
                            r = m - 1;
                        }
                    }
                    st.upd(L, PreL, 1);
                    st.upd(ans - cnt + 1, ans + 1, 1);
                }
                else {
                    st.upd(L, R + 1, 1);
                }
            }
        }
        else {
            int L, R;
            cin >> L >> R;
            int l = 1, r = n, ans_l = -1;
            while (l <= r) {
                int m = (l + r) / 2;
                if (st.qry(m, m + 1) >= L) {
                    ans_l = m;
                    r = m - 1;
                }
                else {
                    l = m + 1;
                }
            }
            int ans_r = -1;
            l = 1, r = n;
            while (l <= r) {
                int m = (l + r) / 2;
                if (st.qry(m, m + 1) <= R) {
                    ans_r = m;
                    l = m + 1;
                }
                else {
                    r = m - 1;
                }
            }
            // dbg(ans_l);
            // dbg(ans_r);
            if (ans_l == -1 || ans_r == -1) {
                cout << 0 << endl;
                continue;
            }
            cout << ans_r - ans_l + 1 << endl;
        }
    }
}

void precalc() {
}

int main() {
    // SetIO("teamwork");
    FastIO();
    int test_case = 1;
    // cin >> test_case;
    precalc();
    int cnt = 1;
    while (test_case--) {
        solve(cnt++);
    }
    return 0;
}

Compilation message (stderr)

grow.cpp: In function 'void SetIO(std::string)':
grow.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen((str + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen((str + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...