Submission #905273

# Submission time Handle Problem Language Result Execution time Memory
905273 2024-01-12T21:47:49 Z asdasdqwer Growing Trees (BOI11_grow) C++14
10 / 100
229 ms 7820 KB
#include <bits/stdc++.h>
using namespace std;

#define int int64_t

struct Segtree {
    int n;
    int nn;
    vector<int> mn;
    vector<int> mx;
    vector<int> add;

    Segtree(int sz,vector<int> &a) {
        n=1;
        nn=sz;
        while (n<sz)n*=2;
        mn.assign(2*n,1e17);
        mx.assign(2*n,0);
        add.assign(2*n,0);
        build(a,0,0,n);
    }

    void build(vector<int> &a, int x, int lx, int rx) {
        if (rx-lx==1) {
            if (lx<(int)a.size()) {
                mn[x]=mx[x]=a[lx];
            }
            return;
        }

        int m=(lx+rx)/2;
        build(a,2*x+1,lx,m);
        build(a,2*x+2,m,rx);
        mx[x]=max(mx[2*x+1],mx[2*x+2]);
        mn[x]=min(mn[2*x+1],mn[2*x+2]);
    }

    void pushdown(int x) {
        if (add[x]==0)return;

        mn[2*x+1]+=add[x];
        mx[2*x+1]+=add[x];
        mn[2*x+2]+=add[x];
        mx[2*x+2]+=add[x];
        add[2*x+1]+=add[x];
        add[2*x+2]+=add[x];
        add[x]=0;
    }

    void inc(int l, int r, int x, int lx, int rx) {
        if (lx >= r || rx <= l) return;

        if (rx-lx==1) {
            mn[x]++;
            mx[x]++;
            return;
        }

        pushdown(x);

        if (lx >= l && rx <= r) {
            add[x]++;
            mn[x]++;
            mx[x]++;
            return;
        }

        int m=(lx+rx)/2;
        inc(l, r, 2*x+1, lx, m);
        inc(l, r, 2*x+2, m, rx);

        mx[x]=max(mx[2*x+1], mx[2*x+2]);
        mn[x]=min(mn[2*x+1], mn[2*x+2]);
    }

    void inc(int l, int r) {
        inc(l, r, 0, 0, n);
    }

    int first(int v, int x, int lx, int rx) {
        if (rx-lx==1) {
            if (mn[x] < v) return -1;
            return lx;
        }

        pushdown(x);

        int m = (lx+rx)/2;

        if (mx[2*x+1] >= v) {
            return first(v, 2*x+1, lx, m);
        }

        else {
            return first(v, 2*x+2, m, rx);
        }
    }

    int first(int v) {
        return first(v, 0, 0, n);
    }

    int last(int v, int x, int lx, int rx) {
        if (rx-lx==1) {
            if (mn[x] > v) return -1;
            return lx;
        }

        pushdown(x);

        int m=(lx+rx)/2;
        
        if (mn[2*x+2] <= v) return last(v, 2*x+2, m, rx);

        else return last(v, 2*x+1, lx, m);
    }

    int last(int v) {
        return last(v, 0, 0, n);
    }

    int getValue(int i, int x, int lx, int rx) {
        if (rx-lx==1) {
            return mn[x];
        }

        pushdown(x);

        int m=(lx+rx)/2;
        if (i<m)return getValue(i,2*x+1,lx,m);
        else return getValue(i, 2*x+2,m,rx);
    }

    int getValue(int i) {
        return getValue(i, 0, 0, n);
    }

    void query(int start, int numInc) {
        int firstIndex = first(start);

        if (firstIndex == -1 || firstIndex >= nn) return;

        if (firstIndex + numInc - 1 >= nn - 1) {
            inc(firstIndex, nn);
            return;
        }

        int valueOfLast = getValue(firstIndex + numInc - 1);

        int fRange = first(valueOfLast), eRange = last(valueOfLast);
        // cout<<fRange<<" "<<eRange<<"\n";

        int end = firstIndex + numInc - 1;

        if (firstIndex < fRange) {
            inc(firstIndex, fRange);
            numInc -= (fRange - firstIndex);
        }

        inc(eRange - numInc + 1, eRange + 1);
    }

    void all() {
        for (int i=0;i<nn;i++) {
            cout<<getValue(i)<<" ";
        }
        cout<<"\n";
    }
};

signed main() {
    int n,q;cin>>n>>q;
    vector<int> a(n);
    for (int &x:a)cin>>x;
    sort(a.begin(),a.end());

    Segtree sg(n, a);

    for (int i=0;i<q;i++) {
        char d;cin>>d;
        if (d == 'F') {
            int c,h;cin>>c>>h;
            sg.query(h,c);
        }

        else{ 
            int mn,mx;cin>>mn>>mx;
            int f=sg.first(mn);
            if (f==-1 || f >= n){
                cout<<0<<"\n";
                continue;
            }

            int s=sg.last(mx);
            if (s==-1 || s >= n)s=n-1;
            cout<<s-f+1<<"\n";
        }

        // sg.all();
    }
}

Compilation message

grow.cpp: In member function 'void Segtree::query(int64_t, int64_t)':
grow.cpp:153:13: warning: unused variable 'end' [-Wunused-variable]
  153 |         int end = firstIndex + numInc - 1;
      |             ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 147 ms 7504 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 344 KB Output is correct
2 Incorrect 6 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 166 ms 1172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 171 ms 1220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 115 ms 4156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 132 ms 7236 KB Output is correct
2 Incorrect 177 ms 7248 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 143 ms 7420 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 188 ms 7264 KB Output is correct
2 Incorrect 185 ms 7324 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 172 ms 7376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 229 ms 7820 KB Output is correct