题解:P17286 「IXOI R2」Horizon Blue

· · 题解

考虑操作分块,每做完一个块就暴力重构,所以只需要处理当前操作块中的 O(\sqrt n) 个特殊点。

考虑特殊点的影响,对于一个特殊点 x,考虑找到它前/后第一个值比它大的特殊点、前/后第一个值比它大的普通点。前者可以每次询问时单调栈,后者可以在每次加入特殊点时二分一次。这样每个特殊点影响了哪些区间就知道了。

然后使用 P3246 的线性做法,即可支持 O(n) 预处理,O(1) 查询一个区间的所有子区间的最大值之和。查询时,先求出不加特殊点的答案,再加上每个特殊点的影响即可。

时间复杂度 O(n \sqrt n),最慢点在 1 秒左右。

#include<bits/stdc++.h>
#define int unsigned
using namespace std;
const int B=500;
int n,m,a[200105],bcnt;
struct node {
    int x,y,pre,nex;
} b[1005];
struct ST {
    pair<int,int> s[12][3205],pre[200105],suf[200105];
    unsigned long long h[200105];
    void build() {
        for(int i=0; i<=(n>>6); i++) {
            int l=i<<6, r=l+63;
            pre[l]={a[l],l};
            for(int j=l+1; j<=r; j++) pre[j]=max(pre[j-1],{a[j],j});
            s[0][i]=pre[r];
            suf[r]={a[r],r};
            for(int j=r-1; j>=max(l,1u); j--) suf[j]=max(suf[j+1],{a[j],j});
            unsigned long long x=0;
            for(int j=l; j<=r; j++) {
                while(x) {
                    int top=63-__builtin_clzll(x);
                    if(a[l+top]<=a[j]) x^=(1llu<<top);
                    else break;
                }
                x|=(1llu<<j-l), h[j]=x;
            }
        }
        for(int i=1; i<=11; i++) {
            if(signed(n>>6)-(1<<i)+1<0) break;
            for(int j=0; j<=(n>>6)-(1<<i)+1; j++) s[i][j]=max(s[i-1][j],s[i-1][j+(1<<i-1)]);
        }
    }
    int query(int l, int r) {
        int lb=l>>6, rb=r>>6;
        if(lb==rb) return l+__builtin_ctzll(h[r]>>(l&63));
        pair<int,int> res=max(suf[l],pre[r]);
        if(lb+1!=rb) {
            lb++, rb--;
            int lg=__lg(rb-lb+1);
            res=max(res,max(s[lg][lb],s[lg][rb-(1<<lg)+1]));
        }
        return res.second;
    }
} t;
void insert(int x, int y) {
    int i=1;
    while(i<=bcnt && b[i].x<x) i++;
    if(i<=bcnt && b[i].x==x) b[i].y+=y;
    else {
        ++bcnt;
        for(int j=bcnt; j>i; j--) b[j]=b[j-1];
        b[i].x=x, b[i].y=a[x]+y;
    }
    int l=1, r=x-1;
    while(l<=r) {
        int mid=l+r>>1;
        if(a[t.query(mid,x)]>b[i].y) l=mid+1;
        else r=mid-1;
    }
    b[i].pre=r;
    l=x+1, r=n;
    while(l<=r) {
        int mid=l+r>>1;
        if(a[t.query(x,mid)]>b[i].y) r=mid-1;
        else l=mid+1;
    }
    b[i].nex=l;
}
int st[200005],top,pre[200005],nex[200005],f[200005],g[200005],fs[200005],gs[200005];
void build()  {
    top=0;
    for(int i=1; i<=n; i++) {
        while(top && a[st[top]]<=a[i]) top--;
        pre[i]=st[top], st[++top]=i;
    }
    top=0;
    for(int i=n; i>=1; i--) {
        while(top && a[st[top]]<a[i]) top--;
        nex[i]=(top?st[top]:n+1), st[++top]=i;
    }
    for(int i=1; i<=n; i++) f[i]=f[pre[i]]+a[i]*(i-pre[i]);
    for(int i=n; i>=1; i--) g[i]=g[nex[i]]+a[i]*(nex[i]-i);
    for(int i=1; i<=n; i++) fs[i]=fs[i-1]+f[i], gs[i]=gs[i-1]+g[i];
    t.build();
}
int qraw(int l, int r) {
    if(l>r) return 0;
    int p=t.query(l,r);
    return (fs[r]-fs[p])-f[p]*(r-p)+(gs[p-1]-gs[l-1])-g[p]*(p-l)+(p-l+1)*(r-p+1)*a[p];
}
int bp[200005],bn[200005];
int query(int l, int r) {
    int res=qraw(l,r);
    int bl=1, br=bcnt;
    while(bl<=br && b[bl].x<l) bl++;
    while(bl<=br && b[br].x>r) br--;
    if(bl>br) return res;
    top=0;
    for(int i=bl; i<=br; i++) {
        while(top && b[st[top]].y<=b[i].y) top--;
        bp[i]=b[st[top]].x, st[++top]=i;
    }
    top=0;
    for(int i=br; i>=bl; i--) {
        while(top && b[st[top]].y<b[i].y) top--;
        bn[i]=(top?b[st[top]].x:n+1), st[++top]=i;
    }
    for(int i=bl; i<=br; i++) {
        bp[i]=max({bp[i],b[i].pre,l-1}), bn[i]=min({bn[i],b[i].nex,r+1});
        res-=qraw(bp[i]+1,bn[i]-1)-qraw(bp[i]+1,b[i].x-1)-qraw(b[i].x+1,bn[i]-1);
        res+=b[i].y*(b[i].x-bp[i])*(bn[i]-b[i].x);
    }
    return res;
}
signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin>>n>>m;
    for(int i=1; i<=n; i++) cin>>a[i];
    build();
    int lst=0;
    for(int i=1; i<=m; i++) {
        int op,x,y;
        cin>>op>>x>>y;
        if(op==1) {
            x^=lst;
            insert(x,y);
            if(bcnt>B) {
                for(int j=1; j<=bcnt; j++) a[b[j].x]=b[j].y;
                bcnt=0;
                build();
            }
        } else {
            x^=lst, y^=lst;
            lst=query(x,y), cout<<lst<<'\n';
        }
    }
    return 0;
}