Submission #362271

#TimeUsernameProblemLanguageResultExecution timeMemory
362271ngpin04Growing Trees (BOI11_grow)C++14
100 / 100
76 ms3436 KiB
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int bit[N];
int a[N];
int n,m;

void update(int pos, int val)
{
    for (; pos <= n; pos += pos & -pos)
        bit[pos] += val;
}

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

int getsum(int pos)
{
    int res = 0;
    for (; pos > 0; pos -= pos & -pos)
        res += bit[pos];
    return res;
}

int getpos(int x)
{
    int cur = 0;
    int val = 0;
    for (int i = 20; i >= 0; i--) if (cur + (1 << i) <= n)
    {
        if (val + bit[cur + (1 << i)] < x)
        {
            cur += (1 << i);
            val += bit[cur];
        }
    }
    return cur + 1;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("file.inp","r",stdin);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];

    sort(a + 1, a + n + 1);

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

    for (int i = 1; i <= m; i++)
    {
        char op;
        cin >> op;
        if (op == 'C')
        {
            int l,r;
            cin >> l >> r;
            cout << getpos(r + 1) - getpos(l) << "\n";
        } else {
            int cnt, val;
            cin >> cnt >> val;
            int pos = getpos(val);
            if (pos + cnt - 1 >= n)
            {
                update(pos, n, 1);
                continue;
            }
            int las = getsum(pos + cnt - 1);
            int nxt = getpos(las) - 1;
            update(pos, nxt, 1);

            cnt -= (nxt - pos + 1);

            int laspos = getpos(las + 1) - 1;
            update(laspos - cnt + 1, laspos, 1);
        }
    }
    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...