답안 #833437

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
833437 2023-08-22T05:37:55 Z raysh07 Growing Trees (BOI11_grow) C++17
10 / 100
107 ms 9560 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);
        } else {
            int l = ffg(1, n, 1, x);
            int r = fls(1, n, 1, y);
            
            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;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 60 ms 8384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 472 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 37 ms 1768 KB Output is correct
6 Incorrect 44 ms 2036 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 37 ms 1876 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 28 ms 2000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 59 ms 4940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 105 ms 8192 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 49 ms 8148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 8908 KB Output is correct
2 Incorrect 97 ms 8392 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 73 ms 8524 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 9560 KB Output is correct