CF1261F Xor-Set (3100🥵)
首先考虑什么样的区间是容易考虑的,显然如果一个区间形如
这样直接暴力两两匹配会产生
考虑优化,显然我们可以定下来可以任意定的位数更多的区间,然后剩下的只需要在线段树上跳父亲,直到跳到与第一个区间深度相等的地方,取这个区间来和第一个区间算答案,容易发现答案不变,但是这样复杂度更优:
考虑一个深度
所以总区间有
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace PTqwq {
int readqwq() {
int x = 0;
bool f = false;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) f |= (c == '-');
for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c - '0');
if (f) {
x = -x;
}
return x;
}
ll readllqwq() {
ll x = 0;
bool f = false;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) f |= (c == '-');
for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c - '0');
if (f) {
x = -x;
}
return x;
}
const ll mod = 998244353;
ll query(ll l, ll r) {
if (l == 0) {
ll u = r;
ll v = r + 1;
if (u % 2 == 0) {
u >>= 1;
} else {
v >>= 1;
}
u %= mod;
v %= mod;
return (u * v % mod);
} else if (l > r) {
return 0;
} else {
ll ans = (query(0, r) + mod - query(0, l - 1) % mod) % mod;
return ans;
}
}
vector<pair<ll, ll>> a[80][2], b[80][2], c[80][2], U;
void dfs(int d, ll l, ll r, ll x, ll y) {
if (l > r) {
return;
}
if (x <= l && r <= y) {
c[d][1].push_back(make_pair(l, r));
return;
}
c[d][0].push_back(make_pair(l, r));
ll mid = (l + r) / 2;
if (x <= mid) dfs(d + 1, l, mid, x, y);
if (y > mid) dfs(d + 1, mid + 1, r, x, y);
}
void Solve() {
int n = readqwq();
for (int i = 1; i <= n; ++ i) {
ll l = readllqwq();
ll r = readllqwq();
for (int j = 0; j <= 70; ++ j) {
c[j][0].clear();
c[j][1].clear();
}
dfs(0, 0, (1ll << 60) - 1, l, r);
for (int j = 0; j <= 70; ++ j) {
for (auto info : c[j][0]) {
a[j][0].push_back(info);
}
for (auto info : c[j][1]) {
a[j][1].push_back(info);
}
}
}
n = readqwq();
for (int i = 1; i <= n; ++ i) {
ll l = readllqwq();
ll r = readllqwq();
for (int j = 0; j <= 70; ++ j) {
c[j][0].clear();
c[j][1].clear();
}
dfs(0, 0, (1ll << 60) - 1, l, r);
for (int j = 0; j <= 70; ++ j) {
for (auto info : c[j][0]) {
b[j][0].push_back(info);
}
for (auto info : c[j][1]) {
b[j][1].push_back(info);
}
}
}
for (int i = 0; i <= 70; ++ i) {
for (int j = 0; j < 2; ++ j) {
for (int k = 0; k < 2; ++ k) {
if (j == 1 || k == 1) {
for (auto info : a[i][j]) {
for (auto infoB : b[i][k]) {
ll LenA = info.second - info.first + 1;
ll LenB = infoB.second - infoB.first + 1;
LenA = max(LenA, LenB);
ll x = info.first - (info.first & (LenA - 1));
ll y = infoB.second - (infoB.second & (LenA - 1));
x ^= y;
y = x + LenA - 1;
if (x <= y) {
U.push_back(make_pair(x, y));
}
}
}
}
}
}
}
if (!U.empty()) {
sort(U.begin(), U.end());
ll posL = -115, posR = -115, ans = 0;
for (auto info : U) {
ll L = info.first;
ll R = info.second;
if (L > posR) {
if (posL >= 0 && posL <= posR) {
ans = (ans + query(posL, posR)) % mod;
}
posL = L;
posR = R;
} else {
if (posR < R) {
posR = R;
}
}
}
if (posL >= 0 && posL <= posR) {
ans = (ans + query(posL, posR)) % mod;
}
ans = (ans % mod + mod) % mod;
ans %= mod;
printf("%lld\n", ans);
} else {
printf("0\n");
}
}
}
int main() {
PTqwq::Solve();
return 0;
}