This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
 
#define N 100005
#define MOD 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
struct Node{
	Node *l = NULL, *r = NULL;
	ll val = INF;
	int L, R;
	Node(int lll, int rrr){
		L = lll;
		R = rrr;
	}
	void update(int x, ll t){
		if(t > INF) return;
		if(L == R){
			val = min(val, t);
			return;
		}
		int M = (L + R) >> 1;
		if(M >= x){
			if(l == NULL)
				l = new Node(L, M);
			l -> update(x, t);
		} else {
			if(r == NULL)
				r = new Node(M + 1, R);
			r -> update(x, t);
		}
		ll v1, v2;
		if(l == NULL)
			v1 = INF;
		else
			v1 = l -> val;
		if(r == NULL)
			v2 = INF;
		else
			v2 = r -> val;
		
		val = min(v1, v2);
	}
	ll query(int LL, int RR){
		if(LL <= L && R <= RR)
			return val;
		if(LL > R || L > RR)
			return INF;
		ll v1, v2;
		if(l == NULL)
			v1 = INF;
		else
			v1 = l -> query(LL, RR);
		if(r == NULL)
			v2 = INF;
		else 
			v2 = r -> query(LL, RR);
		return min(v1, v2);
	}
} *a = NULL, *b = NULL;
ll ans = INF;
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
 
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	int m, n;
	cin>>m>>n;
	a = new Node(1, n);
	b = new Node(1, n);
	a -> update(1, 0);
	b -> update(n, 0);
	for(int i = 1; i <= m; i++){
		int A, B, C, D;
		cin>>A>>B>>C>>D;
		ll av = a -> query(A, B);
		ll bv = b -> query(A, B);
		ans = min(ans, av + bv + D);
		a -> update(C, av + D);
		b -> update(C, bv + D);
	}
	if(ans == INF)
		cout<<-1;
	else
		cout<<ans;
}
| # | 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... |