답안 #650096

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
650096 2022-10-12T11:44:12 Z MEGalodon Growing Trees (BOI11_grow) C++14
0 / 100
97 ms 4360 KB
#include <bits/stdc++.h>
#define MAXN 100010
using namespace std;
int height[MAXN];
class SegmentTree{
private:
    int n;
    vector<int> st, marked;

    int left(int p){ return 2*p; }
    int right(int p){ return 2*p+1; }
    void build(int p, int L, int R){
        if( L == R ){ st[p] = height[L]; return; }
        int mid = (L+R)/2;
        build(left(p), L, mid); build(right(p), mid+1, R);
        st[p] = max(st[left(p)], st[right(p)]); //make sure this is right
    }
    void push(int p, int L, int R){
        if( L == R ) return;
        int k = marked[p];
        marked[left(p)] += k; marked[right(p)] += k; marked[p] = 0;
        st[left(p)] += k; st[right(p)] += k;
    }
    void update(int p, int L, int R, int i, int j){
        if( i > R || j < L ) return;
        if( L >= i && R <= j ){
            marked[p] += 1;
            st[p] += 1;
            return;
        }
        int mid = (L+R)/2;
        if( marked[p] ) push(p, L, R);
        update(left(p), L, mid, i, j); update(right(p), mid+1, R, i, j);
        st[p] = max(st[left(p)], st[right(p)]);
    }
    int query(int p, int L, int R, int h){
        if( st[p] < h ) return 0;
        if( L == R ) return L;
        int mid = (L+R)/2;
        if( marked[p] ) push(p, L, R);
        if( st[left(p)] >= h ) return query(left(p), L, mid, h);
        return query(right(p), mid+1, R, h);
    }

public:
    SegmentTree(int _n) : n(_n){
        marked.assign(4*n, 0);
        st.resize(4*n);
        build(1, 1, n);
    }
    void update(int i, int j){ update(1, 1, n, i, j); }
    int query(int h){ return query(1, 1, n, h); }
};
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int trees, queries;
    cin>>trees>>queries;
    for( int i{1} ; i <= trees ; ++i ) cin>>height[i];
    sort(height+1, height+1+trees);
    height[trees+1] = INT_MAX;
    SegmentTree st(trees+1);
    while( queries-- ){
        char type; cin>>type;
        if( type == 'F' ){
            int c, h;
            cin>>c>>h;
            int p1 = st.query(h);
            if( p1 == trees+1 ) continue;
            int p2 = min(trees, p1+c-1);
            if( height[p2] < height[p2+1] ) st.update(p1, p2);
            else{
                int p3 = st.query(height[p2])-1;
                if( p1 <= p3 ){ st.update(p1, p3); int l = p3 - p1 + 1; c -= l; }
                int p4 = st.query(height[p2]+1);
                p3 = p4-c;
                st.update(p3, p4-1);
            }
        }
        else{
            int l, r;
            cin>>l>>r;
            int p1 = st.query(l);
            int p2 = st.query(r+1);
            cout<<p2-p1<<'\n';
        }
        //any edge cases you are missing?
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 3572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 28 ms 848 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 28 ms 852 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 32 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 56 ms 3152 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 37 ms 3292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 97 ms 3596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 46 ms 3504 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 78 ms 4360 KB Output isn't correct
2 Halted 0 ms 0 KB -