QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#562053 | #6521. Swapping Operation | liuziao | TL | 1ms | 5688kb | C++17 | 4.4kb | 2024-09-13 14:38:55 | 2024-09-13 14:38:55 |
Judging History
answer
#include <bits/stdc++.h>
// #define int int64_t
const int kMaxN = 1e5 + 5;
int n;
int a[kMaxN], fir[32][2], lst[32][2];
bool vis[kMaxN];
std::vector<std::tuple<int, int, int>> vec;
struct SGT {
int mx[kMaxN * 4], tag[kMaxN * 4];
void pushup(int x) {
mx[x] = std::max(mx[x << 1], mx[x << 1 | 1]);
}
void addtag(int x, int v) { mx[x] += v, tag[x] += v; }
void pushdown(int x) {
if (tag[x]) addtag(x << 1, tag[x]), addtag(x << 1 | 1, tag[x]), tag[x] = 0;
}
void build(int x, int l, int r) {
mx[x] = tag[x] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r);
}
void update(int x, int l, int r, int ql, int qr, int v) {
if (l > qr || r < ql) return;
else if (l >= ql && r <= qr) return addtag(x, v);
pushdown(x);
int mid = (l + r) >> 1;
update(x << 1, l, mid, ql, qr, v), update(x << 1 | 1, mid + 1, r, ql, qr, v);
pushup(x);
}
} sgt;
int brute() {
static int pre[kMaxN], suf[kMaxN];
pre[1] = a[1], suf[n] = a[n];
for (int i = 2; i <= n; ++i) pre[i] = (pre[i - 1] & a[i]);
for (int i = n - 1; i; --i) suf[i] = (suf[i + 1] & a[i]);
int ans = 0;
for (int i = 1; i < n; ++i) ans = std::max(ans, pre[i] + suf[i + 1]);
return ans;
}
void work(int x, int y) {
for (int i = 0; i <= 30; ++i) {
if (fir[i][0]) {
int now = n + 1;
if ((~a[x] >> i & 1) && (~a[y] >> i & 1)) now = fir[i][0];
else if ((~a[x] >> i & 1) && (a[y] >> i & 1)) now = std::min(y, x == fir[i][0] ? fir[i][1] : fir[i][0]);
else if ((a[x] >> i & 1) && (~a[y] >> i & 1)) now = std::min(x, y == fir[i][0] ? fir[i][1] : fir[i][0]);
else now = fir[i][0];
if (now > fir[i][0]) {
sgt.update(1, 1, n - 1, fir[i][0], now - 1, (1 << i));
vec.emplace_back(fir[i][0], now - 1, (1 << i));
} else {
sgt.update(1, 1, n - 1, now, fir[i][0] - 1, -(1 << i));
vec.emplace_back(now, fir[i][0] - 1, -(1 << i));
}
now = 0;
if ((~a[x] >> i & 1) && (~a[y] >> i & 1)) now = lst[i][0];
else if ((~a[x] >> i & 1) && (a[y] >> i & 1)) now = std::max(y, x == lst[i][0] ? lst[i][1] : lst[i][0]);
else if ((a[x] >> i & 1) && (~a[y] >> i & 1)) now = std::max(x, y == lst[i][0] ? lst[i][1] : lst[i][0]);
else now = lst[i][0];
if (now < lst[i][0]) {
sgt.update(1, 1, n - 1, now, lst[i][0] - 1, (1 << i));
vec.emplace_back(now, lst[i][0] - 1, (1 << i));
} else {
sgt.update(1, 1, n - 1, lst[i][0], now - 1, -(1 << i));
vec.emplace_back(lst[i][0], now - 1, -(1 << i));
}
}
}
}
int solve(int x, int y) {
work(x, y);
int res = sgt.mx[1];
for (auto [l, r, w] : vec) sgt.update(1, 1, n - 1, l, r, -w);
vec.clear();
return res;
}
void dickdreamer() {
std::cin >> n;
memset(fir, 0, sizeof(fir));
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
for (int j = 0; j <= 30; ++j) {
if (~a[i] >> j & 1) {
if (!fir[j][0]) fir[j][0] = i, vis[i] = 1;
else if (!fir[j][1]) fir[j][1] = i;
}
}
}
sgt.build(1, 1, n - 1);
for (int i = 0; i <= 30; ++i) lst[i][0] = lst[i][1] = n + 1;
for (int i = n; i; --i) {
for (int j = 0; j <= 30; ++j) {
if (~a[i] >> j & 1) {
if (lst[j][0] == n + 1) lst[j][0] = i, vis[i] = 1;
else if (lst[j][1] == n + 1) lst[j][1] = i;
}
}
}
int ans = brute(), det = 0;
for (int i = 0; i <= 30; ++i) {
if (fir[i][0]) {
sgt.update(1, 1, n - 1, 1, fir[i][0] - 1, (1 << i));
sgt.update(1, 1, n - 1, lst[i][0], n - 1, (1 << i));
} else {
det += (1 << i);
}
}
assert(ans == sgt.mx[1] + det);
for (int i = 1; i <= n; ++i) {
if (vis[i]) {
for (int j = 1; j <= n; ++j) {
if (vis[j]) {
std::swap(a[i], a[j]);
ans = std::max(ans, brute());
std::swap(a[i], a[j]);
} else {
ans = std::max(ans, solve(i, j) + det);
}
}
}
}
std::cout << ans << '\n';
}
int32_t main() {
#ifdef ORZXKR
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
int T = 1;
std::cin >> T;
while (T--) dickdreamer();
// std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5688kb
input:
3 6 6 5 4 3 5 6 6 1 2 1 1 2 2 5 1 1 2 2 2
output:
7 3 3
result:
ok 3 number(s): "7 3 3"
Test #2:
score: -100
Time Limit Exceeded
input:
1000 100 803046221 177233942 164782937 53823867 383853667 250036100 888743479 576687945 737272231 801579441 647643785 393059353 401516062 663466861 308544510 825328494 162739603 501761995 570908380 655227403 444493122 844535039 208303132 493226172 421479971 634680694 396627047 787471115 335787136 16...