首先考虑什么样的区间是容易考虑的,显然如果一个区间形如 那么我们就可以任意的考虑低 位的值,也就是可以随意定低 位的异或和,所以我们把每个区间都放到 的线段树上,显然每个区间最多被拆成 个区间,然后每个集合最多会有 个区间,容易发现任意两个 具有这个性质的区间 可以 xor 出来的数依然是一个区间,假设两个区间分别可以让我们随意定低 位,那么最后的低 位都是可以随意定的,然后剩下的位是固定的。
这样直接暴力两两匹配会产生 个区间,sort 之后可以算出答案,但是这个复杂度过高。
考虑优化,显然我们可以定下来可以任意定的位数更多的区间,然后剩下的只需要在线段树上跳父亲,直到跳到与第一个区间深度相等的地方,取这个区间来和第一个区间算答案,容易发现答案不变,但是这样复杂度更优:
考虑一个深度 ,拆分后的区间是这个深度的区间最多有 个,跳父亲跳到这个深度的区间也最多有 个,具体分析可以模拟一下这个过程,找到第一个非单侧递归的时刻,然后左右儿子分成的都是线段树拆一个前后缀的情况,这样分析起来就是容易的了,所以这样产生的区间最多有 个。
所以总区间有 个,加上排序总时间复杂度 ,可以通过。
#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;
}