Submission #501410

# Submission time Handle Problem Language Result Execution time Memory
501410 2022-01-03T06:24:32 Z Jomnoi Growing Trees (BOI11_grow) C++17
100 / 100
107 ms 2860 KB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int N = 1e5 + 10;

int n;
int h[N];
int fenwick[N];

void update(int idx, int val) {
    for(; idx < N; idx += (idx & -idx)) {
        fenwick[idx] += val;
    }
}

int query(int idx) {
    int res = 0;
    for(; idx > 0; idx -= (idx & -idx)) {
        res += fenwick[idx];
    }
    return res;
}

int lowerbound(int x) {
    int l = 1, r = n, low = n + 1;
    while(l <= r) {
        int mid = (l + r) / 2;

        if(h[mid] + query(mid) < x) {
            l = mid + 1;
        }
        else {
            r = mid - 1;
            low = mid;
        }
    }
    return low;
}

int upperbound(int x) {
    int l = 1, r = n, up = 0;
    while(l <= r) {
        int mid = (l + r) / 2;

        if(h[mid] + query(mid) > x) {
            r = mid - 1;
        }
        else {
            l = mid + 1;
            up = mid;
        }
    }
    return up;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        cin >> h[i];
    }
    sort(h + 1, h + n + 1);

    while(m--) {
        char t;
        int a, b;
        cin >> t >> a >> b;
        if(t == 'F') {
            int idx = lowerbound(b);
            if(idx == n + 1) {
                continue;
            }

            update(idx, 1);
            if(idx + a - 1 >= n) {
                update(n + 1, -1);
                continue;
            }

            int last1 = h[idx + a - 1] + query(idx + a - 1);
            int last2 = h[idx + a] + query(idx + a);
            if(last1 == last2) {
                int idx2 = lowerbound(last1);
                int idx3 = upperbound(last1);
                update(idx2, -1);
                a -= idx2 - idx;
                update(idx3 - a + 1, 1);
                update(idx3 + 1, -1);
            }
            else {
                update(idx + a, -1);
            }
        }
        else {
            cout << max(0, upperbound(b) - lowerbound(a) + 1) << '\n';
        }

        if(DEBUG) {
            cout << "Debugging : \n";
            for(int i = 1; i <= n; i++) {
                cout << h[i] + query(i) << " ";
            }
            cout << '\n';
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 59 ms 1860 KB Output is correct
2 Correct 85 ms 2756 KB Output is correct
3 Correct 35 ms 2624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 396 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 45 ms 1364 KB Output is correct
6 Correct 62 ms 1676 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
8 Correct 26 ms 1172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 1448 KB Output is correct
2 Correct 54 ms 1732 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 32 ms 1216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 1740 KB Output is correct
2 Correct 60 ms 1700 KB Output is correct
3 Correct 5 ms 460 KB Output is correct
4 Correct 52 ms 1784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 1372 KB Output is correct
2 Correct 107 ms 2356 KB Output is correct
3 Correct 16 ms 836 KB Output is correct
4 Correct 30 ms 2224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 1476 KB Output is correct
2 Correct 95 ms 2432 KB Output is correct
3 Correct 35 ms 2504 KB Output is correct
4 Correct 16 ms 808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 1716 KB Output is correct
2 Correct 63 ms 2372 KB Output is correct
3 Correct 60 ms 2628 KB Output is correct
4 Correct 15 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 1924 KB Output is correct
2 Correct 93 ms 2220 KB Output is correct
3 Correct 22 ms 1744 KB Output is correct
4 Correct 60 ms 2120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 1976 KB Output is correct
2 Correct 91 ms 2616 KB Output is correct
3 Correct 82 ms 2860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 2576 KB Output is correct