Submission #955117

#TimeUsernameProblemLanguageResultExecution timeMemory
955117LCJLYConstellation 3 (JOI20_constellation3)C++14
100 / 100
674 ms144764 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int arr[200005];
int n;

//fw
int fw[200005];
void upd(int x, int y){
	while(x<200005){
		fw[x]+=y;
		x+=x&(-x);
	}
}

int sum(int x){
	int counter=0;
	while(x>0){
		counter+=fw[x];
		x-=x&(-x);
	}
	return counter;
}

void rangeUpd(int x, int y, int c){
	if(x>y) return;
	//show3(x,x,y,y,c,c);
	upd(x,c);
	upd(y+1,-c);
}

//sparse table
struct SparseTable{
	vector<vector<pii>>st;
	int n,k;
	void init(int nn, int arr[]){
		n=nn;
		k=MSB(n);
		st.resize(k);
		for(int x=0;x<n;x++){
			st[0].push_back({arr[x],x});
		}
		for(int x=1;x<k;x++){
			for(int y=0;y+(1<<x)<=n;y++){
				st[x].push_back(min(st[x-1][y],st[x-1][y+(1<<(x-1))]));
			}
		}
	}
	
	inline int MSB(unsigned int x){ return 32-__builtin_clz(x);}
	
	pii query(int x, int y){
		int k=MSB(y-x+1)-1;
		return min(st[k][x],st[k][y-(1<<k)+1]);
	}
};

//cartesian tree
vector<int>adj[200005];
int two[22][200005];
SparseTable st;
int rt=0;
int ptr=0;
int in[400005];
int out[400005];

void dnc(int l, int r, int par){
	pii hold=st.query(l,r);
	//show2(l,l,r,r);
	in[hold.second]=++ptr;
	
	if(par!=-1){
		adj[par].push_back(hold.second);
		adj[hold.second].push_back(par);
		two[0][hold.second]=par;
	}
	else{
		rt=hold.second;
	}
	
	for(int x=0;x<20;x++){
		two[x+1][hold.second]=two[x][two[x][hold.second]];
	}
	if(hold.second!=l){
		dnc(l,hold.second-1,hold.second);
	}
	if(hold.second!=r){
		dnc(hold.second+1,r,hold.second);
	}
	out[hold.second]=ptr;
	//show3(hold.second,hold.second,in[hold.second],in[hold.second],out[hold.second],out[hold.second]);
}

vector<pii>star[200005];
void add(int index, int h, int cost){
	int hold=index;
	for(int x=19;x>=0;x--){
		int nxt=two[x][index];
		if(arr[nxt]>=h){
			index=nxt;
		}
	}
	star[index].push_back({hold,cost});
}

int dp[200005];

void dfs(int index, int par){
	for(auto it:adj[index]){
		if(it==par) continue;
		dfs(it,index);
	}
	//show(index,index);
	int counter=0;
	for(auto it:adj[index]){
		if(it==par) continue;
		counter+=dp[it];
		//show(it,it);
	}
	//for(int y=1;y<=n;y++){
		//cout << sum(y) << " ";
	//}
	//cout << endl;
	rangeUpd(in[index],out[index],counter);
	//show2(in[index],in[index],out[index],out[index]);
	//for(int y=1;y<=n;y++){
		//cout << sum(y) << " ";
	//}
	//cout << endl;
	//show3(l,in[index],r,out[index],counter,counter);
	dp[index]=counter;
	//show(counter,counter);
	for(auto it:star[index]){
		//show2(it.first,in[it.first],it.second,it.second);
		//show(val,sum(in[it.first])-sum2(in[it.first]));
		//show(val2,sum2(in[it.first]));
		//show(in[it.first],in[it.first]);
		dp[index]=max(dp[index],sum(in[it.first])+it.second);
	}
	
	rangeUpd(in[index],out[index],-dp[index]);
	//show3(l,in[index],r,out[index],dp[index],dp[index]);
	//show2(index,index,dp[index],dp[index]);
}

void solve(){
	cin >> n;
	for(int x=1;x<=n;x++){
		cin >> arr[x];
		arr[x]=n-arr[x];
	}
	
	st.init(n+1,arr);
	dnc(1,n,-1);
	
	int temp,temp2,temp3;
	int q;
	cin >> q;
	
	int counter=0;
	for(int x=0;x<q;x++){
		cin >> temp >> temp2 >> temp3;
		temp2=n-temp2+1;
		add(temp,temp2,temp3);
		counter+=temp3;
	}
	
	dfs(rt,-1);
	cout << counter-dp[rt];
}	
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...