답안 #362271

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
362271 2021-02-02T13:07:21 Z ngpin04 Growing Trees (BOI11_grow) C++14
100 / 100
76 ms 3436 KB
#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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 2432 KB Output is correct
2 Correct 71 ms 2796 KB Output is correct
3 Correct 45 ms 2816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 3 ms 492 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 33 ms 1516 KB Output is correct
6 Correct 44 ms 1772 KB Output is correct
7 Correct 3 ms 492 KB Output is correct
8 Correct 31 ms 1184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 1752 KB Output is correct
2 Correct 44 ms 1900 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 35 ms 1388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 2028 KB Output is correct
2 Correct 45 ms 1772 KB Output is correct
3 Correct 5 ms 492 KB Output is correct
4 Correct 43 ms 1924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 2028 KB Output is correct
2 Correct 66 ms 2468 KB Output is correct
3 Correct 17 ms 876 KB Output is correct
4 Correct 37 ms 2284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 2028 KB Output is correct
2 Correct 62 ms 2412 KB Output is correct
3 Correct 44 ms 2668 KB Output is correct
4 Correct 13 ms 876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 2156 KB Output is correct
2 Correct 54 ms 2412 KB Output is correct
3 Correct 46 ms 2668 KB Output is correct
4 Correct 13 ms 876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 2796 KB Output is correct
2 Correct 63 ms 2284 KB Output is correct
3 Correct 27 ms 1900 KB Output is correct
4 Correct 58 ms 2284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 2540 KB Output is correct
2 Correct 67 ms 2796 KB Output is correct
3 Correct 73 ms 3052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 3436 KB Output is correct