제출 #720311

#제출 시각아이디문제언어결과실행 시간메모리
720311kymTreatment Project (JOI20_treatment)C++14
100 / 100
1078 ms287384 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{"  << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach 
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first 
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 100005;
map<int,set<pi>> S0;
map<int,set<pi>> S1;
struct node {
	int s,e,m;
	pi val;
	node *l=nullptr, *r=nullptr;
	node (int _s, int _e) {
		s=_s;e=_e;m=(s+e)/2;
		val={oo,0};
	}
	void update(int x, pi nval) {
		if(s==e) {
			val=nval;
			return;
		}
		if(x>m){
			if(r==nullptr)r=new node(m+1,e);
			r->update(x,nval);
		} else {
			if(l==nullptr)l=new node(s,m);
			l->update(x,nval);
		}
		val=min(l?l->val:pi(oo,0),r?r->val:pi(oo,0));
	}
	pi query(int x, int y) {
		if(s==x&&e==y)return val;
		if(x>m) {
			if(r==nullptr)r=new node(m+1,e);
			return r->query(x,y);
		} else if(y<=m) {
			if(l==nullptr)l=new node(s,m);
			return l->query(x,y);
		} else {
			if(l==nullptr)l=new node(s,m);
			if(r==nullptr)r=new node(m+1,e);
			return min(l->query(x,m),r->query(m+1,y));
		}
	}
} *roots[2];
int T[MAXN];
int L[MAXN];
int R[MAXN];
int C[MAXN];

int dist[MAXN];
int32_t main() 
{
	IAMSPEED
	int n,m;
	roots[0]=new node(0,inf);
	roots[1]=new node(0,inf);
	cin >> n >> m;
	priority_queue<pi,vector<pi>,greater<pi>> pq;
	FOR(i,1,m)dist[i]=oo;
	FOR(i,1,m){
		cin >> T[i] >> L[i] >> R[i] >> C[i];
		++R[i];
		if(L[i]==1) {
			dist[i]=C[i];
			pq.push({C[i],i});
		}
	}
	FOR(i,1,m) {
		if(L[i]==1)continue;
		int plus=L[i]+T[i];
		S0[T[i]].insert({plus,i});
		int minus=L[i]-T[i];
		S1[T[i]].insert({minus,i});
	}
	
	for(auto t:S0) {
		roots[0]->update(t.f,*t.s.begin());
	
	}
	for(auto t:S1) {
		roots[1]->update(t.f,*t.s.begin());
	}
	
	while(pq.size()) {
		pi cur=pq.top(); pq.pop();
		int x=cur.s;
		int d=cur.f;
		db3(x,d,dist[x]);
		if(dist[x]!=d)continue;

		if(R[x]==n+1){
			cout<<d<<'\n';exit(0);
		}

		for(pi res=roots[0]->query(T[x],inf);res.f!=oo;res=roots[0]->query(T[x],inf)){
			int val = res.f;
			int idx = res.s;
			

			if(val<=R[x]+T[x]){
				db2(idx,val); 
				dist[idx]=dist[x]+C[idx];
				pq.push({dist[idx],idx});

				S0[T[idx]].erase({L[idx]+T[idx],idx});
				if(S0[T[idx]].size())roots[0]->update(T[idx],*S0[T[idx]].begin());
				else roots[0]->update(T[idx],{oo,oo});

				S1[T[idx]].erase({L[idx]-T[idx],idx});
				if(S1[T[idx]].size())roots[1]->update(T[idx],*S1[T[idx]].begin());
				else roots[1]->update(T[idx],{oo,oo});

			} else {
				break;
			}
		}
		reach

		for(pi res=roots[1]->query(0,T[x]);res.f!=oo;res=roots[1]->query(0,T[x])){
			int val = res.f;
			int idx = res.s;
			

			if(val<=R[x]-T[x]){
				db2(idx,val);
				dist[idx]=dist[x]+C[idx];
				pq.push({dist[idx],idx});

				S0[T[idx]].erase({L[idx]+T[idx],idx});
				if(S0[T[idx]].size())roots[0]->update(T[idx],*S0[T[idx]].begin());
				else roots[0]->update(T[idx],{oo,oo});

				S1[T[idx]].erase({L[idx]-T[idx],idx});
				if(S1[T[idx]].size())roots[1]->update(T[idx],*S1[T[idx]].begin());
				else roots[1]->update(T[idx],{oo,oo});
				
			} else {
				break;
			}
		}
	}
	cout<<-1<<'\n';
	
	return (0-0) ; 
}



#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...