QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#608383#5278. Mex and CardsYshanqianTL 22ms76952kbC++204.1kb2024-10-03 21:20:382024-10-03 21:20:38

Judging History

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

  • [2024-10-03 21:20:38]
  • 评测
  • 测评结果:TL
  • 用时:22ms
  • 内存:76952kb
  • [2024-10-03 21:20:38]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define xx first
#define yy second
#define endl "\n"
#define pb push_back
#define int long long
#define ll long long
#define lowbit(x) x & (-x)
typedef pair<int, int> pii;
#define LF(x) fixed << setprecision(x)
#define Yshanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 1e6 + 10, M = 1010, inf = 0x3f3f3f3f, mod = 1e9 + 7, P = 13331;
int n;
int a[N], b[N], cnt[N];
multiset<int> st;
struct node
{
    int l, r;
    int mn, lazy;
} tr[N << 2];
void pushup(int u)
{
    tr[u].mn = min(tr[u << 1].mn, tr[u << 1 | 1].mn);
}
void build(int u, int l, int r)
{
    tr[u] = {l, r, 0, 0};
    if (l == r)
        return;
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
}
void change(int u, int x)
{
    tr[u].mn += x;
    tr[u].lazy += x;
}
void pushdown(int u)
{
    if (tr[u].lazy)
    {
        change(u << 1, tr[u].lazy);
        change(u << 1 | 1, tr[u].lazy);
        tr[u].lazy = 0;
    }
}
void modify(int u, int l, int r, int x)
{
    if (tr[u].l >= l && tr[u].r <= r)
    {
        change(u, x);
        return;
    }
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    if (l <= mid)
        modify(u << 1, l, r, x);
    if (r > mid)
        modify(u << 1 | 1, l, r, x);
    pushup(u);
}
int ask(int u, int l, int r)
{
    if (tr[u].l >= l && tr[u].r <= r)
        return tr[u].mn;
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    int ans = inf;
    if (l <= mid)
        ans = min(ans, ask(u << 1, l, r));
    if (r > mid)
        ans = min(ans, ask(u << 1 | 1, l, r));
    return ans;
}
void solve()
{
    int ans = 0;
    cin >> n;
    build(1, 1, 1e6);
    int mn = inf;
    b[0] = inf;
    int lst = -1;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        b[i] = min(b[i - 1], a[i]);
        if (b[i] != 0)
            lst = i;
    }
    int s = 0;
    int pre = 0;
    for (int i = lst; i >= 1; i--)
    {
        if (b[i] != pre)
        {
            for (int j = 1; j <= b[i] - s; j++)
                st.insert(i), ans += i, cnt[i]++;
            pre = b[i], s += b[i] - s;
        }

        for (int j = 1; j <= a[i] - pre; j++)
            modify(1, i, i, 1);

        // cout << s << "----\n";
    }

    for (int i = lst + 1; i <= n; i++)
    {
        if (a[i])
            modify(1, i, i, a[i]);
    }

    cout << ans << endl;

    st.insert(0);
    int q;
    cin >> q;
    while (q--)
    {
        int op, x;
        cin >> op >> x;
        x++;

        if (op == 1) // 加一个数
        {
            if (st.find(x - 1) != st.end())
            {
                int l = 1, r = 2e5 + 10;
                while (l < r)
                {
                    int mid = l + r + 1 >> 1;
                    if (ask(1, x + 1, x + mid))
                        l = mid;
                    else
                        r = mid - 1;
                }
                if (ask(1, x + 1, x + 1))
                {
                    ans += r + 1;
                    modify(1, x + 1, x + r, -1);
                    st.insert(x + r);
                }
                else
                {
                    ans++;
                    st.insert(x);
                }
                if (x - 1 != 0)
                    st.erase(st.find(x - 1));
            }
            else
                modify(1, x, x, 1);
        }
        else
        {
            if (ask(1, x, x))
                modify(1, x, x, -1);
            else
            {
                auto it = st.lower_bound(x);
                int now = *it;
                ans -= now - x + 1;
                if (x != now)
                    modify(1, x + 1, now, 1);
                st.erase(it);
                st.insert(x - 1);
            }
        }
        cout << ans << endl;
    }
}
signed main()
{
    Yshanqian;
    int T;
    T = 1;
    // cin >> T;
    for (int cases = 1; cases <= T; ++cases)
    {
        // cout<<"Case #"<<cases<<": ";
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 76352kb

input:

5
2 1 3 0 2
6
1 0
1 1
2 4
1 3
2 1
2 1

output:

4
5
7
7
9
7
3

result:

ok 7 numbers

Test #2:

score: 0
Accepted
time: 16ms
memory: 75796kb

input:

1
0
0

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 8ms
memory: 75856kb

input:

10
3 8 1 4 10 3 10 9 7 10
20
2 5
1 4
1 2
1 4
1 3
1 3
1 0
2 8
1 5
1 4
1 0
1 3
1 8
1 6
1 4
1 1
1 5
1 9
1 6
2 7

output:

14
14
14
22
22
22
22
24
24
24
24
26
26
26
26
26
26
26
26
26
26

result:

ok 21 numbers

Test #4:

score: 0
Accepted
time: 22ms
memory: 75924kb

input:

10
9 8 7 5 5 4 3 2 1 1
20
2 4
1 8
2 6
1 2
1 2
2 5
2 2
1 0
1 6
1 6
2 9
1 2
2 7
2 8
2 3
1 9
1 7
1 4
2 6
1 7

output:

45
44
45
44
45
45
44
44
45
46
46
45
45
43
43
42
43
44
44
44
45

result:

ok 21 numbers

Test #5:

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

input:

100
969 519 608 546 957 82 670 100 92 581 974 529 970 54 639 216 238 620 966 162 430 10 446 884 895 292 450 180 619 389 943 855 204 605 514 997 325 98 643 915 744 249 333 431 160 434 714 976 168 573 682 69 873 285 668 561 159 858 864 683 266 564 350 214 461 421 213 568 279 624 749 433 735 437 978 95...

output:

4923
4923
4923
4923
4923
4923
4923
4923
4923
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4927
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
4931
...

result:

ok 1001 numbers

Test #6:

score: 0
Accepted
time: 12ms
memory: 76952kb

input:

100
994 983 981 980 962 959 959 950 933 887 877 869 841 828 809 807 803 793 789 778 773 772 768 767 765 765 759 757 749 747 742 738 730 724 681 675 656 638 627 626 615 612 593 592 545 543 540 534 531 529 525 520 518 515 512 510 500 494 472 472 432 415 414 411 399 375 350 328 315 307 292 261 258 246 ...

output:

51041
51042
51041
51042
51043
51042
51043
51042
51040
51038
51037
51038
51039
51038
51039
51040
51039
51040
51039
51038
51037
51036
51037
51038
51037
51038
51037
51036
51035
51036
51037
51035
51034
51035
51034
51035
51036
51037
51036
51037
51039
51038
51037
51036
51035
51034
51033
51032
51031
51032
...

result:

ok 1001 numbers

Test #7:

score: -100
Time Limit Exceeded

input:

1000
549187 776775 748956 28001 575132 18015 112492 144497 206885 842190 403842 456113 424268 871411 648618 186832 693358 781526 443190 175126 586343 918652 923262 973941 509929 433837 392849 452585 497398 331180 118333 152788 959909 539943 747365 261855 819641 618091 801231 355664 285761 895793 398...

output:


result: