Submission #798488

# Submission time Handle Problem Language Result Execution time Memory
798488 2023-07-30T18:31:43 Z serifefedartar Growing Trees (BOI11_grow) C++17
100 / 100
90 ms 2884 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 998244353
#define LOGN 20
#define MAXN 100005

int N, M, l, r, c, h;
int tree[MAXN];
void add(int k, int a) {
    while (k <= N) {
        tree[k] += a;
        k += k&-k;
    }
}

void update(int l, int r, int x) {
    add(r+1, -x);
    add(l, x);
}

int query(int k) {
    int res = 0;
    while (k) {
        res += tree[k];
        k -= k&-k;
    }
    return res;
}

int main() {
    fast
    cin >> N >> M;

    vector<int> arr(N);
    for (int i = 0; i < N; i++)
        cin >> arr[i];
    sort(arr.begin(), arr.end());
    for (int i = 1; i <= N; i++)
        update(i, i, arr[i-1]);

    char type;
    while (M--) {
        cin >> type;
        if (type == 'F') {
            cin >> c >> h;
            int L = 1;
            int R = N;
            int ans = -1;
            while (R >= L) {
                int mid = L + (R-L)/2;
                int q = query(mid);
                if (q >= h) {
                    R = mid - 1;
                    ans = mid;
                } else
                    L = mid + 1;
            }

            int rightEnd = ans + c - 1;
            if (ans == -1)
                continue; 
            if (rightEnd >= N) {
                update(ans, N, 1);
            } else {
                int q = query(rightEnd);
                int L = 1;
                int R = N;
                int ans2 = -1;
                while (R >= L) {
                    int mid = L + (R-L)/2;
                    int q2 = query(mid);
                    if (q2 >= q) {
                        R = mid - 1;
                        ans2 = mid; 
                    } else
                        L = mid + 1;
                }

                L = 1;
                R = N;
                int right = -1;
                while (R >= L) {
                    int mid = L + (R-L)/2;
                    int q2 = query(mid);
                    if (q2 <= q) {
                        L = mid + 1;
                        right = mid;
                    } else
                        R = mid - 1;
                }

                int len = ans2-ans;
                update(ans, ans2-1, 1);
                update(right-c+len+1, right, 1);
            }
        } else {
            cin >> l >> r;
            int L = 1;
            int R = N;
            int ans1 = -1;
            while (R >= L) {
                int mid = L + (R-L)/2;
                int q = query(mid);
                if (q >= l) {
                    R = mid - 1;
                    ans1 = mid;
                } else
                    L = mid + 1;
            }

            L = 1;
            R = N;
            int ans2 = -1;
            while (R >= L) {
                int mid = L + (R-L)/2;
                int q = query(mid);
                if (q > r) {
                    R = mid - 1;
                    ans2 = mid;
                } else
                    L = mid + 1;
            }

            if (ans1 == -1)
                cout << "0\n"; 
            else {
                if (ans2 == -1)
                    ans2 = N+1;
                cout << ans2 - ans1 << "\n";
            }
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 48 ms 1832 KB Output is correct
2 Correct 87 ms 2660 KB Output is correct
3 Correct 29 ms 2616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 35 ms 500 KB Output is correct
6 Correct 40 ms 1544 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 19 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 1236 KB Output is correct
2 Correct 44 ms 1760 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 23 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 1760 KB Output is correct
2 Correct 50 ms 1604 KB Output is correct
3 Correct 5 ms 464 KB Output is correct
4 Correct 43 ms 1752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 1536 KB Output is correct
2 Correct 80 ms 2284 KB Output is correct
3 Correct 13 ms 852 KB Output is correct
4 Correct 25 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 1984 KB Output is correct
2 Correct 78 ms 2380 KB Output is correct
3 Correct 28 ms 2540 KB Output is correct
4 Correct 13 ms 880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 1748 KB Output is correct
2 Correct 48 ms 2392 KB Output is correct
3 Correct 29 ms 2608 KB Output is correct
4 Correct 13 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 1128 KB Output is correct
2 Correct 78 ms 1740 KB Output is correct
3 Correct 22 ms 1740 KB Output is correct
4 Correct 43 ms 2208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 1992 KB Output is correct
2 Correct 86 ms 2636 KB Output is correct
3 Correct 87 ms 2884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 1512 KB Output is correct