1 solutions

  • 0
    @ 2026-7-29 10:49:39
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无 BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
    */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         998244353
    #define int         long long
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<endl;return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    mt19937 sd(random_device{}());
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    int exgcd(int x,int y,int &a,int &b){
    	if(y==0){
    		a=1;b=0;
    		return x;
    	}
    	int d=exgcd(y,x%y,a,b);
    	int z=b;
    	b=a-b*(x/y);
    	a=z;
    	return d;
    }
    int _x_,_y_;
    inline int inverse(int x,int y=MOD){
    	exgcd(x,y,_x_,_y_);
    	return (_x_+y)%y;
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    int n,N,m,q;
    vector<int>t,a;
    int b[20000005];
    int s[20000005];
    int pid[20000005];
    int id[400005];
    int ban[400005],S[400005];
    int s2[400005],pr[400005];
    int f[400005];
    bitset<20000005>vis;
    vector<tuple<int,int,int>>qr[400005];
    struct ds{
    	struct node{
    		int x,y;
    		int l,r;
    		int ask(int z){return -s[x+z]+y;}
    	};
    	vector<node>seg[1600005];
    	void init(){
    		REP(i,0,N*4)seg[i].clear();
    	}
    	int cmp(int x,int y,int X,int Y){
    		int l=max(-x,-X),r=min(m-x,m-X);
    		if(l>r)return -m-1;
    		int res=r+1;
    		while(l<=r){
    			int mid=(l+r)>>1;
    			if(-s[x+mid]+y<=-s[X+mid]+Y)res=mid,r=mid-1;
    			else l=mid+1;
    		}
    		return res;
    	}
    	void add(int x,int y,int p){
    		while(seg[p].size()){
    			int r=seg[p].back().r;
    			if(r+x<=m&&seg[p].back().ask(r)<=-s[x+r]+y)seg[p].pop_back();
    			else break;
    		}
    		if(!seg[p].size()){
    			seg[p].pb({x,y,-x,m-x});
    			return;
    		}
    		auto [X,Y,l,r]=seg[p].back();
    		int t=cmp(x,y,X,Y);
    		if(t==-m-1){
    			seg[p].pb({x,y,-x,m-x});
    			return;
    		}
    		seg[p].pop_back();
    		seg[p].pb({X,Y,t,r});seg[p].pb({x,y,-x,t-1});
    	}
    	void update(int pos,int l,int r,int p,int x,int y){
    		add(x,y,p);
    		if(l==r)return;
    		int m=(l+r)>>1;
    		if(m>=pos)update(pos,l,m,p*2+1,x,y);
    		else update(pos,m+1,r,p*2+2,x,y);
    	}
    	int ask(int x,int p){
    		if(!seg[p].size())return -1e18;
    		int l=0,r=seg[p].size()-1,res=-1;
    		while(l<=r){
    			int mid=(l+r)>>1;
    			if(seg[p][mid].l<=x)res=mid,r=mid-1;
    			else l=mid+1;
    		}
    		if(res==-1)return -1e18;
    		else return seg[p][res].ask(x);
    	}
    	void ask(int l,int r,int s,int t,int p,int &res,int x){
    		if(l<=s&&t<=r){
    			res=max(res,ask(x,p));
    			return;
    		}
    		int m=(s+t)>>1;
    		if(m>=l)ask(l,r,s,m,p*2+1,res,x);
    		if(m<r)ask(l,r,m+1,t,p*2+2,res,x);
    	}
    }T;
    int ask(int x,int y){return s[id[x]]-(id[x]>=y? s[id[x]-y]:0);}
    int ask2(int x,int y,int z){return s[x]-s[max(x-y,z)];}
    void Main(){
    	cin>>n;
    //	cin>>n>>N>>q;
    	t.pb(-1);a.pb(-1);
    	REP(i,0,n){
    		int x;cin>>x;
    		if(x!=-1){
    			t.pb(i);a.pb(x-1);
    			pid[x-1]=++N;vis[x-1]=1;
    		}
    	}
    //	REP(i,0,N){
    //		int x,y;
    //		cin>>x>>y;
    //		--x,--y;
    //		t.pb(x);a.pb(y);
    //		pid[y]=i+1;
    //		vis[y]=1;
    //	}
    	REP(i,0,n)cin>>b[i];
    	int x=0,mx=-1,lst=0;
    	REP(i,1,N+1){
    		REP(j,lst,t[i]){
    			while(vis[x])++x;
    			mx=max(mx,x);++x;
    		}
    		if(mx<a[i])mx=a[i];else ban[i]=1;lst=t[i]+1;
    	}
    	vector<pii>tval;
    	q=n;
    	REP(i,0,n){
    		if(vis[i]){
    			tval.pb({i,m});
    			id[pid[i]]=m;
    		}else{
    			s[m+1]=s[m]+b[i];
    			++m;
    		}
    	}
    	REP(i,0,n){
    		int x,y;x=i+1;y=n;
    //		cin>>x>>y;
    		--x,--y;
    		qr[upper_bound(all(t),x)-t.begin()-1].pb({x,y,i});
    	}
    	++N;
    	REP(i,1,N)s2[i]=s2[i-1]+t[i]-t[i-1]-1;
    	vector<int>St(N+1,0);int tp=1;
    	REP(i,1,N)S[i]=id[i]-s2[i];
    	St[0]=0;
    	T.init();
    	mx=0;
    	vector<int>ret(q,-1);
    	REP(i,0,N){
    		if(i)mx=max(mx,a[i]);
    		if(i&&!ban[i]){
    			while(tp>1&&S[i]>=S[St[tp-1]])--tp;
    			pr[i]=St[tp-1];St[tp++]=i;
    			int j=pr[i];
    			f[i]=ask(i,t[j]==-1? s2[i]:id[i]-id[j])+(t[j]==-1? 0:f[j])-s[id[i]];
    			if(j+1<N)T.ask(j+1,N-1,0,N-1,0,f[i],id[i]-s2[i]);
    			f[i]+=b[a[i]]+s[id[i]];
    			T.update(i,0,N-1,0,s2[i],f[i]);
    		}
    		for(auto [x,y,qid]:qr[i]){
    			if(mx>y)continue;
    			int as2=s2[i]+x-t[i],aid=0;
    			auto it=lower_bound(all(tval),pii{y+1,-1});
    			if(it==tval.begin()){
    				aid=y+1;
    			}else{
    				--it;
    				aid=(it->second)+y-(it->first);
    			}
    			if(aid<as2)continue;
    //			cout<<x<<' '<<y<<' '<<qid<<' '<<aid<<' '<<as2<<endl;
    			int l=1,r=tp-1,res=0;
    			while(l<=r){
    				int mid=(l+r)>>1;
    				if(S[St[mid]]>aid-as2)res=mid,l=mid+1;
    				else r=mid-1;
    			}
    			res=St[res];
    			int ans=res==0? ask2(aid,as2,0):ask2(aid,as2-s2[res],id[res])+f[res];
    //			cout<<res<<' '<<ans<<endl;
    			ans-=s[aid];T.ask(res+1,N-1,0,N-1,0,ans,aid-as2);
    //			cout<<'?'<<res+1<<' '<<aid-as2<<' '<<ans<<endl;
    			ans+=s[aid];
    			ret[qid]=ans;
    		}
    	}
    	for(auto i:ret)cout<<i<<' ';
    	cout<<'\n';
    }
    void TC() {
        int tc=1;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {	freopen("noip.in","r",stdin);
    	freopen("noip.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    
    • 1

    Information

    ID
    3057
    Time
    2000ms
    Memory
    1024MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    1
    Accepted
    1
    Uploaded By