Submission #1027149

# Submission time Handle Problem Language Result Execution time Memory
1027149 2024-07-19T00:16:31 Z a5a7 Growing Trees (BOI11_grow) C++14
100 / 100
528 ms 6868 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using indexedset = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;

struct Node{
    ll value = 0;
    ll add = 0;
    Node() {};
};
 
vector<Node> arr;
int length, arrl;
 
void build(int n){
    arr.clear();
    int powerOf2 = 1;
    while (powerOf2 < n) powerOf2 *= 2;
    n = powerOf2;
    length = 2 * n - 1, arrl = n;
    for (int i = 0; i < length; i++) arr.push_back(Node());
}
 
void setval(int i, ll val){
    int child = length-arrl+i;
    arr[child].value = val;
    while (child != 0){
        if (child%2 == 0) child--;
        arr[(child-1)/2].value = arr[child].value + arr[child+1].value;
        child = (child-1)/2;
    }
}

void add(int left, int right, int start, int end, int node, ll ad){
    if (left > end || right < start) return;
    if (left <= start && end <= right){
        arr[node].add += ad;
        return;
    }
    int mid = (start+end)/2;
    arr[node].value += ll(min(end, right) - max(start, left) + 1) * ad;
    add(left, right, start, mid, node*2+1, ad);
    add(left, right, mid+1, end, node*2+2, ad);
}
 
ll range(int left, int right, int start, int end, int node){
    if (left > end || right < start) return 0;
    if (left <= start && end <= right) return arr[node].value + arr[node].add * ll(end-start+1);
    int mid = (start+end)/2;
    arr[node].value += arr[node].add * ll(end-start+1);
    if ((node*2+2) < length){
        arr[node*2+1].add += arr[node].add;
        arr[node*2+2].add += arr[node].add;
    }
    arr[node].add = 0;
    return range(left, right, start, mid, node*2+1) + range(left, right, mid+1, end, node*2+2);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    int a[n];
    build(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a+n);
    for (int i = 0; i < n; i++) setval(i, a[i]);
    function<int(int)> early = [&](int x){
        int left = 0, right = n;
        while (left < right){
            int mid = (left+right)/2;
            if (range(mid, mid, 0, arrl-1, 0) >= x){
                right = mid;
            }else{
                left = mid + 1;
            }
        }
        return left;
    };
    function<int(int)> later = [&](int x){
        return early(x+1)-1;
    };
    for (int i = 0; i < m; i++){
        char c; int u, v; 
        cin >> c >> u >> v;
        if (c == 'C'){
            cout << (later(v)-early(u)+1) << endl;
        }else{
            int index = early(v);
            if (index == n) continue;
            if ((index+u) >= n){
                add(index, n-1, 0, arrl-1, 0, 1ll);
            }else{
                int val = range(index+u-1, index+u-1, 0, arrl-1, 0);
                int ear = early(val);
                int lat = later(val);
                int taken = u-(ear-index);
                add(index, ear-1, 0, arrl-1, 0, 1ll);
                add(lat-taken+1, lat, 0, arrl-1, 0, 1ll);
            }
        }
    }
} 
# Verdict Execution time Memory Grader output
1 Correct 329 ms 5840 KB Output is correct
2 Correct 435 ms 6556 KB Output is correct
3 Correct 188 ms 6592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 9 ms 588 KB Output is correct
3 Correct 9 ms 604 KB Output is correct
4 Correct 6 ms 612 KB Output is correct
5 Correct 239 ms 1868 KB Output is correct
6 Correct 309 ms 2008 KB Output is correct
7 Correct 14 ms 860 KB Output is correct
8 Correct 230 ms 1656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 324 ms 980 KB Output is correct
2 Correct 302 ms 2048 KB Output is correct
3 Correct 4 ms 860 KB Output is correct
4 Correct 240 ms 1772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 1168 KB Output is correct
2 Correct 310 ms 2008 KB Output is correct
3 Correct 24 ms 992 KB Output is correct
4 Correct 278 ms 2124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 287 ms 3016 KB Output is correct
2 Correct 451 ms 6336 KB Output is correct
3 Correct 75 ms 2008 KB Output is correct
4 Correct 148 ms 6340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 399 ms 6868 KB Output is correct
2 Correct 427 ms 5840 KB Output is correct
3 Correct 196 ms 6848 KB Output is correct
4 Correct 76 ms 2044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 364 ms 5328 KB Output is correct
2 Correct 325 ms 6868 KB Output is correct
3 Correct 175 ms 6652 KB Output is correct
4 Correct 71 ms 2008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 463 ms 5588 KB Output is correct
2 Correct 460 ms 6868 KB Output is correct
3 Correct 71 ms 5844 KB Output is correct
4 Correct 396 ms 6080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 483 ms 6868 KB Output is correct
2 Correct 436 ms 6592 KB Output is correct
3 Correct 528 ms 6848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 455 ms 6868 KB Output is correct