Submission #672801

# Submission time Handle Problem Language Result Execution time Memory
672801 2022-12-18T07:46:54 Z Cookie Growing Trees (BOI11_grow) C++14
100 / 100
142 ms 7044 KB
#include<bits/stdc++.h>

#include<fstream>
using namespace std;
#define ll long long
#define vt vector
#define pb push_back
#define fi first
#define se second
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int long long
const int mxn = 1e5 + 3;
const int inf = 1e7;
int n, q;
int mx[4 * mxn + 1], lz[4 * mxn + 1], a[mxn + 1];
void push(int nd){
    mx[nd * 2] += lz[nd]; mx[nd * 2 + 1] += lz[nd]; lz[nd * 2] += lz[nd];
    lz[nd * 2  + 1] += lz[nd]; lz[nd] = 0;
}
void upd(int nd, int l, int r, int ql, int qr, int v){
    if(ql > r || qr < l)return;
    if(ql <= l && qr >= r){
        mx[nd] += v; lz[nd] += v;
        return;
    }
    int mid = (l + r) >> 1;
    push(nd);
    upd(nd * 2, l, mid, ql, qr, v); upd(nd * 2 + 1, mid + 1, r, ql, qr, v);
    mx[nd] = max(mx[nd * 2], mx[nd * 2 + 1]);
}
int getv(int nd, int l, int r, int id){
    if(l == r)return(mx[nd]);
    int mid = (l + r) >> 1;
    push(nd);
    if(id <= mid)return(getv(nd * 2, l, mid, id));
    else return(getv(nd * 2 + 1, mid + 1, r, id));
}
int findl(int nd, int l, int r, int h){
    if(l == r)return(l);
    int mid = (l + r) >> 1;
    push(nd);
    if(mx[nd * 2] >= h){
        return(findl(nd * 2, l, mid, h));
    }else{
        return(findl(nd * 2 + 1, mid + 1, r, h));
    }
}

int findr(int nd, int l, int r, int h){
    if(l == r)return(r - 1);
    int mid = (l + r) >> 1;
    push(nd);
    if(mx[nd * 2] > h){
        return(findr(nd * 2, l, mid, h));
    }else{
        return(findr(nd * 2 + 1, mid + 1, r, h));
    }
}
void add(int c, int h){
    int l = findl(1, 1, n + 1, h); 
    if(l == n + 1)return;
    int r = min(l + c - 1, n), v = getv(1, 1, n + 1, r);
    
    int rl = findl(1, 1, n + 1, v), rr = findr(1, 1, n + 1, v);
    if(l == rl){
        upd(1, 1, n + 1, max(rr - c + 1, l), rr, 1);
    }else{
    upd(1, 1, n + 1, l, rl - 1, 1); 
    int lft = rr - (c - (rl - l)) + 1; 
    //assert(lft >= rl);
    upd(1, 1, n + 1, max(lft, rl), rr, 1);
    }
    
}
signed main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> q;
    for(int i = 1; i <= n; i++)cin >> a[i];
    sort(a + 1, a + n + 1);
    for(int i = 1; i <= n; i++)upd(1, 1, n + 1, i, i, a[i]);
    forr(i, 0, q){
        char idq; cin >> idq;
        if(idq == 'F'){
            int c, h; cin >> c >> h;
            add(c, h);
            /*
            for(int j = 1; j <= n; j++)cout << getv(1, 1, n + 1, j) << " ";
            cout << "\n";
            */
            
        }else{
            int l, r; cin >> l >> r;
            cout << findr(1, 1, n + 1, r) - findl(1, 1, n + 1, l) + 1 << "\n";
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 87 ms 5288 KB Output is correct
2 Correct 115 ms 6776 KB Output is correct
3 Correct 109 ms 6684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 2 ms 448 KB Output is correct
5 Correct 37 ms 1696 KB Output is correct
6 Correct 47 ms 1860 KB Output is correct
7 Correct 5 ms 596 KB Output is correct
8 Correct 24 ms 1412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 1000 KB Output is correct
2 Correct 44 ms 2000 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 38 ms 1556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 976 KB Output is correct
2 Correct 46 ms 1868 KB Output is correct
3 Correct 9 ms 720 KB Output is correct
4 Correct 44 ms 1996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 3000 KB Output is correct
2 Correct 114 ms 6544 KB Output is correct
3 Correct 15 ms 1872 KB Output is correct
4 Correct 83 ms 6384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 5064 KB Output is correct
2 Correct 108 ms 6432 KB Output is correct
3 Correct 106 ms 6620 KB Output is correct
4 Correct 15 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 5200 KB Output is correct
2 Correct 78 ms 6380 KB Output is correct
3 Correct 114 ms 6696 KB Output is correct
4 Correct 15 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 5156 KB Output is correct
2 Correct 98 ms 6348 KB Output is correct
3 Correct 44 ms 5836 KB Output is correct
4 Correct 84 ms 6264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 5356 KB Output is correct
2 Correct 101 ms 6736 KB Output is correct
3 Correct 142 ms 7044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 5664 KB Output is correct