AT4142 [ARC098B] Xor Sum 2

\(\large{题目链接}\)
\(\\\)
\(\Large\textbf{Solution: } \large{首先,异或有一个性质:\text{x} - \text{y} \leq \text{x} \oplus \text{y} \leq \text{x} + \text{y}。\\观察题目,容易发现,若一个区间符合要求,那么\text{a}_l到\text{a}_y的二进制每一位上最多只有一个1。\\然后设f[l]为l所能拓展到的最右点,那么显然l + 1也符合条件,因为l+1中1的个数只可以比l少,所以l满足单增,可以用尺取法来维护。}\)

\(\Large\textbf{Code: }\)

#include <bits/stdc++.h>
#define gc() getchar() 
#define LL long long
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rep(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
const int N = 2e5 + 5;
int n, a[N];

inline int read() {
	int x = 0;
	char ch = gc();
	while (!isdigit(ch)) ch = gc();
	while (isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
	return x; 
}

int main() {
	ios::sync_with_stdio(false);
	n = read();
	rep(i, 1, n) a[i] = read();
	LL ans = 0;
	for (int i = 1, l = 1, r = 1, cur = 0; i <= n; ++i) {
		for (; !(cur & a[r]) && r <= n;) cur |= a[r++], ans += r - l;
		cur ^= a[l++];
	}
	cout << ans << endl;
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/Miraclys/p/12583198.html