제출 #1341464

#제출 시각아이디문제언어결과실행 시간메모리
1341464vahagngGrowing Trees (BOI11_grow)C++20
0 / 100
473 ms9888 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(unordered_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'
#define stress_test freopen("out.txt", "r", stdin); freopen("in.txt", "w", stdout);

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);
}

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

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

__int128_t gcd(__int128_t a, __int128_t b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

__int128_t lcm(__int128_t a, __int128_t b) {
    return a / gcd(a, b) * b;
}

const int N = 1e5 + 10;

ll n, q, a[N];

struct segtree {
    struct Node {
        ll mn, lazy;

        Node() {
            mn = lazy = 0;
        }
    };

    vector<Node>stree;
    int size = 1;

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

    void build(int x, int lx, int rx) {
        if (rx - lx == 1) {
            if (1 <= lx && lx <= n) {
                stree[x].mn = a[lx];
            }
            else {
                stree[x].mn = inf;
            }
            return;
        }
        int m = (lx + rx) / 2;
        build(2 * x + 1, lx, m);
        build(2 * x + 2, m, rx);
        stree[x].mn = min(stree[2 * x + 1].mn, stree[2 * x + 2].mn);
    }

    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;
        stree[2 * x + 1].mn += stree[x].lazy;
        stree[2 * x + 2].mn += stree[x].lazy;
        stree[x].lazy = 0;
    }

    void upd(int l, int r, int v, int x, int lx, int rx) {
        push(x, lx, rx);
        if (lx >= r || rx <= l) return;
        if (lx >= l && rx <= r) {
            stree[x].mn += 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].mn = min(stree[2 * x + 1].mn, stree[2 * x + 2].mn);
    }

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

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

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

int get_first(ll h) {
    int l = 1, r = n, ans = n + 1;
    while (l <= r) {
        int m = (l + r) / 2;
        if (st.qry(m, m + 1) >= h) {
            ans = m;
            r = m - 1;
        }
        else {
            l = m + 1;
        }
    }
    return ans;
}

int get_last(ll h) {
    int l = 1, r = n, ans = -1;
    while (l <= r) {
        int m = (l + r) / 2;
        if (st.qry(m, m + 1) <= h) {
            ans = m;
            l = m + 1;
        }
        else {
            r = m - 1;
        }
    }
    return ans;
}

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 c;
        cin >> c;
        if (c == 'F') {
            int cnt;
            ll h;
            cin >> cnt >> h;
            int l = get_first(h);
            if(l > n) continue;
            int r = l + cnt - 1;
            if(r >= n){
                r = n;
                st.upd(l, r + 1, 1);
            }else{
                ll cur = st.qry(r, r + 1), nxt = st.qry(r + 1, r + 2);
                if(cur == nxt){
                    int fr = get_first(cur);
                    int ls = get_last(cur);
                    int mnac = cnt - (fr - l);
                    // dbg(l);
                    // dbg(fr - 1);
                    // dbg(ls - mnac + 1);
                    // dbg(ls);
                    st.upd(l, fr, 1);
                    st.upd(ls - mnac + 1, ls + 1, 1);
                }else{
                    st.upd(l, r + 1, 1);
                }
            }
        }else{
            ll L, R;
            cin >> L >> R;
            // cerr << "A = { ";
            // for(int i = 1; i <= n; i++){
            //     cerr << st.qry(i, i + 1) << ' ';
            // }
            // cerr << "}\n";
            int l = get_first(L);
            int r = get_last(R);
            // dbg(l);
            // dbg(r);
            if(l >= r){
                cout << 0 << endl;
            }else{
                cout << r - l + 1 << endl;
            }
        }
    }
}


void precalc() {

}

int main() {
    // SetIO("in");
    // stress_test;
    FastIO();
    int test_case = 1;
    auto start = chrono::steady_clock::now();
    // cin >> test_case;
    precalc();
    int cnt = 1;
    while (test_case--) {
        solve(cnt++);
    }
    auto end = chrono::steady_clock::now();
    std::chrono::duration<double> elapsed_seconds = end - start;
    cerr << "Execution: " << elapsed_seconds.count() << endl;
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

grow.cpp: In function 'void SetIO(std::string)':
grow.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         freopen((str + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         freopen((str + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:54:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         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...