#include<bits/stdc++.h>
using namespace std;
const long long INF = LLONG_MAX;
struct AINT_node{
int l,r;
long long val;
AINT_node *ls,*rs;
AINT_node(int _l,int _r){
l = _l;
r= _r;
val = INF;
ls = rs = NULL;
}
void merg(){
val = INF;
if(rs!=NULL){
val = min(val, rs->val);
}
if(ls!=NULL){
val = min(val, ls->val);
}
}
};
void add(AINT_node *nod, int poz, long long val){
if(nod->l == nod->r){
nod->val =min(nod->val,val);
return;
}
int mid = (nod->l + nod->r) / 2;
if(poz <= mid){
if(nod->ls == NULL){
nod->ls = new AINT_node(nod->l,mid);
}
add(nod->ls,poz,val);
}
else{
if(nod->rs == NULL){
nod->rs = new AINT_node(mid+1, nod->r);
}
add(nod->rs,poz,val);
}
(*nod).merg();
}
long long query(AINT_node *nod,int ql,int qr){
if(nod->r < ql || nod->l > qr){
return INF;
}
if(ql <= nod->l && nod->r <=qr)
return nod->val;
long long lrsp, rrsp;
if(nod->ls != NULL)
lrsp = query(nod->ls, ql, qr);
else
lrsp = INF;
if(nod -> rs !=NULL)
rrsp = query(nod->rs , ql, qr);
else
rrsp = INF;
return min(lrsp,rrsp);
}
const int M_MAX = 100005;
int main()
{
//freopen(".in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int m,n;
cin>>m>>n;
AINT_node *st = new AINT_node(1,n);
AINT_node *dr = new AINT_node(1,n);
long long ans = INF;
for(int i=1;i<=m;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
long long dpst,dpdr;
if(a == 1)
dpst = 0;
else
dpst = query(st, a, b);
if(b == n)
dpdr = 0;
else
dpdr = query(dr, a, b);
if(dpst != INF)
dpst += d;
if(dpdr != INF)
dpdr += d;
if(dpst != INF && dpdr != INF)
ans = min(ans, dpst + dpdr - d);
if(dpst != INF)
add(st, c, dpst);
if(dpdr != INF)
add(dr, c, dpdr);
}
if(ans == INF){
cout<<-1;
return 0;
}
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
1536 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
3 ms |
1280 KB |
Output is correct |
20 |
Correct |
4 ms |
1280 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
768 KB |
Output is correct |
23 |
Correct |
3 ms |
1280 KB |
Output is correct |
24 |
Correct |
2 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
1536 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
3 ms |
1280 KB |
Output is correct |
20 |
Correct |
4 ms |
1280 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
768 KB |
Output is correct |
23 |
Correct |
3 ms |
1280 KB |
Output is correct |
24 |
Correct |
2 ms |
1024 KB |
Output is correct |
25 |
Correct |
26 ms |
5376 KB |
Output is correct |
26 |
Correct |
94 ms |
20984 KB |
Output is correct |
27 |
Correct |
355 ms |
47988 KB |
Output is correct |
28 |
Correct |
289 ms |
2680 KB |
Output is correct |
29 |
Correct |
199 ms |
41208 KB |
Output is correct |
30 |
Correct |
477 ms |
16436 KB |
Output is correct |
31 |
Correct |
629 ms |
95736 KB |
Output is correct |
32 |
Correct |
596 ms |
76280 KB |
Output is correct |
33 |
Correct |
29 ms |
8440 KB |
Output is correct |
34 |
Correct |
76 ms |
18028 KB |
Output is correct |
35 |
Correct |
181 ms |
62840 KB |
Output is correct |
36 |
Correct |
376 ms |
77304 KB |
Output is correct |
37 |
Correct |
162 ms |
36520 KB |
Output is correct |
38 |
Correct |
164 ms |
38392 KB |
Output is correct |