题解:P17723 [SCP-S 2026] 函数 / func

· · 题解

首先看到 \text{OR}(S)\lor\text{MEX}(S),感觉很吓人很抽象,可以先只考虑其中一个,然后补充另一个的额外贡献。

显然前者更简单。如果只有 \text{OR}(S) 那么将是简单拆贡献题。考虑拆位。枚举二进制位 j,若 a_i 的第 j 位为 1,则它的贡献是它作为区间内最左侧 1 的区间个数。记 lst 为上一个 1 的位置,则其左端点可以在 lst + 1 \sim i 中任选,右端点可以在 i \sim n 中任选,贡献为 (i-lst) \times (n-i+1)。将贡献累加,乘以 2^j,加入答案即可。

现在考虑补充 \text{MEX}(S) 的额外贡献。设 x=\text{MEX}(S),则所有小于 x 的 2 的幂均已被 0 \sim x-1 的按位或贡献。故当且仅当 x 为 2 的幂的时候会产生 x 的贡献。同时要注意到 x \le n。

考虑如何寻找所有满足 \text{MEX}(S)=x 的区间 [l,r],其中 x 为 2 的幂。注意到若两个区间 \text{MEX}(S) 相同,其中一个区间包含另一个,那么可以只保留被包含的。所以我们可以先枚举 x,然后使用双指针来找到每个左端点对应的最小的右端点,满足区间内 0 \sim x-1 都至少出现一次(对于区间内包含 x 等非法情况我们一会排除)。具体地,我们只考虑在 0 \sim x-1 中的数,然后使用 cnt_i 记录当前区间内 i 的个数,用 tot 记录当前区间内数的种数,在加后或减前 cnt_i=1 时更改 tot。对于枚举的左端点,我们移动右端点,直到 tot=x 结束即可。

最后考虑这些区间的贡献。我们称“X 按位包含 Y”当且仅当 X \land Y = Y,则每个区间可以贡献到所有包含它且不存在元素按位包含 x 的区间。为避免算重,与先前的贡献计算类似,对于一个区间 [l,r],我们找到上一个区间 [l',r'],贡献即为 (l-\max(pre, l')) \times (nxt-r),其中 pre,nxt 分别为 l 前面第一个、后面第一个按位包含 x 的数的位置(若不存在 pre,则令 pre=0;若不存在 nxt,则令 nxt=n+1)。注意如果 [l,r] 中已经存在元素按位包含 x,则贡献为 0。

由于外层有对二进制位的枚举,而 n \le 2 \times 10^6,所以内层必须线性维护 pre,nxt。注意到 pre,nxt 随左端点右移单调不降,直接用一个指针 p 维护 pre 即可。具体地,用一个 vector 存所有按位包含 x 的 a_i 的位置,然后向右移动 p 直到 v_{p} < l \le v_{p+1},此时 pre=v_p,nxt=v_{p+1}。为实现方便,可以提前往 vector 内插入 0 和 n+1 来处理掉没有 pre 和没有 nxt 的边界情况。

至此本题结束。时间复杂度 \mathcal O(n \log V),其中 V 为 a_i 的值域大小。

PS:

#include<iostream>
#include<vector>
using namespace std;
typedef unsigned long long ull;
typedef pair <int, int> pii;
const int maxn = 2e6 + 5;
int n, a[maxn], p, cnt[maxn], tot;
ull ans, now, lst;
vector <int> v;
vector <pii> v1;
int read(){
    char ch = getchar_unlocked();
    int res = 0;
    while(ch < '0' || ch > '9') ch = getchar_unlocked();
    while(ch >= '0' && ch <= '9') res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar_unlocked();
    return res;
}
int main(){
    n = read();
    for(int i = 1; i <= n; ++i) a[i] = read();
    for(int i = 0; i < 30; ++i){
        lst = now = 0;
        for(int j = 1; j <= n; ++j)
            if(a[j] >> i & 1) now += (j - lst) * (n - j + 1), lst = j; 
        now *= (1 << i);
        ans += now;
    }
    for(int i = 0; i <= 20; ++i){
        v.clear();
        v1.clear();
        tot = p = 0;
        for(int j = 0; j < (1 << i); ++j) cnt[j] = 0;
        v.push_back(0);
        for(int j = 1; j <= n; ++j){
            if(a[j] >> i & 1) v.push_back(j);
            while(p < n && tot < (1 << i)){
                p++;
                if(a[p] >= (1 << i)) continue;
                cnt[a[p]]++;
                if(cnt[a[p]] == 1) tot++;
            }
            if(tot == (1 << i)) v1.push_back({j, p});
            if(a[j] >= (1 << i)) continue;
            cnt[a[j]]--;
            if(!cnt[a[j]]) tot--;
        } 
        v.push_back(n + 1);
        p = -1, now = 0;
        for(int j = 0; j < v1.size(); ++j){
            while(v[p + 1] < v1[j].first) p++;
            if(v[p + 1] <= v1[j].second) continue;
            lst = v[p];
            if(j) lst = max((int)lst, v1[j - 1].first);
            now += (v1[j].first - lst) * (v[p + 1] - v1[j].second);
        }
        now *= (1 << i);
        ans += now;
    }
    cout << ans;
    return 0;
}