跳到正文

题解

AT_abc470_c [ABC470C] Inc, Dec, Xor 题解

KobeBeanBryantCox约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年8月10日
收录
2026年9月13日
更新
2026年11月16日
篇幅
约 2 分钟

版本

  1. v1当前

AT_abc470_c [ABC470C] Inc, Dec, Xor 题解

好题啊,我赛时不会,所以是好题。

题目传送门。

题意

初始全 00,单点加 11,全局减 11 再对 00 取 max⁡\max。每次询问全局异或和。

思路

注意到 2 操作修改的个数不会超过前面 1 操作的个数。

所以你拿个 vector 啥的维护一下 ≥1\ge 1 的位置暴力修改就是对的。

时间复杂度 O(n)O(n)。

AC 代码

C++
#include<bits/stdc++.h>
#define Code using
#define by namespace
#define wjb std
Code by wjb;
const int N=5e5+10;
int a[N];
int ans;
vector<int>v,p;
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n,q;cin>>n>>q;
    while(q--)
    {
        int op;cin>>op;
        if(op==1)
        {
            int x;cin>>x;
            if(!a[x])v.push_back(x);
            ans^=a[x]^(a[x]+1),a[x]++,cout<<ans<<'\n';
            continue;
        }
        p.clear();
        for(int k:v)
        {
            ans^=a[k]^(a[k]-1);
            if(a[k]>1)p.push_back(k);
            a[k]--;
        }
        v=p,cout<<ans<<'\n';
    }
    return 0;
}
原文