#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
constexpr int NMAX = 1e5 + 5;
constexpr int VALMAX = 3 * NMAX + 5;
constexpr LL INF = 1e18;
int N, M;
int A[NMAX], B[NMAX], C[NMAX];
LL cost[NMAX];
LL AINT[4*VALMAX];
int vf = 0;
LL BestLeft[NMAX];
LL BestRight[NMAX];
void Read () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
for (int i = 1; i <= N; ++ i )
cin >> A[i] >> B[i] >> C[i] >> cost[i];
}
void Normalize () {
map <int, int> mp;
for (int i = 1; i <= N; ++ i ) {
mp[A[i]] = 1;
mp[B[i]] = 1;
mp[C[i]] = 1;
}
mp[1] = 1;
mp[M] = 1;
int vf = 0;
for (auto &it : mp)
it.second = ++vf;
for (int i = 1; i <= N; ++ i ) {
A[i] = mp[A[i]];
B[i] = mp[B[i]];
C[i] = mp[C[i]];
}
M = mp[M];
}
void ResetTree (int nod, int a, int b) {
AINT[nod] = INF;
if (a == b) return;
int mij = (a + b) / 2;
ResetTree(2*nod, a, mij);
ResetTree(2*nod+1, mij+1, b);
}
void Update (int nod, int a, int b, int poz, LL val) {
if (a == b) {
AINT[nod] = min(AINT[nod], val);
return;
}
int mij = (a + b) / 2;
if (poz <= mij) Update(2*nod, a, mij, poz, val);
else Update(2*nod+1, mij+1, b, poz, val);
AINT[nod] = min(AINT[2*nod], AINT[2*nod+1]);
}
LL Query (int nod, int a, int b, int qa, int qb) {
if (qa <= a && b <= qb) return AINT[nod];
int mij = (a + b) / 2;
LL ans_left = INF, ans_right = INF;
if (qa <= mij) ans_left = Query(2*nod, a, mij, qa, qb);
if (mij < qb) ans_right = Query(2*nod+1, mij+1, b, qa, qb);
return min(ans_left, ans_right);
}
void Solve_Left () {
ResetTree(1, 1, M);
Update(1, 1, M, 1, 0);
for (int i = 1; i <= N; ++ i ) {
BestLeft[i] = Query(1, 1, M, A[i], B[i]);
Update(1, 1, M, C[i], cost[i] + BestLeft[i]);
}
}
void Solve_Right () {
ResetTree(1, 1, M);
Update(1, 1, M, M, 0);
for (int i = 1; i <= N; ++ i ) {
BestRight[i] = Query(1, 1, M, A[i], B[i]);
Update(1, 1, M, C[i], cost[i] + BestRight[i]);
}
}
void Solve () {
Solve_Left();
Solve_Right();
LL ans = INF;
for (int i = 1; i <= N; ++ i )
ans = min(ans, BestLeft[i] + BestRight[i] + cost[i]);
cout << (ans == INF ? -1 : ans) << '\n';
}
int main()
{
Read();
Normalize();
Solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
456 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
456 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
20 ms |
2168 KB |
Output is correct |
26 |
Correct |
65 ms |
5752 KB |
Output is correct |
27 |
Correct |
197 ms |
12068 KB |
Output is correct |
28 |
Correct |
145 ms |
7652 KB |
Output is correct |
29 |
Correct |
124 ms |
10296 KB |
Output is correct |
30 |
Correct |
180 ms |
9028 KB |
Output is correct |
31 |
Correct |
295 ms |
19752 KB |
Output is correct |
32 |
Correct |
249 ms |
15404 KB |
Output is correct |
33 |
Correct |
35 ms |
5444 KB |
Output is correct |
34 |
Correct |
144 ms |
15188 KB |
Output is correct |
35 |
Correct |
181 ms |
29888 KB |
Output is correct |
36 |
Correct |
388 ms |
29892 KB |
Output is correct |
37 |
Correct |
321 ms |
29892 KB |
Output is correct |
38 |
Correct |
294 ms |
29892 KB |
Output is correct |