CF 1872 E

发布时间 2023-09-08 13:11:45作者: 铜锣骚

E. Data Structures Fan

这道题可以先对数组\(a\)进行异或前缀和操作,得到数组\(b\),并在初始情况下记录下所有\(s[i]\)为0/1的异或和为\(x0\)\(x1\),之后处理询问操作。

  • \(ty=1\)时,需要对\(s[i]\)(\(l≤i≤r\))进行反转,即让\(x0\)\(x1\)\(a[i]\)(\(l≤i≤r\))进行异或操作,即

\[x0= x0\oplus(b[r] \oplus b[l - 1]); \]

\[x1= x1\oplus(b[r] \oplus b[l - 1]); \]

  • \(ty=2\)时,输出\(x0\)\(x1\)

代码

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;

const ll N = 1e5 + 10;
ll t;
ll n, a[N], q;
ll b[N];
string s;

signed main()
{
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> t;
    while(t--)
    {
        cin >> n;
        ll x0 = 0, x1 = 0;
        for(ll i = 1;i <= n;i++)
        {
            cin >> a[i];
            b[i] = b[i - 1] ^ a[i];
        }
        cin >> s;
        s = ' ' + s;
        for(ll i = 1;i <= n;i++)
        {
            if(s[i] == '0')
            {
                x0 ^= a[i];
            }
            else
            {
                x1 ^= a[i];
            }
        }
        cin >> q;
        while(q--)
        {
            ll ty;
            cin >> ty;
            if(ty == 1)
            {
                ll l, r;
                cin >> l >> r;
                x0 ^= (b[r] ^ b[l - 1]);
                x1 ^= (b[r] ^ b[l - 1]);
            }
            else
            {
                ll g;
                cin >> g;
                if(g == 0)
                {
                    cout << x0 << " ";
                }
                else
                {
                    cout << x1 << " ";
                }
            }
            
        }
        cout << endl;
    }
    return 0;
}