답안 #630211

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
630211 2022-08-16T04:05:20 Z Soul234 Growing Trees (BOI11_grow) C++14
100 / 100
138 ms 5812 KB
#include<bits/stdc++.h>
using namespace std;

void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h; if(sizeof...(t)) cerr << ", ";
    DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL

#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

void setIO(string NAME = "") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(NAME)) {
        freopen((NAME + ".inp").c_str(),"r",stdin);
        freopen((NAME + ".out").c_str(),"w",stdout);
    }
}

tcT> bool ckmin(T&a, const T&b) {
    return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
    return b > a ? a=b,1 : 0; }

const int MOD = 1e9 + 7;

const int MX = 1e5+5;
int A[MX];

struct SegTree {
    int N; vi mx, lazy;
    const int ID = 0;
    int comb(int a, int b) { return max(a,b); }
    void build(int ind, int L, int R) {
        if(L == R) mx[ind] = A[L];
        else {
            int mid = (L + R) >> 1;
            build(ind<<1, L, mid);
            build(ind<<1|1, mid+1, R);
            mx[ind] = comb(mx[ind<<1], mx[ind<<1|1]);
        }
    }
    void init(int _N = 0) {
        N = _N;
        mx.assign(N*4 + 7, ID);
        lazy.assign(N*4 + 7, ID);
        build(1, 1, N);
    }

    void push(int ind) {
        if(lazy[ind] == 0) return;
        F0R(i, 2) {
            mx[ind*2 + i] += lazy[ind];
            lazy[ind*2 + i] += lazy[ind];
        }
        lazy[ind] = 0;
    }

    int getFst(int val, int ind, int L, int R) {
        if(L == R) return L;
        push(ind);
        int mid = (L + R) >> 1;
        if(mx[ind << 1] > val) return getFst(val, ind<<1, L, mid);
        else return getFst(val, ind<<1|1, mid+1, R);
    }
    int getFst(int val) {
        if(mx[1] <= val) return N+1;
        return getFst(val, 1, 1, N);
    }
    int getVal(int p, int ind, int L, int R) {
        if(L == R) return mx[ind];
        int mid = (L + R) >> 1;
        push(ind);
        if(p <= mid) return getVal(p, ind<<1, L, mid);
        return getVal(p, ind<<1|1, mid+1, R);
    }
    int getVal(int p) {
        return getVal(p, 1, 1, N);
    }

    void updInc(int l, int r, int ind, int L, int R) {
        if(l > R || r < L || L > R || l > r) return;
        if(l <= L && R <= r) {
            mx[ind]++;
            lazy[ind]++;
            return;
        }
        int mid = (L + R) >> 1;
        push(ind);
        updInc(l, r, ind<<1, L, mid);
        updInc(l, r, ind<<1|1, mid+1, R);
        mx[ind] = comb(mx[ind<<1], mx[ind<<1|1]);
    }
    void updInc(int l, int r) {
        if(l > r) return;
        updInc(l, r, 1, 1, N);
    }
};

int N, M;
SegTree st;

void solve() {
    cin >> N >> M;
    FOR(i,1,N+1) cin >> A[i];
    sort(A+1, A+N+1);
    st.init(N);
    while(M-- > 0) {
        char t;
        cin >> t;
        if(t == 'F') {
            int amt, mnH;
            cin >> amt >> mnH;
            int l = st.getFst(mnH-1);
            if(l == N+1) continue;
            int r = min(N, l + amt - 1);
            int val_at_r = st.getVal(r);
            int _l = st.getFst(val_at_r-1);
            st.updInc(l, _l-1);

            amt = r-l+1;

            amt -= (_l - l);

            int _r = st.getFst(val_at_r);
            st.updInc(_r - amt, _r-1);
        }
        else {
            int mnH, mxH;
            cin >> mnH >> mxH;
            cout << st.getFst(mxH) - st.getFst(mnH-1) << nl;
        }
    }
}

int main() {
    setIO();

    int t=1;
    //cin>>t;
    while(t-->0) {
        solve();
    }

    return 0;
}

Compilation message

grow.cpp: In function 'void setIO(std::string)':
grow.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen((NAME + ".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen((NAME + ".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 3640 KB Output is correct
2 Correct 99 ms 5068 KB Output is correct
3 Correct 95 ms 4864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 34 ms 1544 KB Output is correct
6 Correct 44 ms 1740 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 27 ms 1228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 896 KB Output is correct
2 Correct 45 ms 1872 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 24 ms 1464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 888 KB Output is correct
2 Correct 44 ms 1868 KB Output is correct
3 Correct 9 ms 596 KB Output is correct
4 Correct 41 ms 1944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 2736 KB Output is correct
2 Correct 89 ms 4280 KB Output is correct
3 Correct 15 ms 1284 KB Output is correct
4 Correct 97 ms 4172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 3148 KB Output is correct
2 Correct 90 ms 4468 KB Output is correct
3 Correct 96 ms 4416 KB Output is correct
4 Correct 12 ms 1316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 3236 KB Output is correct
2 Correct 62 ms 4772 KB Output is correct
3 Correct 98 ms 4800 KB Output is correct
4 Correct 12 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 3468 KB Output is correct
2 Correct 94 ms 4512 KB Output is correct
3 Correct 26 ms 4556 KB Output is correct
4 Correct 48 ms 4664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 3588 KB Output is correct
2 Correct 88 ms 5072 KB Output is correct
3 Correct 138 ms 5812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 4300 KB Output is correct