#include<bits/stdc++.h>
#define el '\n'
#define fi first
#define sc second
#define int ll
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
const int mod=1e9+7;
const int N=1e5+11;
struct node
{
node* l;
node* r;
int val;
node()
{
l=r=NULL;
val=1e18;
}
};
void update(node* pos, int l, int r, int u, int v, int val)
{
if(l>v||r<u) return;
if(u<=l && r<=v)
{
pos->val=min(pos->val, val);
return;
}
if(pos->l==NULL) pos->l = new node();
if(pos->r==NULL) pos->r = new node();
int mid=(l+r)>>1;
update(pos->l, l, mid, u, v, val);
update(pos->r, mid+1, r, u, v, val);
pos->val=min(pos->l->val, pos->r->val);
}
int get(node* pos, int l, int r, int u, int v)
{
if(l>v||r<u) return 1e18;
if(u<=l && r<=v)
{
return pos->val;
}
if(pos->l==NULL) pos->l = new node();
if(pos->r==NULL) pos->r = new node();
int mid=(l+r)>>1;
return min(get(pos->l, l, mid, u, v), get(pos->r, mid+1, r, u, v));
}
int m, n, a, b, c, d;
void sol()
{
cin >> m >> n;
node* root1 = new node();
node* root2 = new node();
int ans=1e18;
for(int i=1;i<=m;i++)
{
cin >> a >> b >> c >> d;
int l=0, r=0;
if(a!=1) l=get(root1, 1, n, a, b);
if(b!=n) r=get(root2, 1, n, a, b);
ans=min(ans, l+r+d);
update(root1, 1, n, c, c, l+d);
update(root2, 1, n, c, c, r+d);
}
if(ans==1e18) cout << -1;
else cout << ans;
}
signed main()
{
// freopen("divisor.INP", "r", stdin);
// freopen("divisor.OUT", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--)
{
sol();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
704 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
2 ms |
1628 KB |
Output is correct |
13 |
Correct |
2 ms |
1744 KB |
Output is correct |
14 |
Correct |
2 ms |
1828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
704 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
2 ms |
1628 KB |
Output is correct |
13 |
Correct |
2 ms |
1744 KB |
Output is correct |
14 |
Correct |
2 ms |
1828 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1116 KB |
Output is correct |
17 |
Correct |
4 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
600 KB |
Output is correct |
19 |
Correct |
7 ms |
6016 KB |
Output is correct |
20 |
Correct |
4 ms |
1884 KB |
Output is correct |
21 |
Correct |
2 ms |
1772 KB |
Output is correct |
22 |
Correct |
7 ms |
5980 KB |
Output is correct |
23 |
Correct |
7 ms |
6748 KB |
Output is correct |
24 |
Correct |
12 ms |
6844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
704 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
2 ms |
1628 KB |
Output is correct |
13 |
Correct |
2 ms |
1744 KB |
Output is correct |
14 |
Correct |
2 ms |
1828 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1116 KB |
Output is correct |
17 |
Correct |
4 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
600 KB |
Output is correct |
19 |
Correct |
7 ms |
6016 KB |
Output is correct |
20 |
Correct |
4 ms |
1884 KB |
Output is correct |
21 |
Correct |
2 ms |
1772 KB |
Output is correct |
22 |
Correct |
7 ms |
5980 KB |
Output is correct |
23 |
Correct |
7 ms |
6748 KB |
Output is correct |
24 |
Correct |
12 ms |
6844 KB |
Output is correct |
25 |
Correct |
25 ms |
10332 KB |
Output is correct |
26 |
Correct |
117 ms |
55212 KB |
Output is correct |
27 |
Correct |
326 ms |
95136 KB |
Output is correct |
28 |
Correct |
168 ms |
5836 KB |
Output is correct |
29 |
Correct |
282 ms |
113032 KB |
Output is correct |
30 |
Correct |
248 ms |
23636 KB |
Output is correct |
31 |
Correct |
582 ms |
204584 KB |
Output is correct |
32 |
Correct |
489 ms |
138736 KB |
Output is correct |
33 |
Correct |
67 ms |
47296 KB |
Output is correct |
34 |
Correct |
316 ms |
198480 KB |
Output is correct |
35 |
Correct |
496 ms |
393112 KB |
Output is correct |
36 |
Correct |
696 ms |
402000 KB |
Output is correct |
37 |
Correct |
663 ms |
399184 KB |
Output is correct |
38 |
Correct |
666 ms |
397904 KB |
Output is correct |