70 pts 求调,疑似错在边界
查看原帖
70 pts 求调,疑似错在边界
766938
BpbjsGreen楼主2024/10/4 11:33
// ID: @BpbjsGreen

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2e5 + 5, B = 1e3 + 3;
int n = 2e5, now, m, a[N], v[N];
int sq, st[B], ed[B], bel[N];
int cnt[B], sum[B];

int Q(int x, int y)
{
    int res = 0;
    for (int i = x; i <= min(y, bel[x] * sq); i++)
        res += a[i];
    if (bel[x] != bel[y])
        for (int i = (bel[y] - 1) * sq + 1; i <= y; i++)
            res += a[i];
    for (int i = bel[x] + 1; i <= bel[y] - 1; i++)
        res += sum[i];
    return res;
}
int D(int x)
{
    int res = 0, rt = 1;
    while (res + cnt[rt] < x)
    {
        res += cnt[rt];
        rt++;
    }
    for (int i = (rt - 1) * sq + 1; i <= min(n, rt * sq); i++)
    {
        if (v[i])
            res++;
        if (res == x)
            return i;
    }
    return 0;
}

signed main()
{
    // freopen("I.in", "r", stdin);
    // freopen("O.out", "w", stdout);

    scanf("%lld%lld", &now, &m);

    sq = sqrt(n);
    for (int i = 1; i <= sq; i++)
    {
        st[i] = n / sq * (i - 1) + 1;
        ed[i] = n / sq * i;
    }
    ed[sq] = n;
    for (int i = 1; i <= sq; i++)
        for (int j = st[i]; j <= ed[i]; j++)
            bel[j] = i;

    for (int i = 1; i <= now; i++)
    {
        scanf("%lld", &a[i]);
        cnt[bel[i]]++;
        sum[bel[i]] += a[i];
        v[i] = 1;
    }

    for (int i = 1; i <= m; i++)
    {
        char opt;
        cin >> opt;
        if (opt == 'Q')
            printf("%lld\n", Q(1, n));
        if (opt == 'C')
        {
            int x, y;
            scanf("%lld%lld", &x, &y);
            if (v[x])
                a[x] -= y, sum[bel[x]] -= y;
        }
        if (opt == 'D')
        {
            int x;
            scanf("%lld", &x);
            int c = D(x);
            sum[bel[c]] -= a[c];
            cnt[bel[c]]--;
            a[c] = 0;
            v[c] = 0;
        }
        if (opt == 'I')
        {
            int x, y;
            scanf("%lld%lld", &x, &y);
            sum[bel[x]] -= a[x];
            a[x] = y;
            sum[bel[x]] += a[x];
            if (!v[x])
                cnt[bel[x]]++, v[x] = 1;
            now = max(now, x);
        }

        // for (int i = 1; i <= now; i++)
        //     printf("%lld ", a[i]);
        // putchar('\n');
    }
    return 0;
}
2024/10/4 11:33
加载中...