洛谷P4462 [CQOI2018]异或序列(莫队)

题意

题目链接

Sol

一开始以为K每次都是给出的想了半天不会做。

然而发现读错题了维护个前缀异或和然后直接莫队搞就行,。

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define LL long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 998244353, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '
';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, Q, K, a[MAXN], num[MAXN], belong[MAXN], block;
LL now, ans[MAXN];
struct Query {
	int l, r, id;
	bool operator < (const Query &rhs) const {
		return belong[l] == belong[rhs.l] ? r < rhs.r : belong[l] < belong[rhs.l];
	}
}q[MAXN];
void Add(int x) {
	now += num[K ^ x];
	num[x]++;
}
void Delet(int x) {
	num[x]--;
	now -= num[K ^ x];
}
void solve() {
	int l = 1, r = 0;
	for(int i = 1; i <= Q; i++) {
		while(r < q[i].r) Add(a[++r]);
		while(r > q[i].r) Delet(a[r--]);
		while(l < q[i].l) Delet(a[l++]);
		while(l > q[i].l) Add(a[--l]);
		ans[q[i].id] = now;
	}
}
signed main() {
	N = read(); Q = read(); K = read(); block = sqrt(N);
	for(int i = 1; i <= N; i++) a[i] = read() ^ a[i - 1], belong[i] = (i - 1) / block + 1;
	for(int i = 1; i <= Q; i++) q[i].l = read() - 1, q[i].r = read(), q[i].id = i;
	sort(q + 1, q + Q + 1);
	solve();
	for(int i = 1; i <= Q; i++) cout << ans[i] << '
';
	return 0;
}
原文地址:https://www.cnblogs.com/zwfymqz/p/10469456.html