제출 #660677

#제출 시각아이디문제언어결과실행 시간메모리
660677four_specksGrowing Trees (BOI11_grow)C++17
100 / 100
113 ms3788 KiB
#include <bits/stdc++.h>

using namespace std;

inline namespace
{
    template <typename T>
    struct PURS
    {
        PURS(int _n) : n(_n), tree(n + 1, 0) {}

        void add(int p, T t)
        {
            for (p++; p <= n; p += p & -p)
                tree[p] += t;
        }

        T sum(int l, int r)
        {
            T ret = 0;
            for (; r; r -= r & -r)
                ret += tree[r];
            for (; l; l -= l & -l)
                ret -= tree[l];

            return ret;
        }

        T sum(int l = 0) { return sum(l, n); }

    private:
        int n;
        vector<int> tree;
    };

} // namespace

void solve()
{
    int n, m;
    cin >> n >> m;

    vector<long> a(n);
    for (long &x : a)
        cin >> x;
    sort(a.begin(), a.end());

    PURS<long> purs(n);

    auto add_height = [&](int l, int r, long x) -> void
    {
        purs.add(l, x);
        purs.add(r, -x);
    };

    auto get_height = [&](int p) -> long
    { return purs.sum(0, p + 1); };

    for (int i = 0; i < n; i++)
        add_height(i, i + 1, a[i]);

    for (int z = 0; z < m; z++)
    {
        char t;
        cin >> t;

        if (t == 'F')
        {
            int c;
            long h;
            cin >> c >> h;

            int p = n, lo = 0;
            while (lo < p)
            {
                int mid = (lo + p) / 2;
                if (get_height(mid) >= h)
                    p = mid;
                else
                    lo = mid + 1;
            }
            if (p + c < n)
            {
                long y = get_height(p + c - 1);

                int i = n, j = p, lo = p, hi = n;
                while (lo < i)
                {
                    int mid = (lo + i) / 2;
                    if (get_height(mid) >= y)
                        i = mid;
                    else
                        lo = mid + 1;
                }
                while (j < hi)
                {
                    int mid = (j + hi) / 2;
                    if (get_height(mid) <= y)
                        j = mid + 1;
                    else
                        hi = mid;
                }

                add_height(p, i, 1);
                add_height(j - c + i - p, j, 1);
            }
            else
                add_height(p, n, 1);
        }
        else if (t == 'C')
        {
            long x, y;
            cin >> x >> y;

            int l = n, r = 0, lo = 0, hi = n;
            while (lo < l)
            {
                int mid = (lo + l) / 2;
                if (get_height(mid) >= x)
                    l = mid;
                else
                    lo = mid + 1;
            }
            while (r < hi)
            {
                int mid = (r + hi) / 2;
                if (get_height(mid) <= y)
                    r = mid + 1;
                else
                    hi = mid;
            }

            cout << r - l << '\n';
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);

    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...