#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
const ll INF = 1e16;
struct SegTree {
struct Node {
Node *st, *dr;
ll mn;
Node() {
st = dr = NULL;
mn = INF;
}
};
Node *root;
inline void init() {
root = new Node;
}
void update(Node *nod, int left, int right, int pos, ll val) {
nod -> mn = min(nod -> mn, val);
if(left < right) {
int mid = (left + right) / 2;
if(pos <= mid) {
if(nod -> st == NULL) {
nod -> st = new Node;
}
update(nod -> st, left, mid, pos, val);
}
else {
if(nod -> dr == NULL) {
nod -> dr = new Node;
}
update(nod -> dr, mid + 1, right, pos, val);
}
}
}
void update(int left, int right, int pos, ll val) {
update(root, left, right, pos, val);
}
ll query(Node *nod, int left, int right, int l, int r) {
if(nod == NULL) {
return INF;
}
if(l <= left && right <= r) {
return nod -> mn;
}
else {
int mid = (left + right) / 2;
ll ans = INF;
ans = min(query(nod -> st, left, mid, l, r), query(nod -> dr, mid + 1, right, l, r));
return ans;
}
}
ll query(int left, int right, int l, int r) {
return query(root, left, right, l, r);
}
};
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, n, m;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> m >> n;
SegTree st[3];
for(i = 1; i <= 2; i++) {
st[i].init();
}
vector < vector <ll> > dp(m + 1, vector<ll>(4, INF));
for(i = 1; i <= m; i++) {
int l, r, pos;
ll cst;
cin >> l >> r >> pos >> cst;
if(l == 1) {
dp[i][1] = min(dp[i][1], cst);
}
if(r == n) {
dp[i][2] = min(dp[i][2], cst);
}
if(l == 1 && r == n) {
dp[i][3] = min(dp[i][3], cst);
}
dp[i][1] = min(dp[i][1], cst + st[1].query(1, n, l, r));
dp[i][2] = min(dp[i][2], cst + st[2].query(1, n, l, r));
dp[i][3] = min(dp[i][3], cst + st[1].query(1, n, l, r) + st[2].query(1, n, l, r));
dp[i][3] = min(dp[i][3], dp[i][1] + dp[i][2] - cst);
for(int j = 1; j <= 2; j++) {
st[j].update(1, n, pos, dp[i][j]);
}
}
ll ans = INF;
for(i = 1; i <= m; i++) {
ans = min(ans, dp[i][3]);
}
if(ans == INF) {
cout << -1;
return 0;
}
cout << ans;
//cin.close();
//cout.close();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
7 ms |
512 KB |
Output is correct |
12 |
Correct |
19 ms |
512 KB |
Output is correct |
13 |
Correct |
19 ms |
640 KB |
Output is correct |
14 |
Correct |
14 ms |
684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
7 ms |
512 KB |
Output is correct |
12 |
Correct |
19 ms |
512 KB |
Output is correct |
13 |
Correct |
19 ms |
640 KB |
Output is correct |
14 |
Correct |
14 ms |
684 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
11 ms |
512 KB |
Output is correct |
17 |
Correct |
217 ms |
1384 KB |
Output is correct |
18 |
Correct |
15 ms |
524 KB |
Output is correct |
19 |
Correct |
330 ms |
1680 KB |
Output is correct |
20 |
Correct |
169 ms |
1188 KB |
Output is correct |
21 |
Correct |
52 ms |
748 KB |
Output is correct |
22 |
Correct |
501 ms |
1572 KB |
Output is correct |
23 |
Correct |
403 ms |
1784 KB |
Output is correct |
24 |
Correct |
539 ms |
1824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
7 ms |
512 KB |
Output is correct |
12 |
Correct |
19 ms |
512 KB |
Output is correct |
13 |
Correct |
19 ms |
640 KB |
Output is correct |
14 |
Correct |
14 ms |
684 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
11 ms |
512 KB |
Output is correct |
17 |
Correct |
217 ms |
1384 KB |
Output is correct |
18 |
Correct |
15 ms |
524 KB |
Output is correct |
19 |
Correct |
330 ms |
1680 KB |
Output is correct |
20 |
Correct |
169 ms |
1188 KB |
Output is correct |
21 |
Correct |
52 ms |
748 KB |
Output is correct |
22 |
Correct |
501 ms |
1572 KB |
Output is correct |
23 |
Correct |
403 ms |
1784 KB |
Output is correct |
24 |
Correct |
539 ms |
1824 KB |
Output is correct |
25 |
Execution timed out |
1067 ms |
2716 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |