跳到正文

题解

题解:P8539 「Wdoi-2」来自地上的支援

woshnd约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年11月16日
收录
2026年11月16日
更新
2026年11月16日
篇幅
约 4 分钟
获赞
2
题目
P8539

版本

  1. v1当前

首先发现如果 ii 被至少选中 kk 次,这 kk 次一定是从 ii 开始的连续 kk 次。

原因是,如果一个数在序列里时,选中的不是它,说明它比选中的数小,而被选中的数会越来越大,它更加不可能被选中。

这个数要满足两个条件

  1. 这个数加入时比操作过的 ac(c<i)a_c(c<i) 都大。
  2. 这个数比 ai+c−c×va_{i+c} - c \times v 大(c≤k−1c \le k-1)。

第一个条件很好做,O(n)O(n) 模拟一下就好了。

第二个条件我们可以用一些小技巧用线段树维护。发现 kk 个数要减的是一个等差数列,我们可以将所有数都变成 ai−i×va_i - i \times v。要查找 aia_i 到 ai+k−1a_{i+k-1} 之间的最大值时,可以都加一个 i×vi \times v,建议自己手推一下样例。

C++
#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;
}

注意卡空间和时间。

原文