Description
给定序列 , 次查询 ,求出 。
Limitations
Solution
视 同阶。对于位 如果 则它为 。设 ,找出 的取值范围:
- 若 ,那么 。
- 若 ,那么 或 。
接下来比较抽象。我们建一个 层的结构,建第 层时,维护一个 上的差分数组,把分界点全部丢进去。为了方便,把查询的端点 的值也丢进去,并对下标排序,然后前缀和回来。
接着要和上一层合并,设上一层的下标为 ,则它对应本层的 或 ,先继承 ,然后加上本层的贡献。
用基数排序和归并优化,每层都可以 建立。由于查询点已经包含在内,建完后在最后一层 RMQ 即可,代码用了二分单调栈,复杂度 。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;
template<class T>
bool chmax(T &a, const T &b){
if(a < b){ a = b; return true; }
return false;
}
template<class T>
bool chmin(T &a, const T &b){
if(a > b){ a = b; return true; }
return false;
}
struct Node {
i64 x;
int tag, mx;
Node() {}
Node(i64 x, int tag, int mx) : x(x), tag(tag), mx(mx) {}
bool operator < (const Node &o) const {
return x < o.x;
}
};
struct Query {
i64 L, R;
int id;
Query() {}
Query(i64 L, i64 R, int id) : L(L), R(R), id(id) {}
bool operator < (const Query &o) const {
return R < o.R;
}
};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
vector<i64> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
i64 mx = 0;
vector<Query> que(q);
vector<i64> c;
for (int i = 0; i < q; i++) {
i64 L, R;
cin >> L >> R;
que[i] = {L, R, i};
c.push_back(L), c.push_back(R + 1);
chmax(mx, R + 1);
}
int bits = __lg(mx) + 2;
vector<Node> now;
for (int k = 0; k < bits; k++) {
i64 y1 = 1LL << k, y2 = 2LL << k, y3 = 3LL << k, mask = y2 - 1;
stable_partition(a.begin(), a.end(), [&](i64 x) { return x & (1ll << k); });
stable_partition(c.begin(), c.end(), [&](i64 x) { return !(x & (1ll << k)); });
vector<Node> prv;
swap(now, prv), now.clear();
auto add = [&](i64 x, int tag) {
if (now.empty() || now.back().x != x) now.emplace_back(x, tag, 0);
else now.back().tag += tag;
};
add(0, 0);
for (int i = 0; i < n; i++)
if ((a[i] & mask) < y2) add(max(0LL, y1 - (a[i] & mask)), 1);
add(y1, 0);
int cnt = now.size();
for (int i = 0; i < n; i++)
if ((a[i] & mask) + mask >= y3) add(y3 - (a[i] & mask), 1);
inplace_merge(now.begin(), now.begin() + cnt, now.end());
cnt = now.size();
for (int i = 0; i < n; i++)
if ((a[i] & mask) < y2) add(y2 - (a[i] & mask), -1);
inplace_merge(now.begin(), now.begin() + cnt, now.end());
cnt = now.size();
for (int i = 0; i < c.size(); i++)
if ((c[i] & mask) != ((i ? c[i - 1] : 0LL) & mask)) add(c[i] & mask, 0);
inplace_merge(now.begin(), now.begin() + cnt, now.end());
add(y2, 0);
int i = 0;
i64 lst = -1;
for (auto &x : now) {
if (lst != x.x) now[i++] = x;
else now[i - 1].tag += x.tag;
lst = x.x;
}
now.resize(i);
for (int i = 1; i < now.size(); i++) now[i].tag += now[i - 1].tag;
if (k) {
int i = 0;
for (int j = 0; j < prv.size(); j++) {
auto [x, tag, mx] = prv[j];
if (x == now[i + 1].x) i++;
chmax(now[i].mx, mx);
}
for (int j = 0; j < prv.size(); j++) {
auto [x, tag, mx] = prv[j];
if (x == now[i + 1].x - y1) i++;
chmax(now[i].mx, mx);
}
}
for (auto &it : now) it.mx += it.tag;
}
sort(que.begin(), que.end());
int i = 0, top = 0; vector<int> ans(q);
for (int j = 0; j < q; j++) {
auto [L, R, id] = que[j];
for (; i < now.size() && now[i].x <= R; i++) {
while (top && now[top - 1].mx < now[i].mx) top--;
now[top++] = now[i];
}
ans[id] = lower_bound(now.begin(), now.begin() + top, Node(L, 0, 0))->mx;
}
for (int i = 0; i < q; i++) cout << ans[i] << '\n';
return 0;
}