#include<bits/stdc++.h>
using namespace std;
#define int long long
#define in pair<int, int>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define INF (int)1e17
#define MX (int)3e5+5
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)
struct segment_tree
{
vector<int> tree;
void init(int n)
{
tree.assign(4*n+69, INF);
return;
}
void upd(int pos, int va, int id, int l, int r)
{
if(l == r)
{
tree[id] = min(tree[id], va);
return;
}
int m = (l+r)/2;
if(pos <= m)
upd(pos, va, 2*id, l, m);
else
upd(pos, va, 2*id+1, m+1, r);
tree[id] = min(tree[2*id], tree[2*id+1]);
return;
}
int val(int ql, int qr, int id, int l, int r)
{
if(r < ql || qr < l)
return INF;
if(ql <= l && r <= qr)
return tree[id];
int m = (l+r)/2;
int s1 = val(ql, qr, 2*id, l, m);
int s2 = val(ql, qr, 2*id+1, m+1, r);
return min(s1, s2);
}
};
vector<int> chungus;
int compress(int x)
{
return lower_bound(chungus.begin(), chungus.end(), x) - chungus.begin();
}
signed main()
{
fast();
segment_tree work_l, work_r;
int n, N;
cin >> n >> N;
work_l.init(3*n+1);
work_r.init(3*n+1);
vector<int> v;
v.pb(1);
v.pb(N);
vector<int> a(n+1), b(n+1), c(n+1), d(n+1);
for(int i = 1; i <= n; i++)
{
cin >> a[i] >> b[i] >> c[i] >> d[i];
v.pb(a[i]);
v.pb(b[i]);
v.pb(c[i]);
}
chungus.pb(-INF);
sort(v.begin(), v.end());
for(auto x: v)
{
if(x!=chungus.back())
chungus.pb(x);
}
for(int i = 1; i <= n; i++)
{
a[i] = compress(a[i]);
b[i] = compress(b[i]);
c[i] = compress(c[i]);
}
int L = compress(1);
int R = compress(N);
vector<int> dp_l, dp_r;
dp_l.assign(n+1, INF);
dp_r.assign(n+1, INF);
for(int i = 1; i <= n; i++)
{
if(a[i] == L)
dp_l[i] = d[i];
else
dp_l[i] = d[i] + work_l.val(a[i], b[i], 1, L, R);
dp_l[i] = min(dp_l[i], INF);
work_l.upd(c[i], dp_l[i], 1, L, R);
}
for(int i = 1; i <= n; i++)
{
if(b[i] == R)
dp_r[i] = d[i];
else
dp_r[i] = d[i] + work_r.val(a[i], b[i], 1, L, R);
work_r.upd(c[i], dp_r[i], 1, L, R);
}
int ans = INF;
for(int i = 1; i <= n; i++)
ans = min(ans, dp_l[i]+dp_r[i]-d[i]);
ans++; ans%=(INF+1); ans--;
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
612 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
612 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
612 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
612 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
808 KB |
Output is correct |
25 |
Correct |
14 ms |
3544 KB |
Output is correct |
26 |
Correct |
40 ms |
8372 KB |
Output is correct |
27 |
Correct |
120 ms |
22220 KB |
Output is correct |
28 |
Correct |
112 ms |
31016 KB |
Output is correct |
29 |
Correct |
83 ms |
16276 KB |
Output is correct |
30 |
Correct |
148 ms |
32396 KB |
Output is correct |
31 |
Correct |
174 ms |
31936 KB |
Output is correct |
32 |
Correct |
167 ms |
32452 KB |
Output is correct |
33 |
Correct |
23 ms |
6748 KB |
Output is correct |
34 |
Correct |
82 ms |
17604 KB |
Output is correct |
35 |
Correct |
114 ms |
35784 KB |
Output is correct |
36 |
Correct |
183 ms |
36800 KB |
Output is correct |
37 |
Correct |
187 ms |
36800 KB |
Output is correct |
38 |
Correct |
155 ms |
35268 KB |
Output is correct |