0%

1872E. Data Structures Fan

思路讲解

哈哈,其实说起来也简单,在xor中,加上这个数和减去这个数都是xor,因此对区间进行反转操作,就是对这个区间进行异或就可以了,不用区分是加上还是减去。

当然,怎么对区间进行异或那?其实也很简单,异或前缀和。

AC代码

https://codeforces.com/problemset/submission/1872/309601127

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Problem: E. Data Structures Fan
// Contest: Codeforces - Codeforces Round 895 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1872/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(1e5)+10,INF=static_cast<ll>(5e18)+3;

ll N,T,A[MAXN];
char S[MAXN];
ll sum[MAXN];
inline void solve(){
cin>>N;
sum[0]=0;
for(int i=1;i<=N;++i){
cin>>A[i];
sum[i]=sum[i-1]^A[i];
}
ll res1=0,res0=0;
for(int i=1;i<=N;++i){
cin>>S[i];
if(S[i]=='1'){
res1^=A[i];
}else{
res0^=A[i];
}
}
ll Q;
cin>>Q;
for(int i=1;i<=Q;++i){
ll op;
cin>>op;
if(op==1){
ll l,r;
cin>>l>>r;
res1^=(sum[r]^sum[l-1]);
res0^=(sum[r]^sum[l-1]);
}else{
ll ind;
cin>>ind;
if(ind){
cout<<res1<<" ";
}else{
cout<<res0<<" ";
}
}
}
cout<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
/*
AC
https://codeforces.com/problemset/submission/1872/309601127
*/

心路历程(WA,TLE,MLE……)