Submission #875473

# Submission time Handle Problem Language Result Execution time Memory
875473 2023-11-19T20:07:28 Z MarwenElarbi Growing Trees (BOI11_grow) C++17
100 / 100
156 ms 9412 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 998244353;
const int nax = 1e6+5;
const int MAX_VAL = 1e6;
double PI=3.14159265359;
int arx[8]={1,1,0,-1,-1,-1, 0, 1};
int ary[8]={0,1,1, 1, 0,-1,-1,-1};
void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}
vi tab;
int segtree[nax*4][2];
int lazy[nax*4];
void conq(int v) {
    segtree[v*2+1][0] += lazy[v];
    segtree[v*2+1][1] += lazy[v];
    lazy[v*2+1] += lazy[v];
    segtree[v*2+2][0] += lazy[v];
    segtree[v*2+2][1] += lazy[v];
    lazy[v*2+2] += lazy[v];
    lazy[v] = 0;
    //cout<<"3asba"<<endl;
}
void build(int pos,int l,int r){
    if (l>r) return;
    if (l==r){
        segtree[pos][0]=tab[l];
        segtree[pos][1]=tab[l];
        return;
    }
    int mid=(r+l)/2;
    build(pos*2+1,l,mid);
    build(pos*2+2,mid+1,r);
    segtree[pos][0]=max(segtree[pos*2+1][0],segtree[pos*2+2][0]);
    segtree[pos][1]=min(segtree[pos*2+1][1],segtree[pos*2+2][1]);
    return;
}
void update(int pos,int l,int r,int left,int right){
    if (l>r||r<left||l>right) return;
    if (l==left&&right==r){
        lazy[pos]++;
        segtree[pos][0]++;
        segtree[pos][1]++;
        return;
    }else{
        conq(pos);
        int mid=(r+l)/2;
        update(pos*2+1,l,mid,left,min(mid,right));
        update(pos*2+2,mid+1,r,max(left,mid+1),right);
        segtree[pos][0]=max(segtree[pos*2+1][0],segtree[pos*2+2][0]);
        segtree[pos][1]=min(segtree[pos*2+1][1],segtree[pos*2+2][1]);
    }
}
int query(int pos,int l,int r,int index){
    if (l>r) return -1e9;
    if (l==r) return l;
    int mid=(r+l)/2;
    conq(pos);
    if (segtree[pos*2+1][0]>=index) return query(pos*2+1,l,mid,index);
    else return query(pos*2+2,mid+1,r,index);
}
int query1(int pos,int l,int r,int index){
    if (l>r) return -1e9;
    if (l==r) return l;
    int mid=(r+l)/2;
    conq(pos);
    if (segtree[pos*2+2][1]<=index) return query1(pos*2+2,mid+1,r,index);
    else return query1(pos*2+1,l,mid,index);;
}
int query2(int pos,int l,int r,int index){
    if (l>r) return -1e9;
    if (l==r) return segtree[pos][0];
    int mid=(r+l)/2;
    conq(pos);
    if (mid>=index) return query2(pos*2+1,l,mid,index);
    else return query2(pos*2+2,mid+1,r,index);
}
int main(){
    optimise;
    /*#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
    //setIO("cowland");
    int n,k;
    cin>>n>>k;
    tab.assign(n,0);
    for (int i = 0; i < n; ++i)
    {
        cin>>tab[i];
    }
    sort(tab.begin(),tab.end());
    build(0,0,n-1);
    while(k--){
        char c;
        cin>>c;
        int x,y;
        cin>>x>>y;
        if (c=='F'){
            if(y>segtree[0][0]) continue;
            int cur=query(0,0,n-1,y);
            if (cur+x-1>=n-1){
                update(0,0,n-1,cur,n-1);
                continue;
            }else{
                int z=query2(0,0,n-1,cur+x-1);
                if (z>y){
                    int h=query1(0,0,n-1,z-1);
                    //cout << "kk "<<y<<" "<<segtree[0][1]<<endl;
                    if(h>cur){
                        update(0,0,n-1,cur,h);
                        x-=(h-cur+1);
                    }else if(query2(0,0,n-1,cur)<z){
                        update(0,0,n-1,cur,h);
                        x-=(h-cur+1);
                    }
                }
                int h=query1(0,0,n-1,z);
                //cout <<h-x+1<<endl;
                update(0,0,n-1,h-x+1,h);
            }
        }else{
            int l=query(0,0,n-1,x);
            int r=query1(0,0,n-1,y);
            //cout<< l<<" "<<r<<endl;
            if (segtree[0][0]<x||segtree[0][1]>y){
                cout << 0<<endl;
                continue;
            }
            cout << r-l+1<<endl;
        }
    }
}

   

Compilation message

grow.cpp: In function 'void setIO(std::string)':
grow.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 99 ms 8272 KB Output is correct
2 Correct 130 ms 8804 KB Output is correct
3 Correct 34 ms 8788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2392 KB Output is correct
2 Correct 5 ms 2396 KB Output is correct
3 Correct 5 ms 2396 KB Output is correct
4 Correct 4 ms 2524 KB Output is correct
5 Correct 106 ms 3624 KB Output is correct
6 Correct 140 ms 3708 KB Output is correct
7 Correct 5 ms 2652 KB Output is correct
8 Correct 109 ms 3300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 3760 KB Output is correct
2 Correct 130 ms 3956 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 115 ms 3292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 3920 KB Output is correct
2 Correct 146 ms 3768 KB Output is correct
3 Correct 8 ms 2648 KB Output is correct
4 Correct 127 ms 3960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 8016 KB Output is correct
2 Correct 126 ms 8704 KB Output is correct
3 Correct 30 ms 4956 KB Output is correct
4 Correct 25 ms 8280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 8032 KB Output is correct
2 Correct 127 ms 8428 KB Output is correct
3 Correct 32 ms 8540 KB Output is correct
4 Correct 31 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 8272 KB Output is correct
2 Correct 100 ms 8432 KB Output is correct
3 Correct 33 ms 8580 KB Output is correct
4 Correct 30 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 8848 KB Output is correct
2 Correct 123 ms 8440 KB Output is correct
3 Correct 31 ms 7760 KB Output is correct
4 Correct 97 ms 8548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 8528 KB Output is correct
2 Correct 138 ms 8784 KB Output is correct
3 Correct 127 ms 9040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 9412 KB Output is correct