답안 #845971

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
845971 2023-09-07T01:41:07 Z vjudge1 Growing Trees (BOI11_grow) C++
100 / 100
103 ms 7768 KB
#define COPYRIGHT CODE BY TRINH TUAN NGHIA
#include<bits/stdc++.h>
#define Boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("popcnt")
#define ll long long
#define endl "\n"
#define st first
#define nd second
#define ii pair <ll,ll>
#define iii pair <ll,ii>
#define iiii pair <ii,ii>
#define pb push_back
#define NAME "growingtrees"
#define int long long
using namespace std;
const ll N = 1e5 + 10;
ll a[N];
ll tree[4 * N];
ll lazy[4 * N];
ll n, m;
void buildTree(ll id, ll l, ll r){
    if (l == r){
        tree[id] = a[l];
        return;
    }
    ll m = (l + r) >> 1;
    buildTree(id * 2 , l, m);
    buildTree(id * 2 + 1, m + 1, r);
    tree[id] = max(tree[id * 2], tree[id * 2 + 1]);
}
void down(ll id, ll l, ll r){
    ll m = (l + r) >> 1;
    if (l < r){
        tree[id * 2] +=  lazy[id];
        tree[id * 2 + 1] +=  lazy[id];
        lazy[id * 2] += lazy[id];
        lazy[id * 2+ 1] += lazy[id];
    }
    lazy[id] = 0;
}
void update(ll id, ll l, ll r, ll u, ll v, ll val){
    if (v < l or r < u)return ;
    if (u <= l and r <= v){
        tree[id] += val;
        lazy[id] += val;
        return;
    }
    down(id , l, r);
    ll m = (l + r) >> 1;
    update(id * 2, l, m, u, v, val);
    update(id * 2 + 1, m + 1, r, u, v, val);
    tree[id] = max(tree[id * 2], tree[id * 2 + 1]);
}
ll get(ll id, ll l, ll r, ll u){
    if (u < l or u > r)return 0;
    if (l == r)return tree[id];
    ll m = (l + r) >> 1;
    down(id, l ,r );
    return max(get(id * 2, l, m,u), get(id * 2 + 1, m + 1, r, u));
}
ll tknp(ll id, ll l, ll r, ll x){
    ll m = (l +r ) >> 1;
    if (tree[id] < x)return n + 1;
    if (l == r)return l;
    down(id , l, r);
    if (tree[id * 2] >= x)return tknp(id * 2, l ,m, x);
    else return tknp(id * 2 + 1, m + 1, r,x);
}
void inp (){
    cin >> n >> m;
    for (int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    //a[n + 1] = 1e18;
}
void solve(){
    buildTree(1,1,n);
    while (m -- ){
        char type;
        ll c,h, can1, can2;
        cin >> type;
        if (type == 'F'){

            cin >> c >> h;
            ll chot1 = tknp(1,1,n,h);
            if (chot1 == n + 1)continue;
            ll chot2 = min(n,chot1 + c - 1);
            ll daubuoi = get(1,1,n,chot2);
            ll concu = tknp(1,1,n,daubuoi);
            ll concac = tknp(1,1,n,daubuoi + 1) - 1;
            update (1,1,n,chot1,concu - 1, 1);
//            ll conlai = c - (daubuoi - 1 - chot1 + 1);
            update(1,1,n, concac - chot2 + concu, concac, 1);
           // cout << conlai << " " << concac << endl;
        }
        else {
            cin >> can1 >> can2;
            ll chot1 = tknp(1,1,n,can1);
            ll chot2;
            chot2 = tknp(1,1,n,can2 + 1);
//            cout << "dcm " << endl;
//            for (int i = 1; i <= n; ++i){
//                cout << get(1,1,n,i) << " ";
//            }
//            cout << endl;

//            cout << chot1 << " " << chot2 << " " << chot2 - chot1 + 1 << endl;
            cout << chot2 - chot1 << endl;
        }
    }
//    while(m--){
//        char c;
//        int x, y;
//        cin >> c >> x >> y;
//        if(c == 'F'){
//            int tmp1 = tknp(1, 1, n, y);
//            if(tmp1 == n + 1)
//                continue;
//            int tmp2 = min(tmp1 + x - 1, n);
//            int v = get(1, 1, n, tmp2);
//            int p = tknp(1, 1, n, v);
//            int q = tknp(1, 1, n, v + 1) - 1;
//            update(1, 1, n, tmp1, p - 1, 1);
//            update(1, 1, n, q - tmp2 + p, q, 1);
//        }
//        else{
//            cout << tknp(1, 1, n, y + 1) - tknp(1, 1, n, x) << "\n";
//        }
//    }
}
signed main (){
    if (fopen(NAME".inp", "r")){
        freopen(NAME".inp", "r", stdin);
        freopen(NAME".out", "w", stdout);
    }
    Boost;
    inp();
    solve();
}
/*
input

*/

/*
output

*/

Compilation message

grow.cpp: In function 'void down(long long int, long long int, long long int)':
grow.cpp:34:8: warning: unused variable 'm' [-Wunused-variable]
   34 |     ll m = (l + r) >> 1;
      |        ^
grow.cpp: In function 'int main()':
grow.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         freopen(NAME".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |         freopen(NAME".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 7504 KB Output is correct
2 Correct 78 ms 7484 KB Output is correct
3 Correct 91 ms 7344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 28 ms 4700 KB Output is correct
6 Correct 32 ms 4944 KB Output is correct
7 Correct 4 ms 4696 KB Output is correct
8 Correct 17 ms 4696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 4952 KB Output is correct
2 Correct 34 ms 4968 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 23 ms 4952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 4952 KB Output is correct
2 Correct 35 ms 4944 KB Output is correct
3 Correct 6 ms 4440 KB Output is correct
4 Correct 33 ms 4800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 7396 KB Output is correct
2 Correct 73 ms 7768 KB Output is correct
3 Correct 12 ms 6748 KB Output is correct
4 Correct 60 ms 7472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 7504 KB Output is correct
2 Correct 69 ms 7504 KB Output is correct
3 Correct 73 ms 7256 KB Output is correct
4 Correct 10 ms 6744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 7508 KB Output is correct
2 Correct 48 ms 7504 KB Output is correct
3 Correct 73 ms 7344 KB Output is correct
4 Correct 10 ms 6744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 7580 KB Output is correct
2 Correct 70 ms 7476 KB Output is correct
3 Correct 21 ms 7512 KB Output is correct
4 Correct 49 ms 7504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 7476 KB Output is correct
2 Correct 70 ms 7480 KB Output is correct
3 Correct 103 ms 7476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 7760 KB Output is correct