#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
bool cmax(X &a, const Y &b) {
return a < b ? a = b, 1 : 0;
}
using ll = long long;
#define il i * 2
#define ir i * 2 + 1
const int N = 1500005;
vector<int> val;
int lo, hi, t[N], x[N], a[N];
ll dp[N * 4][2][2];
ll tag1[N * 4], tag2[N * 4];
void build(int i, int l, int r) {
dp[i][0][1] = -1e18;
if (l < r) {
int m = (l + r) / 2;
build(il, l, m);
build(ir, m + 1, r);
}
}
void apply(int i, ll v1, ll v2) {
dp[i][0][0] += v1 + v2;
dp[i][1][1] -= v1;
dp[i][0][1] += v2;
dp[i][1][0] += v2;
tag1[i] += v1;
tag2[i] += v2;
}
void push(int i) {
apply(il, tag1[i], tag2[i]);
apply(ir, tag1[i], tag2[i]);
tag1[i] = tag2[i] = 0;
}
void pull(int i) {
for (int x = 0; x < 2; x++)
for (int y = 0; y < 2; y++) {
dp[i][x][y] = max(dp[il][x][y], dp[ir][x][y]);
for (int z = 0; z < 2; z++)
cmax(dp[i][x][y], dp[il][x][z] + dp[ir][z ^ 1][y]);
}
}
void add(int i, int l, int r, ll v1, ll v2) {
if (l >= lo && r <= hi)
return apply(i, v1, v2);
push(i);
int m = (l + r) / 2;
if (m >= lo) add(il, l, m, v1, v2);
if (m < hi) add(ir, m + 1, r, v1, v2);
pull(i);
}
void add(int l, int r, ll v1, ll v2) {
if (l > r) return;
lo = l; hi = r;
add(1, 1, val.size(), v1, v2);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int q, m; cin >> q >> m;
for (int i = 0; i < q; i++) {
cin >> t[i] >> x[i] >> a[i];
if (t[i] == 2) {
val.push_back(x[i] - m);
val.push_back(x[i] + m);
} else val.push_back(x[i]);
}
sort(val.begin(), val.end());
val.erase(unique(val.begin(), val.end()), val.end());
build(1, 1, val.size());
ll sum = 0;
for (int i = 0; i < q; i++) {
if (t[i] == 1) {
int l = upper_bound(val.begin(),
val.end(), x[i]) - val.begin();
add(l + 1, val.size(), a[i], 0);
add(l, l, 0, a[i]); sum += a[i];
} else {
int l = upper_bound(val.begin(),
val.end(), x[i] - m) - val.begin();
int r = upper_bound(val.begin(),
val.end(), x[i] + m) - val.begin();
add(r + 1, val.size(), -a[i], 0);
add(l, r, 0, -a[i]);
}
cout << sum - max(dp[1][1][0], 0ll) << '\n';
}
}
# |
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 |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
7 ms |
1236 KB |
Output is correct |
11 |
Correct |
5 ms |
1236 KB |
Output is correct |
12 |
Correct |
6 ms |
1232 KB |
Output is correct |
13 |
Correct |
6 ms |
1336 KB |
Output is correct |
14 |
Correct |
5 ms |
1236 KB |
Output is correct |
15 |
Correct |
5 ms |
1240 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
6 ms |
852 KB |
Output is correct |
18 |
Correct |
4 ms |
852 KB |
Output is correct |
19 |
Correct |
5 ms |
1236 KB |
Output is correct |
20 |
Correct |
4 ms |
852 KB |
Output is correct |
21 |
Correct |
7 ms |
1236 KB |
Output is correct |
22 |
Correct |
4 ms |
852 KB |
Output is correct |
23 |
Correct |
5 ms |
1304 KB |
Output is correct |
24 |
Correct |
5 ms |
848 KB |
Output is correct |
25 |
Correct |
5 ms |
1224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
741 ms |
42164 KB |
Output is correct |
5 |
Correct |
365 ms |
24552 KB |
Output is correct |
6 |
Correct |
874 ms |
46136 KB |
Output is correct |
7 |
Correct |
348 ms |
24640 KB |
Output is correct |
8 |
Correct |
1067 ms |
49288 KB |
Output is correct |
9 |
Correct |
787 ms |
50680 KB |
Output is correct |
10 |
Correct |
1036 ms |
50248 KB |
Output is correct |
11 |
Correct |
772 ms |
49364 KB |
Output is correct |
12 |
Correct |
304 ms |
22776 KB |
Output is correct |
13 |
Correct |
480 ms |
38436 KB |
Output is correct |
14 |
Correct |
849 ms |
47048 KB |
Output is correct |
15 |
Correct |
778 ms |
47072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
363 ms |
22452 KB |
Output is correct |
3 |
Correct |
491 ms |
38312 KB |
Output is correct |
4 |
Correct |
774 ms |
46824 KB |
Output is correct |
5 |
Correct |
794 ms |
46960 KB |
Output is correct |
6 |
Correct |
972 ms |
67268 KB |
Output is correct |
7 |
Correct |
54 ms |
8024 KB |
Output is correct |
8 |
Correct |
433 ms |
33844 KB |
Output is correct |
9 |
Correct |
689 ms |
62852 KB |
Output is correct |
10 |
Correct |
1336 ms |
122512 KB |
Output is correct |
11 |
Correct |
1286 ms |
122304 KB |
Output is correct |
12 |
Correct |
1278 ms |
122160 KB |
Output is correct |
13 |
Correct |
1351 ms |
122428 KB |
Output is correct |
14 |
Correct |
1317 ms |
122560 KB |
Output is correct |
15 |
Correct |
1225 ms |
121908 KB |
Output is correct |
16 |
Correct |
1308 ms |
122428 KB |
Output is correct |
17 |
Correct |
1385 ms |
122412 KB |
Output is correct |
18 |
Correct |
1510 ms |
122316 KB |
Output is correct |
19 |
Correct |
1373 ms |
122584 KB |
Output is correct |
20 |
Correct |
1382 ms |
121904 KB |
Output is correct |
21 |
Correct |
1515 ms |
122444 KB |
Output is correct |
22 |
Correct |
1506 ms |
122304 KB |
Output is correct |
23 |
Correct |
1590 ms |
122700 KB |
Output is correct |
24 |
Correct |
1379 ms |
122384 KB |
Output is correct |
25 |
Correct |
1502 ms |
122380 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 |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
7 ms |
1236 KB |
Output is correct |
11 |
Correct |
5 ms |
1236 KB |
Output is correct |
12 |
Correct |
6 ms |
1232 KB |
Output is correct |
13 |
Correct |
6 ms |
1336 KB |
Output is correct |
14 |
Correct |
5 ms |
1236 KB |
Output is correct |
15 |
Correct |
5 ms |
1240 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
6 ms |
852 KB |
Output is correct |
18 |
Correct |
4 ms |
852 KB |
Output is correct |
19 |
Correct |
5 ms |
1236 KB |
Output is correct |
20 |
Correct |
4 ms |
852 KB |
Output is correct |
21 |
Correct |
7 ms |
1236 KB |
Output is correct |
22 |
Correct |
4 ms |
852 KB |
Output is correct |
23 |
Correct |
5 ms |
1304 KB |
Output is correct |
24 |
Correct |
5 ms |
848 KB |
Output is correct |
25 |
Correct |
5 ms |
1224 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
741 ms |
42164 KB |
Output is correct |
30 |
Correct |
365 ms |
24552 KB |
Output is correct |
31 |
Correct |
874 ms |
46136 KB |
Output is correct |
32 |
Correct |
348 ms |
24640 KB |
Output is correct |
33 |
Correct |
1067 ms |
49288 KB |
Output is correct |
34 |
Correct |
787 ms |
50680 KB |
Output is correct |
35 |
Correct |
1036 ms |
50248 KB |
Output is correct |
36 |
Correct |
772 ms |
49364 KB |
Output is correct |
37 |
Correct |
304 ms |
22776 KB |
Output is correct |
38 |
Correct |
480 ms |
38436 KB |
Output is correct |
39 |
Correct |
849 ms |
47048 KB |
Output is correct |
40 |
Correct |
778 ms |
47072 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
363 ms |
22452 KB |
Output is correct |
43 |
Correct |
491 ms |
38312 KB |
Output is correct |
44 |
Correct |
774 ms |
46824 KB |
Output is correct |
45 |
Correct |
794 ms |
46960 KB |
Output is correct |
46 |
Correct |
972 ms |
67268 KB |
Output is correct |
47 |
Correct |
54 ms |
8024 KB |
Output is correct |
48 |
Correct |
433 ms |
33844 KB |
Output is correct |
49 |
Correct |
689 ms |
62852 KB |
Output is correct |
50 |
Correct |
1336 ms |
122512 KB |
Output is correct |
51 |
Correct |
1286 ms |
122304 KB |
Output is correct |
52 |
Correct |
1278 ms |
122160 KB |
Output is correct |
53 |
Correct |
1351 ms |
122428 KB |
Output is correct |
54 |
Correct |
1317 ms |
122560 KB |
Output is correct |
55 |
Correct |
1225 ms |
121908 KB |
Output is correct |
56 |
Correct |
1308 ms |
122428 KB |
Output is correct |
57 |
Correct |
1385 ms |
122412 KB |
Output is correct |
58 |
Correct |
1510 ms |
122316 KB |
Output is correct |
59 |
Correct |
1373 ms |
122584 KB |
Output is correct |
60 |
Correct |
1382 ms |
121904 KB |
Output is correct |
61 |
Correct |
1515 ms |
122444 KB |
Output is correct |
62 |
Correct |
1506 ms |
122304 KB |
Output is correct |
63 |
Correct |
1590 ms |
122700 KB |
Output is correct |
64 |
Correct |
1379 ms |
122384 KB |
Output is correct |
65 |
Correct |
1502 ms |
122380 KB |
Output is correct |
66 |
Correct |
640 ms |
63800 KB |
Output is correct |
67 |
Correct |
805 ms |
66960 KB |
Output is correct |
68 |
Correct |
980 ms |
70200 KB |
Output is correct |
69 |
Correct |
884 ms |
68396 KB |
Output is correct |
70 |
Correct |
1263 ms |
125192 KB |
Output is correct |
71 |
Correct |
1451 ms |
125388 KB |
Output is correct |
72 |
Correct |
1317 ms |
125404 KB |
Output is correct |
73 |
Correct |
1328 ms |
125380 KB |
Output is correct |
74 |
Correct |
1413 ms |
120348 KB |
Output is correct |
75 |
Correct |
1512 ms |
125792 KB |
Output is correct |
76 |
Correct |
1204 ms |
70500 KB |
Output is correct |
77 |
Correct |
1027 ms |
68944 KB |
Output is correct |
78 |
Correct |
1051 ms |
68920 KB |
Output is correct |
79 |
Correct |
1334 ms |
125372 KB |
Output is correct |
80 |
Correct |
1134 ms |
68820 KB |
Output is correct |
81 |
Correct |
1311 ms |
125020 KB |
Output is correct |
82 |
Correct |
1066 ms |
68968 KB |
Output is correct |
83 |
Correct |
1568 ms |
125628 KB |
Output is correct |
84 |
Correct |
1082 ms |
68900 KB |
Output is correct |
85 |
Correct |
1572 ms |
125656 KB |
Output is correct |
86 |
Correct |
1040 ms |
69224 KB |
Output is correct |
87 |
Correct |
1675 ms |
126072 KB |
Output is correct |
88 |
Correct |
1022 ms |
69200 KB |
Output is correct |
89 |
Correct |
1439 ms |
125400 KB |
Output is correct |