函数(func)题解
mahaorui2012 · · 题解
记值域为
先快速计算
:::success[对
因此,对固定的左端点,满足上述条件的右端点,是一个后缀,且最小的满足条件的右端点单调不减。
:::success[更严谨的证明]
对固定的左端点
| 综上,对固定的左端点,满足 |
|---|
考虑双指针。对每个左端点
:::success[std]
#include <iostream>
#include <cstring>
#define ull unsigned long long
using namespace std;
const int N=2000005,LOGV=30;
int arr[N];
int buc[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;++i){
cin>>arr[i];
}
ull ans=0;
arr[n+1]=(1<<30)-1;
for(int k=0;k<LOGV;++k){
int p2k=(1<<k);
int r=0,cnot=p2k,ccnt=0;
memset(buc,0,sizeof(buc));
for(int i=1;i<=n;++i){
if(i>1){
ccnt-=bool(arr[i-1]&p2k);
if(arr[i-1]<min(n,p2k)){
--buc[arr[i-1]];
if(!buc[arr[i-1]]){
++cnot;
}
}
}
while(r<=n && (r<i || (cnot && !ccnt))){
++r;
ccnt+=bool(arr[r]&p2k);
if(arr[r]<min(n,p2k)){
if(!buc[arr[r]]){
--cnot;
}++buc[arr[r]];
}
}ans+=(n-r+1)*(1ull<<k);
}
}cout<<ans;
return 0;
}
:::