40pts,WA,求助

P2801 教主的魔法

__Victory @ 2022-12-03 14:02:42

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e6 + 3;
int a[N], ed[N], st[N], num[N], belong[N], t[N];
void Sort(int k)
{
    for (int i = st[k]; i <= ed[k]; i++)
        t[i] = a[i];
    sort(t + st[k], t + ed[k] + 1);
}
void modify(int l, int r, int c)
{
    int x = belong[l], y = belong[r];
    if (x == y)
    {
        for (int i = l; i <= r; i++)
            a[i] += c;
        Sort(x);
        return;
    }
    for (int i = l; i <= ed[x]; i++)
        a[i] += c;
    for (int i = st[y]; i <= r; i++)
        a[i] += c;
    for (int i = x + 1; i < y; i++)
        num[i] += c;
    Sort(x), Sort(y);
}
int answer(int l, int r, int c)
{
    int ans = 0, x = belong[l], y = belong[r];
    if (x == y)
    {
        for (int i = l; i <= r; i++)
            if (a[i] + num[x] >= c)
                ans++;
        return ans;
    }
    for (int i = l; i <= ed[x]; i++)
        if (a[i] + num[x] >= c)
            ans++;
    for (int i = st[y]; i <= r; i++)
        if (a[i] + num[y] >= c)
            ans++;
    for (int i = x + 1; i <= y - 1; i++)
        ans += ed[i] - (lower_bound(t + st[i], t + ed[i] + 1, c - num[i]) - t) + 1;
    return ans;
}
void solve()
{
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int len = sqrt(n);
    for (int i = 1; i <= len; i++)
        st[i] = n / len * (i - 1) + 1, ed[i] = n / len * i;
    ed[len] = n;
    for (int i = 1; i <= len; i++)
        for (int j = st[i]; j <= ed[i]; j++)
            belong[j] = i;
    while (q--)
    {
        string s;
        int x, y, z;
        cin >> s >> x >> y >> z;
        if (s == "M")
            modify(x, y, z);
        if (s == "A")
            cout << answer(x, y, z) << endl;
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //    int t;cin >> t;while (t--)
    solve();
    return 0;
}

|