QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#778430#7787. Maximum Rating4eyebirdWA 3ms5772kbC++174.6kb2024-11-24 14:35:402024-11-24 14:35:42

Judging History

你现在查看的是最新测评结果

  • [2024-11-24 14:35:42]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:5772kb
  • [2024-11-24 14:35:40]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define Buff ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
//#define int long long

int idx;
struct node
{
    int l, r;
    int ct, lzc;
    ll sm, lz;
} tr[7000000];

map<int, int> Ct;

constexpr int MX = 1000000010;

void update(int l, int r, ll x, int L = 0, int R = MX, int p = 1)
{
    if (x > 0)
        tr[p].ct += (r - l + 1);
    else if (x < 0)
        tr[p].ct -= (r - l + 1);
    tr[p].sm += x * (r - l + 1);
    if (l <= L && R <= r)
    {
        if (x > 0)
            tr[p].lzc++;
        else if (x < 0)
            tr[p].lzc--;
        tr[p].lz += x;
        return;
    }
    int mid = (L + R) >> 1;
    if (l <= mid)
    {
        if (tr[p].l == 0)
            tr[p].l = idx++;
        update(l, min(mid, r), x, L, mid, tr[p].l);
    }
    if (r > mid)
    {
        if (tr[p].r == 0)
            tr[p].r = idx++;
        update(max(l, mid + 1), r, x, mid + 1, R, tr[p].r);
    }
}

int query(int px, int L = 0, int R = MX, int p = 1)
{
    if (L == px && R == px)
        return p;
    int mid = (L + R) >> 1;
    if (tr[p].lz > 0 || tr[p].lzc != 0)
    {
        if (tr[p].l == 0)
            tr[p].l = idx++;
        if (tr[p].r == 0)
            tr[p].r = idx++;

        tr[tr[p].l].sm += tr[p].lz * (mid - L + 1);
        tr[tr[p].r].sm += tr[p].lz * (R - mid);
        tr[tr[p].l].lz += tr[p].lz;
        tr[tr[p].r].lz += tr[p].lz;
        tr[p].lz = 0;

        tr[tr[p].l].ct += tr[p].lzc * (mid - L + 1);
        tr[tr[p].r].ct += tr[p].lzc * (R - mid);
        tr[tr[p].l].lzc += tr[p].lzc;
        tr[tr[p].r].lzc += tr[p].lzc;
        tr[p].lzc = 0;
    }
    if (px <= mid)
    {
        if (tr[p].l == 0)
        {
            return -1;
        }
        return query(px, L, mid, tr[p].l);
    }
    else if (px > mid)
    {
        if (tr[p].r == 0)
        {
            return -1;
        }
        return query(px, mid + 1, R, tr[p].r);
    }
}

ll a[200010];

void solve()
{
    idx = 2;
    int n, q;
    cin >> n >> q;
    ll Sf = 0, Sz = 0, Cz = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if (a[i] < 0)
            Sf += a[i];
        else if (a[i] > 0)
        {
            Cz++;
            Sz += a[i];
            Ct[a[i]]++;
            update(a[i], MX, a[i]);
        }
    }

    while (q--)
    {
        int p;
        ll x;
        cin >> p >> x;
        if (a[p] < 0)
        {
            if (x <= 0)
            {
                Sf -= a[p];
                Sf += x;
                a[p] = x;
            }
            else
            {
                Sf -= a[p];
                a[p] = x;
                Ct[x]++;
                Cz++;
                Sz += x;
                update(x, MX, x);
            }
        }
        else
        {
            Cz--;
            Sz -= a[p];
            Ct[a[p]]--;
            update(a[p], MX, -a[p]);
            if (x <= 0)
            {
                Sf += x;
                a[p] = x;
            }
            else
            {
                a[p] = x;
                Ct[x]++;
                Cz++;
                Sz += x;
                update(x, MX, x);
            }
        }

        if (Sf == 0)
        {
            cout << "1\n";
            continue;
        }
        if (Sz + Sf <= 0)
        {
            cout << Cz + 1 << '\n';
            continue;
        }

        auto check = [&](int px) -> bool
        {
            int qy = query(px);
            if (qy == -1)
            {
                if (Sf == 0)
                    return true;
                else
                    return false;
            }
            if (Sf + tr[qy].sm >= 0)
                return true;
            else
                return false;
        };

        int l = 0, r = MX;
        while (l < r)
        {
            int mid = (l + r) >> 1;
            if (check(mid))
                r = mid;
            else
                l = mid + 1;
        }
        int qy = query(l);
        int mxc = tr[qy].ct;
        ll ca = tr[qy].sm + Sf;
        int cty = Ct[l];
        while (cty-- && ca > 0)
        {
            ca -= l;
            mxc--;
        }
        int ans = mxc + 1;
        cout << ans << '\n';
    }
}

signed main()
{
#ifdef YGYYYGJ
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    Buff;
    int _T_ = 1;
    while (_T_--)
    {
        solve();
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3692kb

input:

3 5
1 2 3
3 4
2 -2
1 -3
3 1
2 1

output:

1
2
2
2
3

result:

ok 5 number(s): "1 2 2 2 3"

Test #2:

score: 0
Accepted
time: 1ms
memory: 5696kb

input:

3 5
1 2 3
3 4
2 -2
1 3
3 1
2 1

output:

1
2
1
2
1

result:

ok 5 number(s): "1 2 1 2 1"

Test #3:

score: 0
Accepted
time: 1ms
memory: 5748kb

input:

1 1
1000000000
1 1000000000

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 1ms
memory: 5568kb

input:

1 1
-1000000000
1 -1000000000

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 3ms
memory: 5772kb

input:

1000 1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

946
65
252
410
915
592
983
487
343
899
809
432
586
587
139
464
804
84
476
699
504
149
579
352
375
856
545
166
140
657
568
509
275
710
873
430
537
879
301
1
298
970
923
510
984
642
55
879
941
344
464
788
917
994
571
610
491
442
926
101
986
840
624
613
425
345
816
423
275
221
317
113
386
116
469
260
4...

result:

ok 1000 numbers

Test #6:

score: -100
Wrong Answer
time: 1ms
memory: 5636kb

input:

1000 1000
1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000...

output:

500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
500
...

result:

wrong answer 395th numbers differ - expected: '500', found: '499'