제출 #1287663

#제출 시각아이디문제언어결과실행 시간메모리
1287663HiepVu217Growing Trees (BOI11_grow)C++17
0 / 100
1096 ms2948 KiB
//Proud of You//

#include <bits/stdc++.h>
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
const int N = 1e5 + 17;
int n, m, a[N], st[4 * N], lz[4 * N];

inline void build (int id, int l, int r)
{
    if (l == r)
    {
        st[id] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build (id * 2, l, mid);
    build (id * 2 + 1, mid + 1, r);
    st[id] = max (st[id * 2], st[id * 2 + 1]);
}

inline void down (int id)
{
    if (lz[id] == 0) return;
    st[id * 2] += lz[id];
    lz[id * 2] += lz[id];
    st[id * 2 + 1] += lz[id];
    lz[id * 2 + 1] += lz[id];
    lz[id] = 0;
}

inline void update (int id, int l, int r, int u, int v)
{
    if (v < l || r < u)
    {
        return;
    }
    if (u <= l && r <= v)
    {
        ++lz[id];
        ++st[id];
        return;
    }
    int mid = (l + r) >> 1;
    down(id);
    update (id * 2, l, mid, u, v);
    update (id * 2 + 1, mid + 1, r, u, v);
    st[id] = max (st[id * 2], st[id * 2 + 1]);
}

inline int get (int id, int l, int r, int u)
{
    if (u < l || u > r)
    {
        return 0;
    }
    if (l == r)
    {
        return st[id];
    }
    int mid = (l + r) >> 1;
    down(id);
    if (u <= mid) return get (id * 2, l, mid, u);
    return get (id * 2 + 1, mid + 1, r, u);
}

inline int walk (int id, int l, int r, int z)
{
    if (st[id] < z)
    {
        return n + 1;
    }
    if (l == r)
    {
        return l;
    }
    int mid = (l + r) >> 1;
    down(id);
    if (st[id * 2] >= z) return walk (id * 2, l, mid, z);
    return walk (id * 2 + 1, mid + 1, r, z);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    sort (a + 1, a + n + 1);
    build (1, 1, n);

    while (m--)
    {
        char t;
        int x, y;
        cin >> t >> x >> y;
        if (t == 'F')
        {
            int i = walk (1, 1, n, y);
            if (i == n + 1) continue;
            int need = x;

            while (need > 0 && i <= n)
            {
                int val = get (1, 1, n, i);
                int j = walk (1, 1, n, val + 1);
                if (j == n + 1) j = n + 1;
                int group_size = (j == n + 1 ? n + 1 - i : j - i);
                if (group_size <= 0) break;
                int take = min (group_size, need);
                update (1, 1, n, i, i + take - 1);
                need -= take;
                i += take;
            }
        }
        else
        {
            int l = walk (1, 1, n, x);
            int r = walk (1, 1, n, y + 1);
            int ans = r - l;
            if (ans < 0) ans = 0;
            if (l == n + 1) ans = 0;
            cout << ans << "\n";
        }
    }

    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...