#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 |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 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 |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 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 |
512 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 |
6 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 |
640 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 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 |
512 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 |
6 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 |
640 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
29 ms |
1916 KB |
Output is correct |
26 |
Correct |
63 ms |
4732 KB |
Output is correct |
27 |
Correct |
175 ms |
10616 KB |
Output is correct |
28 |
Correct |
140 ms |
7520 KB |
Output is correct |
29 |
Correct |
128 ms |
8568 KB |
Output is correct |
30 |
Correct |
189 ms |
8700 KB |
Output is correct |
31 |
Correct |
275 ms |
16248 KB |
Output is correct |
32 |
Correct |
269 ms |
14328 KB |
Output is correct |
33 |
Correct |
40 ms |
4216 KB |
Output is correct |
34 |
Correct |
128 ms |
9976 KB |
Output is correct |
35 |
Correct |
197 ms |
19836 KB |
Output is correct |
36 |
Correct |
302 ms |
20088 KB |
Output is correct |
37 |
Correct |
274 ms |
19832 KB |
Output is correct |
38 |
Correct |
260 ms |
19832 KB |
Output is correct |