#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;
const long long inf=1e18;
struct flight{
	int from,to;
	int l,r,cost;
	inline friend bool operator < (flight fr,flight sc){
		return fr.l<sc.l;
	}
}s[MAXN];
struct meal{
	int l,r;
}w[MAXN];
int n,m,k,cnt;
long long cost[MAXN],dp[MAXN];
priority_queue< pair<int,long long> , vector< pair<int,long long> > , greater< pair<int,long long> > > endf[MAXN];
vector<int> vals,z[5*MAXN];
unordered_map<int,int> mp;
struct PST{
	struct node{
		int l,r;
		int val;
	};
	int top[5*MAXN],num;
	node tree[100*MAXN];
	int update(int v,int l,int r,int pos){
		if(l==r){
			num++; 
			tree[num].val=tree[v].val+1;
			return num;
		}else{
			int tt=(l+r)/2;
			int ll=tree[v].l,rr=tree[v].r;
			if(pos<=tt){
				ll=update(tree[v].l,l,tt,pos);
			}else{
				rr=update(tree[v].r,tt+1,r,pos);
			}
			num++;
			tree[num]={ll,rr,0};
			tree[num].val=tree[tree[num].l].val + tree[tree[num].r].val;
			return num;
		}
	}
	int getdiff(int v1,int v2,int l,int r,int ll,int rr){
		if(ll>rr)return 0;
		if(l==ll and r==rr)return tree[v2].val-tree[v1].val;
		int tt=(l+r)/2;
		return getdiff(tree[v1].l,tree[v2].l,l,tt,ll,min(tt,rr)) + getdiff(tree[v1].r,tree[v2].r,tt+1,r,max(tt+1,ll),rr);
	}
}seg;
long long meals(int l,int r){
	if(l>r)return 0;
	return seg.getdiff(seg.top[l-1],seg.top[r],1,cnt,l,r);
}
struct CHT{
	struct state{
		long long bias;
		int l,from;
	};
	vector<state> v;
	long long coef;
	bool better(state a,state b,int x){
		if(x<a.l)return false;
		return a.bias + meals(a.l,x)*coef < b.bias + meals(b.l,x)*coef;
	}
	int intersect(state a,state b){
		int l=max(a.l,b.l)-1,r=cnt,tt;
		while(l+1<r){
			tt=(l+r)/2;
			if(a.bias + meals(a.l,tt)*coef <= b.bias + meals(b.l,tt)*coef)l=tt;
			else r=tt;
		}
		return r;
	}
	void add(state s){
		if(!v.empty() and v.back().bias + meals(v.back().l,cnt)*coef <= s.bias + meals(s.l,cnt)*coef)return;
		while(!v.empty() and better(s,v.back(),v.back().from))v.pop_back();
		if(v.empty())v.push_back({s.bias,s.l,s.l});
		else v.push_back({s.bias,s.l,intersect(v.back(),s)});
	}
	long long query(int x){
		if(v.empty() or x<v[0].l)return inf;
		int l=0,r=v.size(),tt;
		while(l+1<r){
			tt=(l+r)/2;
			if(v[tt].from<=x){
				l=tt;
			}else{
				r=tt;
			}
		}
		return v[l].bias + meals(v[l].l,x) * coef;
	}
}cht[MAXN];
long long solve(int N, int M, int W, vector<int> T,vector<int> X, vector<int> Y,vector<int> A, vector<int> B, vector<int> C,vector<int> L,vector<int> R){
	n=N; m=M; k=W;
	vals.push_back(0);
	for(int i=1;i<=m;i++){
		vals.push_back(A[i-1]-1);
		vals.push_back(A[i-1]);
		vals.push_back(B[i-1]);
	}
	for(int i=1;i<=k;i++){
		vals.push_back(L[i-1]);
		vals.push_back(R[i-1]);
	}
	sort(vals.begin(),vals.end());
	for(int i=0;i<vals.size();i++){
		if(i==0 or vals[i]!=vals[i-1])cnt++;
		mp[vals[i]]=cnt;
	}
	for(int i=1;i<=m;i++){
		A[i-1]=mp[A[i-1]];
		B[i-1]=mp[B[i-1]];
	}
	for(int i=1;i<=k;i++){
		L[i-1]=mp[L[i-1]];
		R[i-1]=mp[R[i-1]];
	}
	for(int i=1;i<=n;i++){
		cost[i]=T[i-1];
		cht[i].coef=cost[i];
	}
	for(int i=1;i<=m;i++){
		s[i]={X[i-1]+1,Y[i-1]+1,A[i-1],B[i-1],C[i-1]};
	}
	for(int i=1;i<=k;i++){
		z[L[i-1]].push_back(R[i-1]);
	}
	for(int i=1;i<=cnt;i++){
		int curr=seg.top[i-1];
		for(int f:z[i]){
			seg.top[i]=seg.update(curr,1,cnt,f);
			curr=seg.top[i];
		}
		seg.top[i]=curr;
	}
	sort(s+1,s+m+1);
	cht[1].add({0,1,1});
	for(int i=1;i<=m;i++){
		dp[i]=inf;
		while(!endf[s[i].from].empty() and endf[s[i].from].top().first<=s[i].l){
			
			auto curr=endf[s[i].from].top();
			cht[s[i].from].add({curr.second,curr.first,1});
			endf[s[i].from].pop();
		}
		long long mindp=cht[s[i].from].query(s[i].l-1);
		dp[i]=mindp+s[i].cost;
		endf[s[i].to].push({s[i].r,dp[i]});
	}
	long long ans=inf;
	for(int i=1;i<=m;i++){
		if(s[i].to!=n)continue;
		ans=min(ans , dp[i] + meals(s[i].r+1,cnt) * cost[n]);
	}
	if(ans>=inf)return -1;
	return ans;
}   
/*int main(){
	//cout<<solve(3, 3, 1, {20, 30, 40}, {0, 1, 0}, {1, 2, 2},
	//	{1, 20, 18}, {15, 30, 40}, {10, 5, 40}, {16}, {19})<<"\n";	   
	cout<<solve(3, 5, 6, {30, 38, 33}, {0, 1, 0, 0, 1}, {2, 0, 1, 2, 2},
		{12, 48, 26, 6, 49}, {16, 50, 28, 7, 54}, {38, 6, 23, 94, 50},
		{32, 14, 42, 37, 2, 4}, {36, 14, 45, 40, 5, 5})<<"\n";
	return 0;
}*/
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |