제출 #1360582

#제출 시각아이디문제언어결과실행 시간메모리
1360582nikolashami별자리 3 (JOI20_constellation3)C++20
100 / 100
260 ms88784 KiB
#include<bits/stdc++.h>
using namespace std;
using ll=long long;

const ll N=2e5+555;
ll a[N],n;
ll st[N][20];
ll lc[N],rc[N];
array<ll,2>rn[N];
ll up[N][20];

ll _lg(ll x){
	return(63-__builtin_clzll(x));
}

ll gmax(ll l,ll r){
	ll len=_lg(r-l+1);
	ll p1=st[l][len];
	ll p2=st[r-(1<<len)+1][len];
	return(a[p1]>a[p2]?p1:p2);
}

void construct(ll l,ll r,ll p){
	rn[p]={l,r};
	if(l==r)return;
	if(l<p){
		lc[p]=gmax(l,p-1);
		construct(l,p-1,lc[p]);
	}
	if(p<r){
		rc[p]=gmax(p+1,r);
		construct(p+1,r,rc[p]);
	}
}

ll fw[N];

void add(ll k,ll x){
	k+=20;
	while(k<N){
		fw[k]+=x;
		k+=k&-k;
	}
}

void upd(ll l,ll r,ll x){
	add(l,x);
	add(r+1,-x);
}

ll qry(ll k){
	ll r=0;
	k+=20;
	while(k){
		r+=fw[k];
		k-=k&-k;
	}
	return r;
}

vector<array<ll,2>>qu[N];
ll f[N];

void dfs(ll u){
	if(!u)return;
	dfs(lc[u]);
	dfs(rc[u]);
	f[u]=f[lc[u]]+f[rc[u]];
	for(auto[v,w]:qu[u])
		f[u]=max(f[u],qry(v)+w+f[lc[u]]+f[rc[u]]);
	upd(rn[u][0],rn[u][1],f[lc[u]]+f[rc[u]]-f[u]);
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;++i)
    	cin>>a[i];

    for(int i=1;i<=n;++i)
    	st[i][0]=i;

    for(int j=1;j<20;++j){
    	for(int i=1;i+(1<<j)-1<=n;++i){
    		ll p1=st[i][j-1];
    		ll p2=st[i+(1<<(j-1))][j-1];
    		st[i][j]=(a[p1]>a[p2]?p1:p2);
    	}
    }

    construct(1,n,gmax(1,n));
    for(int i=1;i<=n;++i){
    	if(lc[i])up[lc[i]][0]=i;
    	if(rc[i])up[rc[i]][0]=i;
    }

    for(int j=1;j<20;++j){
    	for(int i=1;i<=n;++i)
    		up[i][j]=up[up[i][j-1]][j-1];
    }

    ll ans=0;
    ll q;cin>>q;
    while(q--){
    	ll u,h,w;
    	cin>>u>>h>>w;
    	ll ou=u;
    	for(int j=19;j>=0;--j)
    		if(up[u][j]&&a[up[u][j]]<h)u=up[u][j];
    	ans+=w;
    	qu[u].push_back({ou,w});
    }

    ll root=gmax(1,n);
    dfs(root);

    cout<<ans-f[root];
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…