// IN THE NAME OF ALLAH
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define wall cerr << "------------------------------------" << endl
#define pb push_back
#define F first
#define S second
#define all(x) x.begin() , x.end()
#define int ll
#define kids int tm = (tl + tr)>>1; int cl = v<<1; int cr = v<<1|1
#define test int n; cin >> n; while(n--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#pragma GCC optimize("Ofast")
typedef long long ll;
typedef pair<int , int> pii;
typedef pair<ll , ll> pll;
const int N = 3e5 + 10;
const int K = 2e3+10;
const ll mod = 1e9+7;
const ll INF = 1e18+10;
const int P = 31;
const int lg = 25;
int n;
struct segment {
int st[N << 2];
void init(int v , int tl , int tr) {
st[v] = INF;
if(tl == tr)
return;
kids;
init(cl , tl , tm);
init(cr , tm+1 , tr);
}
void upd(int v , int tl , int tr , int pos , int x) {
if(tl == tr) {
st[v] = min(st[v] , x);
return;
}
kids;
if(pos <= tm)
upd(cl , tl , tm , pos , x);
else
upd(cr , tm+1 , tr , pos , x);
st[v] = min(st[cl] , st[cr]);
}
int ask(int v , int tl , int tr , int l , int r) {
if(tl == l && tr == r)
return st[v];
if(l > r)
return INF;
kids;
// cout << v << " " << min(ask(cl , tl , tm , l , min(tm , r)) , ask(cr , tm+1 , tr , max(l , tm+1) , r)) << "\n";
return min(ask(cl , tl , tm , l , min(tm , r)) , ask(cr , tm+1 , tr , max(l , tm+1) , r));
}
} seg , ges;
int a[N] , b[N] , c[N] , d[N];
void solve() {
vector<int> vec;
int n , m;
cin >> n >> m;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
vec.pb(a[i]); vec.pb(b[i]); vec.pb(c[i]);
}
vec.pb(1); vec.pb(m);
sort(all(vec)); vec.resize(unique(all(vec)) - vec.begin());
m = vec.size();
seg.init(1 , 1 , m); ges.init(1 , 1 , m);
seg.upd(1 , 1 , m , 1 , 0);
ges.upd(1 , 1 , m , m , 0);
int ans = INF;
for(int i = 1 ; i <= n ; i++) {
int x = seg.ask(1 , 1 , m , lower_bound(all(vec) , a[i]) - vec.begin() + 1 , lower_bound(all(vec) , b[i]) - vec.begin() + 1);
seg.upd(1 , 1 , m , lower_bound(all(vec) , c[i]) - vec.begin() + 1 , x+d[i]);
int y = ges.ask(1 , 1 , m , lower_bound(all(vec) , a[i]) - vec.begin() + 1 , lower_bound(all(vec) , b[i]) - vec.begin() + 1);
ges.upd(1 , 1 , m , lower_bound(all(vec) , c[i]) - vec.begin() + 1 , y+d[i]);
ans = min(ans , x + y + d[i]);
}
if(ans >= INF) {
cout << "-1\n";
return;
}
cout << ans << "\n";
}
signed main()
{
solve();
}
// IT'S EASY TO SEE
// CODE = LOVE
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10692 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10692 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10696 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10692 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10696 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
4 ms |
10700 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
4 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10692 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10584 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10696 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
4 ms |
10700 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
4 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10588 KB |
Output is correct |
25 |
Correct |
23 ms |
11224 KB |
Output is correct |
26 |
Correct |
70 ms |
16400 KB |
Output is correct |
27 |
Correct |
232 ms |
21180 KB |
Output is correct |
28 |
Correct |
197 ms |
19328 KB |
Output is correct |
29 |
Correct |
130 ms |
20216 KB |
Output is correct |
30 |
Correct |
245 ms |
20344 KB |
Output is correct |
31 |
Correct |
274 ms |
27368 KB |
Output is correct |
32 |
Correct |
275 ms |
24828 KB |
Output is correct |
33 |
Correct |
39 ms |
14020 KB |
Output is correct |
34 |
Correct |
128 ms |
24260 KB |
Output is correct |
35 |
Correct |
204 ms |
34080 KB |
Output is correct |
36 |
Correct |
306 ms |
34228 KB |
Output is correct |
37 |
Correct |
259 ms |
35172 KB |
Output is correct |
38 |
Correct |
251 ms |
33636 KB |
Output is correct |