답안 #476464

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
476464 2021-09-27T08:30:00 Z Shin Growing Trees (BOI11_grow) C++14
100 / 100
134 ms 3276 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK "task"
#define all(x) x.begin(), x.end()

using namespace std;
const int N = 2e5 + 7;
const int MOD = 1e9 + 7; // 998244353;
const int INF = 1e9 + 7;
const long long INFLL = 1e18 + 7;

typedef long long ll;
template <class X, class Y> bool minimize(X &a, Y b) {
    if (a > b) return a = b, true;
    return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
    if (a < b) return a = b, true;
    return false;
}

struct FenwickTree {
    vector<ll> bit;
    int n;

    FenwickTree(int n = 0) {
        this->n = n;
        bit.resize(n + 7);
    }
    
    ll get(int i) {
        ll res = 0;
        for (; i > 0; i -= i & -i) res += bit[i];
        return res;
    }

    ll getRange(int l, int r) {
        if (r > l) return 0LL;
        return get(r) - get(l - 1);
    }
    
    void modify(int i, int v) {
        for (; i <= n; i += i & -i) bit[i] += v;
    }
    
    void modifyRange(int l, int r, int v) {
        if (r < l) return;
        modify(l, v);
        modify(r + 1, -v);
    }
} f;

int n, q;
int a[N];

void solve(void) {
    cin >> n >> q;
    f = FenwickTree(n);
    for (int i = 1; i <= n; i ++) cin >> a[i];
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i ++) f.modifyRange(i, i, a[i]);

    auto lowerBound = [&](int lo, int hi, int val) -> int {
        assert(lo <= hi); hi ++;
        while (lo < hi) {
            int mid = (lo + hi) >> 1;
            if (f.get(mid) >= val) hi = mid;
            else
                lo = mid + 1;
        }
        return lo;
    }; 

    while (q --) {
        char type; cin >> type;
        if (type == 'C') {
            int mn, mx; cin >> mn >> mx;
            cout << lowerBound(1, n, mx + 1) - lowerBound(1, n, mn) << '\n';
        } else {
            int number, height; cin >> number >> height;
            int l = lowerBound(1, n, height);
            int r = l + number - 1;

            if (l > n) continue;
            if (r >= n) {
                f.modifyRange(l, n, 1);
                continue;
            }
            
            int val = f.get(r);
            int ll = lowerBound(l, n, val);
            int rr = lowerBound(l, n, val + 1) - 1;
            f.modifyRange(l, ll - 1, 1);
            f.modifyRange(rr - (r - ll), rr, 1);
        }
    }
}

int main(void) {
    cin.tie(0)->sync_with_stdio(0); 
    int test = 1;
    // cin >> test;
    while (test --) {
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 1572 KB Output is correct
2 Correct 116 ms 3028 KB Output is correct
3 Correct 41 ms 2944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 3 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 56 ms 1420 KB Output is correct
6 Correct 63 ms 1592 KB Output is correct
7 Correct 5 ms 460 KB Output is correct
8 Correct 31 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 692 KB Output is correct
2 Correct 64 ms 1776 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 39 ms 1268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 788 KB Output is correct
2 Correct 77 ms 1720 KB Output is correct
3 Correct 7 ms 460 KB Output is correct
4 Correct 68 ms 1744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 1268 KB Output is correct
2 Correct 110 ms 2588 KB Output is correct
3 Correct 18 ms 892 KB Output is correct
4 Correct 36 ms 2548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 1228 KB Output is correct
2 Correct 134 ms 2572 KB Output is correct
3 Correct 56 ms 2800 KB Output is correct
4 Correct 19 ms 924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 1448 KB Output is correct
2 Correct 83 ms 2716 KB Output is correct
3 Correct 50 ms 2904 KB Output is correct
4 Correct 19 ms 884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 1488 KB Output is correct
2 Correct 111 ms 2504 KB Output is correct
3 Correct 37 ms 2216 KB Output is correct
4 Correct 68 ms 2484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 1588 KB Output is correct
2 Correct 116 ms 3044 KB Output is correct
3 Correct 115 ms 3276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 1916 KB Output is correct