CodeForces 1847F The Boss's Identity
我们首先观察 \(a\) 的形态。令题面中给出的 \(a_{1 \sim n}\) 为 \(b_{1 \sim n}\)。先从 \(n\) 比较小的情况开始分析,若 \(n = 6\),那么 \(a\) 从第 \(n + 1\) 位开始长这样(数字拼接代表下标对应的值按位或起来):
\[12, 23, 34, 45, 56
\]
\[612, 123, 234, 345, 456
\]
\[5612, 6123, 1234, 2345, 3456
\]
\[45612, 56123, 61234, 12345, 23456
\]
\[123456 \cdots
\]
我们发现 \(a\) 的值是由一些环形子区间的按位或组成的,且长度较小的在长度较大的前面。
现在一个简单的想法是对于每个询问都二分找到第一个满足的位置。先二分区间长度,再二分区间左端点。注意到固定左端点,以这个点的后缀按位或最多变化 \(O(\log V)\) 次。因此我们可以预处理出 \(O(n \log V)\) 个按位或和相同的区间,从而得到固定长度的区间按位或最大值。但是我们发现没办法快速回答左端点在一个区间内,区间长度固定的区间按位或最大值。
考虑离线,枚举区间长度 \(i\),将 \(O(n \log V)\) 个按位或和相同的区间按照长度从小到大处理,这样我们能顺便用线段树维护,长度为 \(i\) 的以每个点为左端点的区间按位或。我们把最短区间长度为 \(i\) 的询问放在一起处理,二分区间左端点即可。
code
// Problem: F. The Boss's Identity
// Contest: Codeforces - Codeforces Round 882 (Div. 2)
// URL: https://codeforces.com/contest/1847/problem/F
// Memory Limit: 512 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
const int maxn = 400100;
const int logn = 22;
int n, m, a[maxn], f[maxn][logn];
ll ans[maxn];
vector<pii> vc[maxn];
struct node {
int x, id;
} b[maxn];
inline int query(int l, int r) {
int k = __lg(r - l + 1);
return (f[l][k] | f[r - (1 << k) + 1][k]);
}
namespace SGT {
int tree[maxn << 2];
inline void pushup(int x) {
tree[x] = max(tree[x << 1], tree[x << 1 | 1]);
}
void build(int rt, int l, int r) {
tree[rt] = 0;
if (l == r) {
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
}
void update(int rt, int l, int r, int x, int y) {
if (l == r) {
tree[rt] = y;
return;
}
int mid = (l + r) >> 1;
(x <= mid) ? update(rt << 1, l, mid, x, y) : update(rt << 1 | 1, mid + 1, r, x, y);
pushup(rt);
}
int query(int rt, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
return tree[rt];
}
int mid = (l + r) >> 1, res = 0;
if (ql <= mid) {
res = max(res, query(rt << 1, l, mid, ql, qr));
}
if (qr > mid) {
res = max(res, query(rt << 1 | 1, mid + 1, r, ql, qr));
}
return res;
}
}
void solve() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
a[i + n] = a[i];
vector<pii>().swap(vc[i]);
}
for (int i = 1; i <= n * 2; ++i) {
f[i][0] = a[i];
}
for (int j = 1; (1 << j) <= n * 2; ++j) {
for (int i = 1; i + (1 << j) - 1 <= n * 2; ++i) {
f[i][j] = (f[i][j - 1] | f[i + (1 << (j - 1))][j - 1]);
}
}
for (int i = 1; i <= m; ++i) {
scanf("%d", &b[i].x);
b[i].id = i;
ans[i] = -1;
}
sort(b + 1, b + m + 1, [&](node a, node b) {
return a.x < b.x;
});
for (int i = 1; i <= n; ++i) {
int lst = i - 1;
while (lst < i + n - 1) {
int l = lst + 1, r = i + n - 1, pos = -1, x = query(i, lst + 1);
while (l <= r) {
int mid = (l + r) >> 1;
if (query(i, mid) == x) {
pos = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
vc[lst - i + 2].pb(i, x);
lst = pos;
}
}
SGT::build(1, 1, n);
for (int i = 1, j = 1; i <= n; ++i) {
for (pii p : vc[i]) {
SGT::update(1, 1, n, p.fst, p.scd);
}
int k = j;
while (j <= m && SGT::tree[1] > b[j].x) {
++j;
}
for (int p = k; p < j; ++p) {
int x = b[p].x, l = n + 3 - i, r = n, pos = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (SGT::query(1, 1, n, n + 3 - i, mid) > x) {
pos = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
if (pos != -1) {
ll t = 1LL * (i - 1) * (n - 1);
if (i != 1) {
++t;
}
ans[b[p].id] = pos - (n + 3 - i) + 1 + t;
continue;
}
l = 1;
r = min(n + 2 - i, n);
pos = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (SGT::query(1, 1, n, 1, mid) > x) {
pos = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
ll t = 1LL * (i - 1) * (n - 1);
if (i != 1) {
++t;
}
ans[b[p].id] = max(0, n - (n + 3 - i) + 1) + t + pos;
}
}
for (int i = 1; i <= m; ++i) {
printf("%lld\n", ans[i]);
}
}
int main() {
int T = 1;
scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}