题解:P17110 「FAOI-R13」XOR and Less
二进制贪心小结论:记最低位为第
我们枚举左端点
考虑最高位(第
我们发现当第
于是我们考虑任意位次(记为第
时间复杂度
:::success[CODE]
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 5e+5 + 5, mod = 998244353;
int n;
int a[maxn], b[maxn][31], nxt[maxn][31];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
for (int j = 0; j <= 30; j++) {
if ((a[i] >> j) & 1) b[i][j] = 1;
}
}
for (int j = 0; j <= 30; j++) {
for (int i = n - 1; i >= 0; i--) {
if (b[i + 1][j] == 1) nxt[i][j] = i + 1;
else nxt[i][j] = nxt[i + 1][j];
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
int r = n + 1;
for (int j = 30; j >= 0; j--) {
int x = nxt[i][j];
if (!x) x = n + 1;
ans = (ans + (n - min(x, r) + 1) * ((1ll << j) % mod) % mod) % mod;
int y = nxt[x][j];
if (y) r = min(r, y);
}
}
cout << ans;
return 0;
}
:::
欢迎找错,欢迎 hack。