题解:P5654 基础函数练习题

· · 题解

题目传送门

没看懂另外几篇题解,写个比较暴力的做法。

看到 F(l,r) 的定义中有 \max,考虑建出排列 p 的大根笛卡尔树,查询 F(l,r) 需要知道 F(l,m-1) 和 F(m+1,r),容易发现这个就是笛卡尔树上一个结点左子树的后缀 F 值和右子树前缀 F 值,因此可以考虑在笛卡尔树上的每个结点维护前缀和后缀的 F 值。

观察一下 F 的式子。

F(l, r) = \begin{cases} \max(F(l, m - 1), F(m + 1, r)) + w_m & , l \le r \\ 0 & , l > r \end{cases}

以维护后缀为例,发现把 F(i,m-1) 变为 F(i,r) 就是把它变成 \max(F(i,m-1)+w_m,F(m+1,r)+w_m),维护前缀同理。

发现需要支持区间加,区间对一个数取 \max,单点查值,上两个线段树合并,一个维护前缀,一个维护后缀即可。

Code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
namespace fast_IO{
    #define IOSIZE 100000
        char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
    #define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
    #define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
    #define isdigit(ch) (ch>47&&ch<58)
    #define isspace(ch) (ch<33)
        template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w;}
        template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true;}
        template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48);}
        inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
        inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true;}
        inline void print(char x) { putchar(x); }
        inline void print(char *x) { while (*x) putchar(*x++); }
        inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
        inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true;}
        inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]);}
        inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true;}
        inline void print(bool b) { putchar(b+48);}
        template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...);}
        template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...);}
        struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout);}} io;
        template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io;}
        template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io;}
    #define cout io
    #define cin io
}using namespace fast_IO;
const int N=5e5+10,inf=2e9;
int n,m,w[N],p[N],tot,rt,cnt;
ll ans[N];
struct sgt{
    int lson,rson,ma;
}t2[2*N];
inline void build(int &u,int l,int r){
    u=++cnt;
    if(l==r){
      t2[u].ma=l;
      return;
    }
    int mid=(l+r)>>1;
    build(t2[u].lson,l,mid);
    build(t2[u].rson,mid+1,r);
    if(p[t2[t2[u].lson].ma]>p[t2[t2[u].rson].ma])t2[u].ma=t2[t2[u].lson].ma;
    else t2[u].ma=t2[t2[u].rson].ma;
}
inline int query(int u,int l,int r,int ql,int qr){
    if(ql<=l&&r<=qr)return t2[u].ma;
    int mid=(l+r)>>1;
    if(qr<=mid)return query(t2[u].lson,l,mid,ql,qr);
    else if(ql>mid)return query(t2[u].rson,mid+1,r,ql,qr);
    else{
      int v1=query(t2[u].lson,l,mid,ql,qr),v2=query(t2[u].rson,mid+1,r,ql,qr);
      return (p[v1]>p[v2])?v1:v2;
    }
}
vector<int>rub;
struct tree{
    int lson,rson;
    ll ma,lazy,lazy2;
}t[38*N];
inline int New(){
    if(!rub.size())return ++tot;
    else{
      int x=rub[rub.size()-1];
      rub.pop_back();
      return x;
    }
}
inline void work(int u,ll val){
    t[u].ma+=val;
    t[u].lazy+=val;
    if(t[u].lazy2!=inf)t[u].lazy2+=val;
}
inline void work2(int u,ll val){
    if(t[u].lazy2!=inf)t[u].lazy2=max(t[u].lazy2,val);
    else t[u].lazy2=val;
    t[u].ma=max(t[u].ma,val);
}
inline void push_down(int u){
    if(t[u].lson)work(t[u].lson,t[u].lazy);
    if(t[u].rson)work(t[u].rson,t[u].lazy);
    t[u].lazy=0;
    if(t[u].lson&&t[u].lazy2!=inf)work2(t[u].lson,t[u].lazy2);
    if(t[u].rson&&t[u].lazy2!=inf)work2(t[u].rson,t[u].lazy2);
    t[u].lazy2=inf;
}
inline void push_up(int u){
    t[u].ma=max(t[t[u].lson].ma,t[t[u].rson].ma);
}
inline void update2(int &u,int l,int r,int ql,int qr,ll val){
    if(ql>qr||ql<1||qr>n)return;
    if(!u){
      u=New();
      t[u].lazy2=inf;
    }
    if(ql<=l&&r<=qr){
      work2(u,val);
      return;
    }
    push_down(u);
    int mid=(l+r)>>1;
    if(ql<=mid)update2(t[u].lson,l,mid,ql,qr,val);
    if(qr>mid)update2(t[u].rson,mid+1,r,ql,qr,val);
    push_up(u);
}
inline void update(int &u,int l,int r,int ql,int qr,ll val){
    if(ql>qr||ql<1||qr>n)return;
    if(!u){
      u=New();
      t[u].lazy2=inf;
    }
    if(ql<=l&&r<=qr){
      work(u,val);
      return;
    }
    push_down(u);
    int mid=(l+r)>>1;
    if(ql<=mid)update(t[u].lson,l,mid,ql,qr,val);
    if(qr>mid)update(t[u].rson,mid+1,r,ql,qr,val);
    push_up(u);
}
inline ll query_tree(int u,int l,int r,int x){
    if(x<1||x>n)return -1e18;
    if(l==r)return t[u].ma;
    push_down(u);
    int mid=(l+r)>>1;
    if(x<=mid)return query_tree(t[u].lson,l,mid,x);
    else return query_tree(t[u].rson,mid+1,r,x);
}
#define pb push_back
#define pii  pair<int,int> 
#define mk make_pair
inline int merge(int a,int b){
    if(!a||!b)return a|b;
    push_down(a),push_down(b);
    t[a].ma=max(t[a].ma,t[b].ma);
    t[a].lson=merge(t[a].lson,t[b].lson);
    t[a].rson=merge(t[a].rson,t[b].rson);
    rub.pb(b);
    t[b].lson=t[b].rson=t[b].lazy=t[b].lazy2=t[b].ma=0;
    return a;
}
struct node{
    int l,r,id;
};
vector<node>v[N];
inline pii solve(int l,int r){
    if(l>r)return mk(0,0);
    int pos=query(rt,1,n,l,r);
    pii lson=solve(l,pos-1),rson=solve(pos+1,r);
    int root1=0,root2=0;
    for(node p:v[pos])ans[p.id]=max(query_tree(lson.second,1,n,p.l),query_tree(rson.first,1,n,p.r))+w[pos];

    if(l==1&&r==n)return mk(0,0);

    update(root2,1,n,pos,pos,w[pos]+max(0ll,query_tree(rson.second,1,n,pos+1)));
    if(lson.second)update2(lson.second,1,n,l,pos-1,query_tree(rson.second,1,n,pos+1));
    if(lson.second)update(lson.second,1,n,l,pos-1,w[pos]);
    root2=merge(merge(lson.second,rson.second),root2);

    update(root1,1,n,pos,pos,w[pos]+max(0ll,query_tree(lson.first,1,n,pos-1)));
    if(rson.first)update2(rson.first,1,n,pos+1,r,query_tree(lson.first,1,n,pos-1));
    if(rson.first)update(rson.first,1,n,pos+1,r,w[pos]);
    root1=merge(merge(lson.first,rson.first),root1);

    return mk(root1,root2);
}
signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;++i)cin>>p[i];
    for(int i=1;i<=n;++i)cin>>w[i];
    build(rt,1,n);
    int l,r;
    for(int i=1;i<=m;++i){
      cin>>l>>r;
      v[query(rt,1,n,l,r)].pb({l,r,i});
    }
    solve(1,n);
    for(int i=1;i<=m;++i)cout<<ans[i]<<"\n";
    return 0;
}