题解:P8539 「Wdoi-2」来自地上的支援
首先发现如果
原因是,如果一个数在序列里时,选中的不是它,说明它比选中的数小,而被选中的数会越来越大,它更加不可能被选中。
这个数要满足两个条件
- 这个数加入时比操作过的
a_c(c<i) 都大。 - 这个数比
a_{i+c} - c \times v 大(c \le k-1 )。
第一个条件很好做,
第二个条件我们可以用一些小技巧用线段树维护。发现
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=2100005;
const int NEG=-(1ll<<62);
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){ if(ch=='-')f=-1; ch=getchar(); }
while(ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); }
return x*f;
}
int mx[N<<1];
pii prmx[N];
vector<int> a;
int n,m,v,base;
inline int query(int l,int r){
if(l>r) return NEG;
l += base-1;
r += base-1;
int ans=NEG;
while(l<=r){
if(l&1) ans=max(ans,mx[l++]);
if(!(r&1)) ans=max(ans,mx[r--]);
l>>=1; r>>=1;
}
return ans;
}
signed main(){
n=read(); m=read(); v=read();
a.resize(n+1);
for(int i=1;i<=n;i++) a[i]=read();
base=1;
while(base<n) base<<=1;
{
vector<pii> stk(n+2);
int top=0;
stk[0]={0,0};
for(int i=1;i<=n;i++){
prmx[i]=stk[top];
pii cur={a[i],a[i]};
while(top && cur > stk[top]) top--;
if(!top) stk[++top]={a[i]+v,a[i]};
else stk[top].fi+=v;
}
}
for(int i=1;i<=base;i++){
if(i<=n) mx[base+i-1]=a[i]-i*v;
else mx[base+i-1]=NEG;
}
for(int p=base-1;p>=1;p--){
mx[p]=max(mx[p<<1],mx[p<<1|1]);
}
vector<int>().swap(a);
int ans1=0, ans2=0;
while(m--){
int x=read(), k=read();
if(x+k-1>n) continue;
int q=query(x+1,x+k-1);
int cand1=max(q + x*v + 1, 0ll);
int cand2=0;
if(!(prmx[x].fi==0 && prmx[x].se==0))
cand2=prmx[x].fi+(prmx[x].fi<=prmx[x].se?1:0);
int cur=max(cand1,cand2);
ans1^=cur;
ans2+=cur;
}
cout<<ans1<<' '<<ans2;
return 0;
}
注意卡空间和时间。