Submission #638212

# Submission time Handle Problem Language Result Execution time Memory
638212 2022-09-04T21:52:07 Z finn__ Growing Trees (BOI11_grow) C++17
100 / 100
262 ms 3916 KB
#include <bits/stdc++.h>
using namespace std;

struct fenwick_tree
{
    vector<long> tree;
    size_t l;

    fenwick_tree(size_t n)
    {
        l = n;
        tree = vector<long>(1 << (32 - __builtin_clz(n)), 0);
    }

    void update(size_t i, long x)
    {
        i++;
        while (i <= tree.size())
        {
            tree[i - 1] += x;
            i += i & -i;
        }
    }

    long prefix_sum(size_t i)
    {
        i++;
        long x = 0;
        while (i)
        {
            x += tree[i - 1];
            i -= i & -i;
        }
        return x;
    }
};

size_t ftree_lower_bound(fenwick_tree &t, long x)
{
    size_t a = 0, b = t.l;

    while (a < b)
    {
        size_t mid = (a + b) / 2;
        if (t.prefix_sum(mid) < x)
            a = mid + 1;
        else
            b = mid;
    }

    return a;
}

size_t ftree_upper_bound(fenwick_tree &t, long x)
{
    size_t a = 0, b = t.l;

    while (a < b)
    {
        size_t mid = (a + b) / 2;
        if (t.prefix_sum(mid) <= x)
            a = mid + 1;
        else
            b = mid;
    }

    return a;
}

int main()
{
    size_t n, m;
    cin >> n >> m;

    vector<long> seq(n);
    for (long &x : seq)
        cin >> x;

    sort(seq.begin(), seq.end());
    fenwick_tree tree(n);
    tree.update(0, seq[0]);
    for (size_t i = 1; i < n; i++)
        tree.update(i, seq[i] - seq[i - 1]);

    for (size_t i = 0; i < m; i++)
    {
        char t;
        cin >> t;

        switch (t)
        {
        case 'F':
        {
            long c, h;
            cin >> c >> h;
            size_t l = ftree_lower_bound(tree, h);
            size_t r = min(l + c - 1, tree.l - 1);
            if (r == tree.l - 1)
            {
                tree.update(l, 1);
            }
            else
            {
                size_t y = ftree_upper_bound(tree, tree.prefix_sum(r));
                size_t x = ftree_lower_bound(tree, tree.prefix_sum(r)) - 1;
                tree.update(y - (r - x), 1);
                if (y < tree.l)
                    tree.update(y, -1);
                tree.update(l, 1);
                tree.update(x + 1, -1);
            }
            break;
        }
        case 'C':
        {
            long hmin, hmax;
            cin >> hmin >> hmax;

            size_t l = ftree_lower_bound(tree, hmin);
            size_t r = ftree_upper_bound(tree, hmax);

            cout << (r - l) << '\n';
            break;
        }
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 145 ms 2124 KB Output is correct
2 Correct 204 ms 3672 KB Output is correct
3 Correct 100 ms 3664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 212 KB Output is correct
2 Correct 6 ms 340 KB Output is correct
3 Correct 8 ms 340 KB Output is correct
4 Correct 5 ms 340 KB Output is correct
5 Correct 145 ms 1412 KB Output is correct
6 Correct 201 ms 1684 KB Output is correct
7 Correct 8 ms 468 KB Output is correct
8 Correct 137 ms 1092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 184 ms 764 KB Output is correct
2 Correct 192 ms 1764 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 155 ms 1320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 210 ms 708 KB Output is correct
2 Correct 214 ms 1740 KB Output is correct
3 Correct 10 ms 468 KB Output is correct
4 Correct 180 ms 1800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 148 ms 1408 KB Output is correct
2 Correct 183 ms 3284 KB Output is correct
3 Correct 60 ms 1100 KB Output is correct
4 Correct 78 ms 3272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 140 ms 1932 KB Output is correct
2 Correct 199 ms 3360 KB Output is correct
3 Correct 98 ms 3520 KB Output is correct
4 Correct 46 ms 1080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 2108 KB Output is correct
2 Correct 146 ms 3280 KB Output is correct
3 Correct 102 ms 3592 KB Output is correct
4 Correct 61 ms 1072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 2148 KB Output is correct
2 Correct 184 ms 3288 KB Output is correct
3 Correct 61 ms 2724 KB Output is correct
4 Correct 158 ms 3300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 191 ms 2260 KB Output is correct
2 Correct 205 ms 3660 KB Output is correct
3 Correct 161 ms 3916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 2660 KB Output is correct