CF1208F. Bits And Pieces 题解 高维前缀和

题目链接:https://codeforces.com/problemset/problem/1208/F

其实是 高维后缀和

解题思路参考自 https://www.cnblogs.com/heyuhhh/p/11585358.html

示例程序:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5, N = 21;

int n, a[maxn];

pair<int, int> pos[1<<N];

void merge(pair<int, int> &a, pair<int, int> b) {
    int tmp[5] = { a.first, a.second, b.first, b.second };
    sort(tmp, tmp+4, greater<int>());
    unique(tmp, tmp+4);
    a = { tmp[0], tmp[1] };
}

int cal(int p) {
    int x = a[p], y = 0;
    for (int i = N-1; i >= 0; i--) {
        if ((x >> i) & 1)
            continue;
        int tmp = y ^ (1 << i);
        if (pos[tmp].second > p)
            y = tmp;
    }
    return x | y;
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", a+i);
        pos[ a[i] ].second = pos[ a[i] ].first;
        pos[ a[i] ].first = i;
    }
    for (int i = 0; i < N; i++) {
        for (int s = 0; s < (1<<N); s++) {
            if ((s>>i) & 1)
                continue;
            merge(pos[s], pos[s^(1<<i)]);
        }
    }
    int ans = 0;
    for (int i = 1; i <= n-2; i++) {
        ans = max(ans, cal(i));
    }
    printf("%d\n", ans);
    return 0;
}
posted @ 2026-03-17 16:43  quanjun  阅读(4)  评论(0)    收藏  举报