#include <bits/stdc++.h>
#define ent '\n'
#define int long long
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 12;
const int mod = 998244353;
int n, m, k;
struct asd {
int dp[2][2];
int rx, d;
asd() {
dp[0][0] = dp[1][0] = dp[0][1] = dp[1][1] = 0;
rx = d = 0;
}
};
asd merge(asd x, asd y) {
asd ans;
ans.rx = y.rx;
ans.d = max(x.d, y.d);
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
ans.dp[i][j] = min(x.dp[i][1] + y.dp[1][j] - x.rx, min(x.dp[i][0], x.dp[i][1]) + min(y.dp[0][j], y.dp[1][j]));
}
}
return ans;
}
asd t[maxn * 25];
int p[maxn * 25], c[maxn * 25];
int rx[maxn * 25];
int nv = 1;
void pull(int v, int x) {
p[v] += x;
t[v].dp[0][0] = min(t[v].dp[0][0] + x, 0ll);
t[v].dp[1][1] += x;
t[v].dp[0][1] += x;
t[v].dp[1][0] += x;
t[v].d += x;
}
void dull(int v, int x) {
rx[v] += x;
t[v].rx += x;
}
void push(int v, int tl, int tr) {
if(tl == tr) return;
int mid = tl + tr >> 1;
pull(v * 2, p[v]);
pull(v * 2 + 1, p[v]);
dull(v * 2, rx[v]);
dull(v * 2 + 1, rx[v]);
p[v] = rx[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int x) {
if(tl > r || l > tr) return;
if(tl >= l && tr <= r) {
pull(v, x);
return;
}
push(v, tl, tr);
int mid = tl + tr >> 1;
upd(v * 2, tl, mid, l, r, x);
upd(v * 2 + 1, mid + 1, tr, l, r, x);
t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
void upd(int v, int tl, int tr, int pos, int x) {
if(tl == tr) {
c[v] += x;
t[v].dp[0][0] = 0;
t[v].dp[1][0] = t[v].dp[0][1] = 1e18;
t[v].dp[1][1] = t[v].d - c[v];
return;
}
push(v, tl, tr);
int mid = tl + tr >> 1;
if(pos <= mid) {
upd(v * 2, tl, mid, pos, x);
}
else {
upd(v * 2 + 1, mid + 1, tr, pos, x);
}
t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
void updrx(int v, int tl, int tr, int l, int r, int x) {
push(v, tl, tr);
if(tl > r || l > tr) return;
if(tl >= l && tr <= r) {
dull(v, x);
return;
}
int mid = tl + tr >> 1;
updrx(v * 2, tl, mid, l, r, x);
updrx(v * 2 + 1, mid + 1, tr, l, r, x);
t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
void solve() {
cin >> n >> k;
int sum = 0;
vector<array<int, 3>> Q;
vector<int> v;
while(n--) {
int tp, pos, x;
cin >> tp >> pos >> x;
Q.push_back({tp, pos, x});
for(int dx = -1; dx <= 1; dx++) {
if(pos + dx >= 0 && tp == 1) {
v.push_back(pos + dx);
}
if(pos + k + dx >= 0 && tp == 2) {
v.push_back(pos + k + dx);
}
if(pos - k + dx >= 0 && tp == 2) {
v.push_back(pos + k + dx);
}
}
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
m = (int) v.size();
for(auto [tp, pos, x] : Q) {
if(tp == 1) {
pos = lower_bound(v.begin(), v.end(), pos) - v.begin();
upd(1, 0, m, pos, x);
sum += x;
}
else {
int l = lower_bound(v.begin(), v.end(), pos - k) - v.begin(), r = lower_bound(v.begin(), v.end(), pos + k) - v.begin();
upd(1, 0, m, l, r, x);
updrx(1, 0, m, l, r - 1, x);
}
cout << sum + min({t[1].dp[0][0], t[1].dp[1][0], t[1].dp[0][1], t[1].dp[1][1]}) << ent;
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--) {
solve();
}
}
Compilation message
sugar.cpp: In function 'void push(long long int, long long int, long long int)':
sugar.cpp:57:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | int mid = tl + tr >> 1;
| ~~~^~~~
sugar.cpp:57:9: warning: unused variable 'mid' [-Wunused-variable]
57 | int mid = tl + tr >> 1;
| ^~~
sugar.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int, long long int)':
sugar.cpp:72:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | int mid = tl + tr >> 1;
| ~~~^~~~
sugar.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int)':
sugar.cpp:87:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | int mid = tl + tr >> 1;
| ~~~^~~~
sugar.cpp: In function 'void updrx(long long int, long long int, long long int, long long int, long long int, long long int)':
sugar.cpp:104:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
104 | int mid = tl + tr >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
288 ms |
589516 KB |
Output is correct |
2 |
Correct |
293 ms |
589460 KB |
Output is correct |
3 |
Correct |
273 ms |
589384 KB |
Output is correct |
4 |
Correct |
317 ms |
589372 KB |
Output is correct |
5 |
Correct |
302 ms |
589460 KB |
Output is correct |
6 |
Correct |
296 ms |
589880 KB |
Output is correct |
7 |
Correct |
292 ms |
589896 KB |
Output is correct |
8 |
Correct |
299 ms |
589648 KB |
Output is correct |
9 |
Correct |
284 ms |
589640 KB |
Output is correct |
10 |
Correct |
287 ms |
590424 KB |
Output is correct |
11 |
Correct |
300 ms |
590664 KB |
Output is correct |
12 |
Correct |
321 ms |
590408 KB |
Output is correct |
13 |
Correct |
321 ms |
590452 KB |
Output is correct |
14 |
Correct |
293 ms |
590396 KB |
Output is correct |
15 |
Correct |
282 ms |
590424 KB |
Output is correct |
16 |
Correct |
284 ms |
590408 KB |
Output is correct |
17 |
Correct |
285 ms |
590408 KB |
Output is correct |
18 |
Correct |
293 ms |
590392 KB |
Output is correct |
19 |
Correct |
294 ms |
590412 KB |
Output is correct |
20 |
Correct |
292 ms |
590544 KB |
Output is correct |
21 |
Correct |
308 ms |
590364 KB |
Output is correct |
22 |
Correct |
325 ms |
590412 KB |
Output is correct |
23 |
Correct |
305 ms |
590408 KB |
Output is correct |
24 |
Correct |
314 ms |
590412 KB |
Output is correct |
25 |
Correct |
300 ms |
590408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
303 ms |
589536 KB |
Output is correct |
2 |
Correct |
299 ms |
589436 KB |
Output is correct |
3 |
Correct |
305 ms |
589384 KB |
Output is correct |
4 |
Correct |
1139 ms |
640556 KB |
Output is correct |
5 |
Correct |
654 ms |
618516 KB |
Output is correct |
6 |
Correct |
1338 ms |
643072 KB |
Output is correct |
7 |
Correct |
660 ms |
618768 KB |
Output is correct |
8 |
Correct |
1477 ms |
648840 KB |
Output is correct |
9 |
Correct |
1093 ms |
649352 KB |
Output is correct |
10 |
Correct |
1539 ms |
648764 KB |
Output is correct |
11 |
Correct |
1133 ms |
649304 KB |
Output is correct |
12 |
Correct |
628 ms |
616732 KB |
Output is correct |
13 |
Correct |
790 ms |
632808 KB |
Output is correct |
14 |
Correct |
1100 ms |
645912 KB |
Output is correct |
15 |
Correct |
1069 ms |
647420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
290 ms |
589448 KB |
Output is correct |
2 |
Correct |
701 ms |
616724 KB |
Output is correct |
3 |
Correct |
763 ms |
632868 KB |
Output is correct |
4 |
Correct |
1012 ms |
645956 KB |
Output is correct |
5 |
Correct |
984 ms |
646024 KB |
Output is correct |
6 |
Correct |
1189 ms |
639228 KB |
Output is correct |
7 |
Correct |
382 ms |
598344 KB |
Output is correct |
8 |
Correct |
810 ms |
625720 KB |
Output is correct |
9 |
Correct |
1016 ms |
632340 KB |
Output is correct |
10 |
Correct |
1884 ms |
719556 KB |
Output is correct |
11 |
Correct |
1988 ms |
724184 KB |
Output is correct |
12 |
Correct |
1882 ms |
724124 KB |
Output is correct |
13 |
Correct |
1772 ms |
724160 KB |
Output is correct |
14 |
Correct |
1783 ms |
724172 KB |
Output is correct |
15 |
Correct |
1803 ms |
723912 KB |
Output is correct |
16 |
Correct |
1812 ms |
723976 KB |
Output is correct |
17 |
Correct |
2239 ms |
724080 KB |
Output is correct |
18 |
Correct |
2225 ms |
724244 KB |
Output is correct |
19 |
Correct |
2247 ms |
724064 KB |
Output is correct |
20 |
Correct |
2125 ms |
724180 KB |
Output is correct |
21 |
Correct |
2410 ms |
724212 KB |
Output is correct |
22 |
Correct |
2303 ms |
722836 KB |
Output is correct |
23 |
Correct |
2189 ms |
721840 KB |
Output is correct |
24 |
Correct |
1592 ms |
708844 KB |
Output is correct |
25 |
Correct |
2384 ms |
713744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
288 ms |
589516 KB |
Output is correct |
2 |
Correct |
293 ms |
589460 KB |
Output is correct |
3 |
Correct |
273 ms |
589384 KB |
Output is correct |
4 |
Correct |
317 ms |
589372 KB |
Output is correct |
5 |
Correct |
302 ms |
589460 KB |
Output is correct |
6 |
Correct |
296 ms |
589880 KB |
Output is correct |
7 |
Correct |
292 ms |
589896 KB |
Output is correct |
8 |
Correct |
299 ms |
589648 KB |
Output is correct |
9 |
Correct |
284 ms |
589640 KB |
Output is correct |
10 |
Correct |
287 ms |
590424 KB |
Output is correct |
11 |
Correct |
300 ms |
590664 KB |
Output is correct |
12 |
Correct |
321 ms |
590408 KB |
Output is correct |
13 |
Correct |
321 ms |
590452 KB |
Output is correct |
14 |
Correct |
293 ms |
590396 KB |
Output is correct |
15 |
Correct |
282 ms |
590424 KB |
Output is correct |
16 |
Correct |
284 ms |
590408 KB |
Output is correct |
17 |
Correct |
285 ms |
590408 KB |
Output is correct |
18 |
Correct |
293 ms |
590392 KB |
Output is correct |
19 |
Correct |
294 ms |
590412 KB |
Output is correct |
20 |
Correct |
292 ms |
590544 KB |
Output is correct |
21 |
Correct |
308 ms |
590364 KB |
Output is correct |
22 |
Correct |
325 ms |
590412 KB |
Output is correct |
23 |
Correct |
305 ms |
590408 KB |
Output is correct |
24 |
Correct |
314 ms |
590412 KB |
Output is correct |
25 |
Correct |
300 ms |
590408 KB |
Output is correct |
26 |
Correct |
303 ms |
589536 KB |
Output is correct |
27 |
Correct |
299 ms |
589436 KB |
Output is correct |
28 |
Correct |
305 ms |
589384 KB |
Output is correct |
29 |
Correct |
1139 ms |
640556 KB |
Output is correct |
30 |
Correct |
654 ms |
618516 KB |
Output is correct |
31 |
Correct |
1338 ms |
643072 KB |
Output is correct |
32 |
Correct |
660 ms |
618768 KB |
Output is correct |
33 |
Correct |
1477 ms |
648840 KB |
Output is correct |
34 |
Correct |
1093 ms |
649352 KB |
Output is correct |
35 |
Correct |
1539 ms |
648764 KB |
Output is correct |
36 |
Correct |
1133 ms |
649304 KB |
Output is correct |
37 |
Correct |
628 ms |
616732 KB |
Output is correct |
38 |
Correct |
790 ms |
632808 KB |
Output is correct |
39 |
Correct |
1100 ms |
645912 KB |
Output is correct |
40 |
Correct |
1069 ms |
647420 KB |
Output is correct |
41 |
Correct |
290 ms |
589448 KB |
Output is correct |
42 |
Correct |
701 ms |
616724 KB |
Output is correct |
43 |
Correct |
763 ms |
632868 KB |
Output is correct |
44 |
Correct |
1012 ms |
645956 KB |
Output is correct |
45 |
Correct |
984 ms |
646024 KB |
Output is correct |
46 |
Correct |
1189 ms |
639228 KB |
Output is correct |
47 |
Correct |
382 ms |
598344 KB |
Output is correct |
48 |
Correct |
810 ms |
625720 KB |
Output is correct |
49 |
Correct |
1016 ms |
632340 KB |
Output is correct |
50 |
Correct |
1884 ms |
719556 KB |
Output is correct |
51 |
Correct |
1988 ms |
724184 KB |
Output is correct |
52 |
Correct |
1882 ms |
724124 KB |
Output is correct |
53 |
Correct |
1772 ms |
724160 KB |
Output is correct |
54 |
Correct |
1783 ms |
724172 KB |
Output is correct |
55 |
Correct |
1803 ms |
723912 KB |
Output is correct |
56 |
Correct |
1812 ms |
723976 KB |
Output is correct |
57 |
Correct |
2239 ms |
724080 KB |
Output is correct |
58 |
Correct |
2225 ms |
724244 KB |
Output is correct |
59 |
Correct |
2247 ms |
724064 KB |
Output is correct |
60 |
Correct |
2125 ms |
724180 KB |
Output is correct |
61 |
Correct |
2410 ms |
724212 KB |
Output is correct |
62 |
Correct |
2303 ms |
722836 KB |
Output is correct |
63 |
Correct |
2189 ms |
721840 KB |
Output is correct |
64 |
Correct |
1592 ms |
708844 KB |
Output is correct |
65 |
Correct |
2384 ms |
713744 KB |
Output is correct |
66 |
Correct |
965 ms |
638484 KB |
Output is correct |
67 |
Correct |
1139 ms |
643584 KB |
Output is correct |
68 |
Correct |
1319 ms |
649472 KB |
Output is correct |
69 |
Correct |
1307 ms |
646148 KB |
Output is correct |
70 |
Correct |
1979 ms |
727016 KB |
Output is correct |
71 |
Correct |
1934 ms |
727008 KB |
Output is correct |
72 |
Correct |
1832 ms |
727264 KB |
Output is correct |
73 |
Correct |
2036 ms |
727156 KB |
Output is correct |
74 |
Correct |
2420 ms |
702180 KB |
Output is correct |
75 |
Correct |
2295 ms |
721236 KB |
Output is correct |
76 |
Correct |
1284 ms |
720644 KB |
Output is correct |
77 |
Correct |
1296 ms |
714904 KB |
Output is correct |
78 |
Correct |
1195 ms |
715668 KB |
Output is correct |
79 |
Correct |
1928 ms |
727172 KB |
Output is correct |
80 |
Correct |
1318 ms |
715556 KB |
Output is correct |
81 |
Correct |
1912 ms |
727264 KB |
Output is correct |
82 |
Correct |
1325 ms |
714984 KB |
Output is correct |
83 |
Correct |
2176 ms |
727384 KB |
Output is correct |
84 |
Correct |
1283 ms |
714988 KB |
Output is correct |
85 |
Correct |
2324 ms |
727336 KB |
Output is correct |
86 |
Correct |
1317 ms |
714956 KB |
Output is correct |
87 |
Correct |
2315 ms |
727140 KB |
Output is correct |
88 |
Correct |
1397 ms |
714956 KB |
Output is correct |
89 |
Correct |
1920 ms |
720572 KB |
Output is correct |