#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5+10;
const long long inf = 2e18+10;
typedef long long ll;
int ind, m;
int l[maxn], r[maxn], p[maxn], d[maxn];
ll tree[4*maxn];
ll dp[2][maxn];
map<int, int> mp;
void compress(int n)
{
for (int i = 1; i <= n; i++)
mp[l[i]] = mp[r[i]] = mp[p[i]] = 0;
mp[1] = 0;
mp[m] = 0;
for (auto &x: mp)
x.second = ++ind;
for (int i = 1; i <= n; i++)
l[i] = mp[l[i]], r[i] = mp[r[i]], p[i] = mp[p[i]];
m = mp[m];
}
void init(void)
{
for (int i = 0; i < 4*maxn; i++)
tree[i] = inf;
}
void upd(int node, int l, int r, int pos, ll v)
{
if (l == r)
{
tree[node] = min(tree[node], v);
return;
}
int mid = (l+r)>>1;
if (pos <= mid) upd(2*node, l, mid, pos, v);
else upd(2*node+1, mid+1, r, pos, v);
tree[node] = min(tree[2*node], tree[2*node+1]);
}
ll query(int node, int l, int r, int a, int b)
{
if (l > b || r < a) return inf;
if (l >= a && r <= b) return tree[node];
int mid = (l+r)>>1;
return min(query(2*node, l, mid, a, b), query(2*node+1, mid+1, r, a, b));
}
int main(void)
{
int n;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d %d %d %d", &l[i], &r[i], &p[i], &d[i]);
dp[0][i] = dp[1][i] = inf;
}
compress(n);
init();
for (int i = 1; i <= n; i++)
{
if (l[i] == 1) dp[0][i] = d[i];
else dp[0][i] = 1ll*d[i] + query(1, 1, ind, l[i], r[i]);
upd(1, 1, ind, p[i], dp[0][i]);
}
init();
for (int i = 1; i <= n; i++)
{
if (r[i] == m) dp[1][i] = d[i];
else dp[1][i] = 1ll*d[i] + query(1, 1, ind, l[i], r[i]);
upd(1, 1, ind, p[i], dp[1][i]);
}
ll ans = inf;
for (int i = 1; i <= n; i++)
if (dp[0][i] < inf && dp[1][i] < inf)
ans = min(ans, dp[0][i]+dp[1][i]-1ll*d[i]);
if (ans == inf) printf("-1\n");
else printf("%lld\n", ans);
}
Compilation message
pinball.cpp: In function 'int main()':
pinball.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
pinball.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d", &l[i], &r[i], &p[i], &d[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
13 ms |
9600 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
11 ms |
9728 KB |
Output is correct |
6 |
Correct |
11 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
11 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
13 ms |
9600 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
11 ms |
9728 KB |
Output is correct |
6 |
Correct |
11 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
11 ms |
9728 KB |
Output is correct |
9 |
Correct |
11 ms |
9728 KB |
Output is correct |
10 |
Correct |
12 ms |
9728 KB |
Output is correct |
11 |
Correct |
13 ms |
9728 KB |
Output is correct |
12 |
Correct |
12 ms |
9856 KB |
Output is correct |
13 |
Correct |
11 ms |
9856 KB |
Output is correct |
14 |
Correct |
11 ms |
9856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
13 ms |
9600 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
11 ms |
9728 KB |
Output is correct |
6 |
Correct |
11 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
11 ms |
9728 KB |
Output is correct |
9 |
Correct |
11 ms |
9728 KB |
Output is correct |
10 |
Correct |
12 ms |
9728 KB |
Output is correct |
11 |
Correct |
13 ms |
9728 KB |
Output is correct |
12 |
Correct |
12 ms |
9856 KB |
Output is correct |
13 |
Correct |
11 ms |
9856 KB |
Output is correct |
14 |
Correct |
11 ms |
9856 KB |
Output is correct |
15 |
Correct |
11 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9856 KB |
Output is correct |
17 |
Correct |
12 ms |
9856 KB |
Output is correct |
18 |
Correct |
11 ms |
9856 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
12 ms |
9856 KB |
Output is correct |
21 |
Correct |
11 ms |
9856 KB |
Output is correct |
22 |
Correct |
12 ms |
9856 KB |
Output is correct |
23 |
Correct |
12 ms |
9856 KB |
Output is correct |
24 |
Correct |
12 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
13 ms |
9600 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
11 ms |
9728 KB |
Output is correct |
6 |
Correct |
11 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
11 ms |
9728 KB |
Output is correct |
9 |
Correct |
11 ms |
9728 KB |
Output is correct |
10 |
Correct |
12 ms |
9728 KB |
Output is correct |
11 |
Correct |
13 ms |
9728 KB |
Output is correct |
12 |
Correct |
12 ms |
9856 KB |
Output is correct |
13 |
Correct |
11 ms |
9856 KB |
Output is correct |
14 |
Correct |
11 ms |
9856 KB |
Output is correct |
15 |
Correct |
11 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9856 KB |
Output is correct |
17 |
Correct |
12 ms |
9856 KB |
Output is correct |
18 |
Correct |
11 ms |
9856 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
12 ms |
9856 KB |
Output is correct |
21 |
Correct |
11 ms |
9856 KB |
Output is correct |
22 |
Correct |
12 ms |
9856 KB |
Output is correct |
23 |
Correct |
12 ms |
9856 KB |
Output is correct |
24 |
Correct |
12 ms |
9984 KB |
Output is correct |
25 |
Correct |
35 ms |
10880 KB |
Output is correct |
26 |
Correct |
94 ms |
14072 KB |
Output is correct |
27 |
Correct |
303 ms |
19236 KB |
Output is correct |
28 |
Correct |
187 ms |
16888 KB |
Output is correct |
29 |
Correct |
202 ms |
17524 KB |
Output is correct |
30 |
Correct |
262 ms |
17660 KB |
Output is correct |
31 |
Correct |
547 ms |
24684 KB |
Output is correct |
32 |
Correct |
436 ms |
22648 KB |
Output is correct |
33 |
Correct |
60 ms |
13816 KB |
Output is correct |
34 |
Correct |
280 ms |
20444 KB |
Output is correct |
35 |
Correct |
347 ms |
30840 KB |
Output is correct |
36 |
Correct |
703 ms |
30968 KB |
Output is correct |
37 |
Correct |
635 ms |
30940 KB |
Output is correct |
38 |
Correct |
523 ms |
30968 KB |
Output is correct |