#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
struct SegTree {
vector<int> seg;
int MAX;
SegTree(int N) {
int i;
for(i=1;i<2*N;i*=2) {}
seg.resize(i);
MAX = i;
for(i=0;i<MAX;i++) seg[i]=INF;
}
void update(int n) {
n += MAX/2;
n /= 2;
while(n) {
seg[n] = min(seg[2*n],seg[2*n+1]);
n /= 2;
}
}
void Edit(int n, int a) {
seg[n+MAX/2] = min(seg[n+MAX/2],a);
update(n);
}
int sum(int s, int e, int n, int ns, int ne) {
if(e<=ns||ne<=s) return INF;
if(s<=ns&&ne<=e) return seg[n];
int mid = ns + ne >> 1;
return min(sum(s,e,2*n,ns,mid),sum(s,e,2*n+1,mid,ne));
}
int sum(int s, int e) {
return sum(s,e,1,0,MAX/2);
}
};
vector<int> idx;
int id(int n) {
return lower_bound(idx.begin(),idx.end(),n)-idx.begin();
}
int A[100005];
int B[100005];
int C[100005];
int D[100005];
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, M;
cin >> N >> M;
int i, j;
for(i=0;i<N;i++) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
idx.push_back(A[i]);
idx.push_back(B[i]);
idx.push_back(C[i]);
}
if(M==1) {
cout << 0;
return 0;
}
idx.push_back(1);
idx.push_back(M);
sort(idx.begin(),idx.end());
idx.erase(unique(idx.begin(),idx.end()),idx.end());
SegTree tree1(idx.size()+5),tree2(idx.size()+5);
tree1.Edit(id(1),0);
tree2.Edit(id(M),0);
int ans = INF;
for(i=0;i<N;i++) {
int l = id(A[i]);
int r = id(B[i]);
int po = id(C[i]);
int c1 = tree1.sum(l, r+1);
tree1.Edit(po,c1+D[i]);
int c2 = tree2.sum(l, r+1);
tree2.Edit(po,c2+D[i]);
ans = min(ans, c1+c2+D[i]);
}
cout << (ans == INF ? -1 : ans);
}
Compilation message
pinball.cpp: In member function 'long long int SegTree::sum(long long int, long long int, long long int, long long int, long long int)':
pinball.cpp:30:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | int mid = ns + ne >> 1;
| ~~~^~~~
pinball.cpp: In function 'int main()':
pinball.cpp:51:12: warning: unused variable 'j' [-Wunused-variable]
51 | int i, j;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
532 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
472 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
532 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
472 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
15 ms |
2264 KB |
Output is correct |
26 |
Correct |
45 ms |
4784 KB |
Output is correct |
27 |
Correct |
136 ms |
10936 KB |
Output is correct |
28 |
Correct |
114 ms |
10840 KB |
Output is correct |
29 |
Correct |
93 ms |
9256 KB |
Output is correct |
30 |
Correct |
145 ms |
10832 KB |
Output is correct |
31 |
Correct |
218 ms |
17948 KB |
Output is correct |
32 |
Correct |
187 ms |
13856 KB |
Output is correct |
33 |
Correct |
23 ms |
4172 KB |
Output is correct |
34 |
Correct |
93 ms |
13360 KB |
Output is correct |
35 |
Correct |
135 ms |
26240 KB |
Output is correct |
36 |
Correct |
245 ms |
26224 KB |
Output is correct |
37 |
Correct |
210 ms |
26240 KB |
Output is correct |
38 |
Correct |
195 ms |
26220 KB |
Output is correct |