#include<bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
// #define fisier 1
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const double dancila = 3.14159265359; // PI
const double eps = 1e-9;
int n, m;
int a[100002], b[100002], c[100002], d[100002];
bool lc[100002], rc[100002];
ll aint[400002], dp[2][100002];
void update(int nod, int st, int dr, int poz, ll val)
{
if(st == dr)
{
aint[nod] = min(aint[nod], val);
return;
}
int mid = (st + dr) / 2;
if(poz <= mid)
update(nod << 1, st, mid, poz, val);
else
update(nod << 1|1, mid+1, dr, poz, val);
aint[nod] = min(aint[nod << 1], aint[nod << 1|1]);
}
ll query(int nod, int st, int dr, int L, int R)
{
if(dr < L || st > R)
return (1LL<<60);
if(L <= st && dr <= R)
return aint[nod];
int mid = (st + dr) / 2;
return min(query(nod << 1, st, mid, L, R), query(nod << 1|1, mid+1, dr, L, R));
}
int main()
{
#ifdef fisier
ifstream f("input.in");
ofstream g("output.out");
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> m >> n;
set<int> dist;
for(int i = 1; i <= m; ++i)
{
cin >> a[i] >> b[i] >> c[i] >> d[i];
if(a[i] == 1)
lc[i] = 1;
if(b[i] == n)
rc[i] = 1;
dist.insert(c[i]);
}
map<int, int> tpoz;
int p = 0;
for (int i : dist)
tpoz[i] = ++p;
n = 0;
for(int i = 1; i <= m; ++i)
{
a[i] = (*tpoz.lower_bound(a[i])).se;
auto it = tpoz.upper_bound(b[i]);
it--;
b[i] = (*it).se;
c[i] = tpoz[c[i]];
n = max(n, b[i]);
}
for(int i = 0; i <= 1; ++i)
{
for(int nod = 1; nod <= 4*n; ++nod)
aint[nod] = (1LL<<60);
for(int j = 1; j <= m; ++j)
{
if((i == 0 && lc[j]) || (i == 1 && rc[j]))
dp[i][j] = d[j];
else
dp[i][j] = min((1LL<<60), query(1, 1, n, a[j], b[j]) + d[j]);
update(1, 1, n, c[j], dp[i][j]);
}
}
ll ans = (1LL<<60);
for(int i = 1; i <= m; ++i)
ans = min(ans, dp[0][i] + dp[1][i] - d[i]);
if(ans == (1LL<<60))
cout << -1;
else
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
384 KB |
Output is correct |
19 |
Correct |
8 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
7 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
384 KB |
Output is correct |
19 |
Correct |
8 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
7 ms |
512 KB |
Output is correct |
25 |
Correct |
31 ms |
1528 KB |
Output is correct |
26 |
Correct |
67 ms |
3748 KB |
Output is correct |
27 |
Correct |
184 ms |
8184 KB |
Output is correct |
28 |
Correct |
144 ms |
3832 KB |
Output is correct |
29 |
Correct |
140 ms |
6520 KB |
Output is correct |
30 |
Correct |
189 ms |
4760 KB |
Output is correct |
31 |
Correct |
296 ms |
12412 KB |
Output is correct |
32 |
Correct |
267 ms |
10488 KB |
Output is correct |
33 |
Correct |
38 ms |
3448 KB |
Output is correct |
34 |
Correct |
141 ms |
8184 KB |
Output is correct |
35 |
Correct |
192 ms |
15992 KB |
Output is correct |
36 |
Correct |
313 ms |
15968 KB |
Output is correct |
37 |
Correct |
260 ms |
15968 KB |
Output is correct |
38 |
Correct |
305 ms |
15992 KB |
Output is correct |