Submission #833440

# Submission time Handle Problem Language Result Execution time Memory
833440 2023-08-22T05:40:41 Z raysh07 Growing Trees (BOI11_grow) C++17
10 / 100
104 ms 7756 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

struct node{
   int mn, mx, lz; 
};

int n, q;
const int N = 1e5 + 69;
node seg[4 * N];
int a[N];

void Build(int l, int r, int pos){
    seg[pos].lz = 0;
    if (l == r){
        seg[pos].mn = a[l];
        seg[pos].mx = a[l];
        return;
    }
    
    int mid = (l + r) / 2;
    Build(l, mid, pos * 2);
    Build(mid + 1, r, pos * 2 + 1);
    
    seg[pos].mn = min(seg[pos * 2].mn, seg[pos * 2 + 1].mn);
    seg[pos].mx = max(seg[pos * 2].mx, seg[pos * 2 + 1].mx);
}

void updlz(int l, int r, int pos){
    seg[pos].mn += seg[pos].lz;
    seg[pos].mx += seg[pos].lz;
    
    if (l == r){
        seg[pos].lz = 0;
        return;
    }
    
    seg[pos * 2].lz += seg[pos].lz;
    seg[pos * 2 + 1].lz += seg[pos].lz;
    seg[pos].lz = 0;
    return;
}

int ele(int l, int r, int pos, int qp){
    if (seg[pos].lz != 0){
        updlz(l, r, pos);
    }
    
    if (l == r) return seg[pos].mx;
    
    int mid = (l + r) / 2;
    if (qp <= mid) return ele(l, mid, pos * 2, qp);
    else return ele(mid + 1, r, pos * 2 + 1, qp);
}

int fls(int l, int r, int pos, int x){
    if (seg[pos].lz != 0){
        updlz(l, r, pos);
    }
    
    if (seg[pos].mn > x) return 0;
    
    if (l == r){
        return l;
    }
    
    int mid = (l + r) / 2;
    int qp = fls(mid + 1, r, pos * 2 + 1, x);
    if (qp == 0) qp = fls(l, mid, pos * 2, x);
    
    return qp;
}

int ffg(int l, int r, int pos, int x){
    if (seg[pos].lz != 0){
        updlz(l, r, pos);
    }
    
    if (seg[pos].mx < x) return n + 1;
    
    if (l == r){
        return l;
    }
    
    int mid = (l + r) / 2;
    int qp = ffg(l, mid, pos * 2, x);
    if (qp == n + 1) qp = ffg(mid + 1, r, pos * 2 + 1, x);
    
    return qp;
}

void upd(int l, int r, int pos, int ql, int qr){
    if (seg[pos].lz != 0){
        updlz(l, r, pos);
    }
    
    if (l >= ql && r <= qr){
        seg[pos].mn++;
        seg[pos].mx++;
        if (l != r){
            seg[pos * 2].lz++;
            seg[pos * 2 + 1].lz++;
        }
    } else if (l > qr || r < ql){
        
    } else {
        int mid = (l + r) / 2;
        upd(l, mid, pos * 2, ql, qr);
        upd(mid + 1, r, pos * 2 + 1, ql, qr);
        
        seg[pos].mn = min(seg[pos * 2].mn, seg[pos * 2 + 1].mn);
        seg[pos].mx = max(seg[pos * 2].mx, seg[pos * 2 + 1].mx);
    }
}

void Solve() 
{
    cin >> n >> q;
    
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    
    sort(a + 1, a + n + 1);
    
    Build(1, n, 1);
    
    while (q--){
        char ch; cin >> ch;
        int x, y; cin >> x >> y;
        
        if (ch == 'F'){
            int l = ffg(1, n, 1, y);
            x = min(x, n - l + 1);
            
            int en = l + x - 1;
            int v = ele(1, n, 1, en);
            
            int p1 = fls(1, n, 1, v - 1);
            int p2 = ffg(1, n, 1, v + 1) - 1;
            
            upd(1, n, 1, l, p1);
            x -= (p1 - l + 1);
            
            upd(1, n, 1, p2 - x + 1, p2);
            
         //   upd(1, n, 1, l, l + x - 1);
        } else {
            int l = ffg(1, n, 1, x);
            int r = fls(1, n, 1, y);
            
            assert((r - l + 1) >= 0);
            
            cout << r - l + 1 << "\n";
        }
    }
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
   // cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 60 ms 7192 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 36 ms 980 KB Output is correct
6 Incorrect 43 ms 1104 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 35 ms 1048 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 27 ms 976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 57 ms 3988 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 7116 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 49 ms 7176 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 104 ms 7244 KB Output is correct
2 Incorrect 104 ms 7232 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 75 ms 7244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 71 ms 7756 KB Output is correct