#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll INF = 4e18;
struct P{
ll x;
int c;
P(){}
P(ll _x, int _c): x(_x), c(_c) {}
bool operator < (const P &p) const{return x < p.x;}
P operator + (const P &p) const{return P(x + p.x, c + p.c);}
};
struct Node{
P ans[2][2];
ll one[2][2];
Node(){}
Node(ll c, ll d){
ans[0][0] = P(-c, 0);
ans[0][1] = P(-INF, 0);
ans[1][0] = P(-INF, 0);
ans[1][1] = P(d, 1);
one[0][0] = -c;
one[0][1] = -INF;
one[1][0] = -INF;
one[1][1] = d;
}
void pcd(ll x){
ans[0][0].x -= x;
ans[1][1].x += x;
one[0][0] -= x;
one[1][1] += x;
}
void pd(ll x){
assert(x<0);
one[0][1] += x;
one[1][0] += x;
one[1][1] += x;
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
assert(ans[i][j].c <= 2);
ans[i][j].x += x * ans[i][j].c;
if (ans[i][j].x < one[i][j]){
ans[i][j].x = one[i][j];
if (i || j) ans[i][j].c = 1;
else ans[i][j].c = 0;
}
}
}
}
Node operator + (const Node &N) const{
Node ret;
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
ret.ans[i][j] = max(ans[i][j], N.ans[i][j]);
ret.one[i][j] = max(one[i][j], N.one[i][j]);
for (int k=0;k<2;k++) ret.ans[i][j] = max(ret.ans[i][j], ans[i][k] + N.ans[k^1][j]);
}
}
ret.one[0][1] = max(ret.one[0][1], one[0][0] + N.one[1][1]);
ret.one[1][0] = max(ret.one[1][0], one[1][1] + N.one[0][0]);
return ret;
}
};
struct Seg{
Node tree[1101000];
pair<ll, ll> lazy[1101000];
void init(int i, int l, int r){
lazy[i] = {0, 0};
if (l==r) {tree[i] = Node(0, 0); return;}
int m = (l+r)>>1;
init(i<<1, l, m); init(i<<1|1, m+1, r);
tree[i] = tree[i<<1] + tree[i<<1|1];
}
void propagate(int i, int l, int r){
if (lazy[i].first) tree[i].pcd(lazy[i].first);
if (lazy[i].second) tree[i].pd(lazy[i].second);
if (l!=r){
lazy[i<<1].first += lazy[i].first;
lazy[i<<1].second += lazy[i].second;
lazy[i<<1|1].first += lazy[i].first;
lazy[i<<1|1].second += lazy[i].second;
}
lazy[i] = {0, 0};
}
void update1(int i, int l, int r, int s, int e, ll x){
propagate(i, l, r);
if (r<s || e<l) return;
if (s<=l && r<=e){
lazy[i].first += x;
propagate(i, l, r);
return;
}
int m = (l+r)>>1;
update1(i<<1, l, m, s, e, x); update1(i<<1|1, m+1, r, s, e, x);
tree[i] = tree[i<<1] + tree[i<<1|1];
}
void update2(int i, int l, int r, int s, int e, ll x){
propagate(i, l, r);
if (r<s || e<l) return;
if (s<=l && r<=e){
lazy[i].second += x;
propagate(i, l, r);
return;
}
int m = (l+r)>>1;
update2(i<<1, l, m, s, e, x); update2(i<<1|1, m+1, r, s, e, x);
tree[i] = tree[i<<1] + tree[i<<1|1];
}
}tree;
vector<int> C[3];
int T[500500], X[500500], A[500500], d;
int main(){
int q;
scanf("%d %d", &q, &d);
for (int i=1;i<=q;i++){
scanf("%d %d %d", T+i, X+i, A+i);
C[T[i]].push_back(X[i]);
C[T[i]].push_back(X[i]-1);
}
int ant = C[1].size()<C[2].size()?1:2;
C[ant].push_back(0);
sort(C[ant].begin(), C[ant].end());
C[ant].erase(unique(C[ant].begin(), C[ant].end()), C[ant].end());
int sz = C[ant].size();
tree.init(1, 1, sz);
ll cur = 0;
for (int i=1;i<=q;i++){
if (T[i]==ant){
int idx = lower_bound(C[ant].begin(), C[ant].end(), X[i]) - C[ant].begin() + 1;
tree.update1(1, 1, sz, idx, sz, A[i]);
cur += A[i];
}
else{
int idx1 = lower_bound(C[ant].begin(), C[ant].end(), X[i]-d) - C[ant].begin() + 1;
int idx2 = lower_bound(C[ant].begin(), C[ant].end(), X[i]+d) - C[ant].begin() + 1;
tree.update1(1, 1, sz, idx2, sz, -A[i]);
tree.update2(1, 1, sz, idx1, idx2-1, -A[i]);
}
ll ans = cur - max(tree.tree[1].ans[0][1].x, 0LL);
printf("%lld\n", ans);
}
return 0;
}
Compilation message
sugar.cpp: In function 'int main()':
sugar.cpp:133:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | scanf("%d %d", &q, &d);
| ~~~~~^~~~~~~~~~~~~~~~~
sugar.cpp:135:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d %d %d", T+i, X+i, A+i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
17492 KB |
Output is correct |
2 |
Correct |
7 ms |
17492 KB |
Output is correct |
3 |
Correct |
7 ms |
17548 KB |
Output is correct |
4 |
Correct |
7 ms |
17476 KB |
Output is correct |
5 |
Correct |
7 ms |
17492 KB |
Output is correct |
6 |
Correct |
10 ms |
18016 KB |
Output is correct |
7 |
Correct |
11 ms |
18004 KB |
Output is correct |
8 |
Correct |
9 ms |
17748 KB |
Output is correct |
9 |
Correct |
9 ms |
17748 KB |
Output is correct |
10 |
Correct |
14 ms |
18336 KB |
Output is correct |
11 |
Correct |
13 ms |
18388 KB |
Output is correct |
12 |
Correct |
13 ms |
18412 KB |
Output is correct |
13 |
Correct |
13 ms |
18348 KB |
Output is correct |
14 |
Correct |
8 ms |
17620 KB |
Output is correct |
15 |
Correct |
12 ms |
17800 KB |
Output is correct |
16 |
Correct |
12 ms |
17692 KB |
Output is correct |
17 |
Correct |
9 ms |
17620 KB |
Output is correct |
18 |
Correct |
11 ms |
17620 KB |
Output is correct |
19 |
Correct |
13 ms |
18388 KB |
Output is correct |
20 |
Correct |
9 ms |
17588 KB |
Output is correct |
21 |
Correct |
14 ms |
18388 KB |
Output is correct |
22 |
Correct |
8 ms |
17620 KB |
Output is correct |
23 |
Correct |
13 ms |
18388 KB |
Output is correct |
24 |
Correct |
9 ms |
17596 KB |
Output is correct |
25 |
Correct |
13 ms |
18424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
17492 KB |
Output is correct |
2 |
Correct |
7 ms |
17556 KB |
Output is correct |
3 |
Correct |
7 ms |
17492 KB |
Output is correct |
4 |
Correct |
1130 ms |
79484 KB |
Output is correct |
5 |
Correct |
529 ms |
75332 KB |
Output is correct |
6 |
Correct |
1330 ms |
96800 KB |
Output is correct |
7 |
Correct |
529 ms |
75324 KB |
Output is correct |
8 |
Correct |
1683 ms |
133292 KB |
Output is correct |
9 |
Correct |
1146 ms |
133896 KB |
Output is correct |
10 |
Correct |
1602 ms |
133284 KB |
Output is correct |
11 |
Correct |
1161 ms |
133896 KB |
Output is correct |
12 |
Correct |
496 ms |
73180 KB |
Output is correct |
13 |
Correct |
727 ms |
124748 KB |
Output is correct |
14 |
Correct |
1153 ms |
130240 KB |
Output is correct |
15 |
Correct |
1115 ms |
130248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
17492 KB |
Output is correct |
2 |
Correct |
515 ms |
73288 KB |
Output is correct |
3 |
Correct |
725 ms |
124808 KB |
Output is correct |
4 |
Correct |
1133 ms |
130376 KB |
Output is correct |
5 |
Correct |
1125 ms |
130348 KB |
Output is correct |
6 |
Correct |
1244 ms |
127372 KB |
Output is correct |
7 |
Correct |
78 ms |
24620 KB |
Output is correct |
8 |
Correct |
639 ms |
72540 KB |
Output is correct |
9 |
Correct |
943 ms |
75376 KB |
Output is correct |
10 |
Correct |
1674 ms |
130212 KB |
Output is correct |
11 |
Correct |
1713 ms |
130212 KB |
Output is correct |
12 |
Correct |
1726 ms |
130076 KB |
Output is correct |
13 |
Correct |
1658 ms |
130168 KB |
Output is correct |
14 |
Correct |
1834 ms |
130256 KB |
Output is correct |
15 |
Correct |
1690 ms |
129964 KB |
Output is correct |
16 |
Correct |
1725 ms |
130352 KB |
Output is correct |
17 |
Correct |
1927 ms |
130140 KB |
Output is correct |
18 |
Correct |
1977 ms |
130248 KB |
Output is correct |
19 |
Correct |
2003 ms |
130300 KB |
Output is correct |
20 |
Correct |
2017 ms |
130196 KB |
Output is correct |
21 |
Correct |
2245 ms |
130228 KB |
Output is correct |
22 |
Correct |
2085 ms |
130300 KB |
Output is correct |
23 |
Correct |
2165 ms |
130132 KB |
Output is correct |
24 |
Correct |
1025 ms |
130272 KB |
Output is correct |
25 |
Correct |
1610 ms |
130320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
17492 KB |
Output is correct |
2 |
Correct |
7 ms |
17492 KB |
Output is correct |
3 |
Correct |
7 ms |
17548 KB |
Output is correct |
4 |
Correct |
7 ms |
17476 KB |
Output is correct |
5 |
Correct |
7 ms |
17492 KB |
Output is correct |
6 |
Correct |
10 ms |
18016 KB |
Output is correct |
7 |
Correct |
11 ms |
18004 KB |
Output is correct |
8 |
Correct |
9 ms |
17748 KB |
Output is correct |
9 |
Correct |
9 ms |
17748 KB |
Output is correct |
10 |
Correct |
14 ms |
18336 KB |
Output is correct |
11 |
Correct |
13 ms |
18388 KB |
Output is correct |
12 |
Correct |
13 ms |
18412 KB |
Output is correct |
13 |
Correct |
13 ms |
18348 KB |
Output is correct |
14 |
Correct |
8 ms |
17620 KB |
Output is correct |
15 |
Correct |
12 ms |
17800 KB |
Output is correct |
16 |
Correct |
12 ms |
17692 KB |
Output is correct |
17 |
Correct |
9 ms |
17620 KB |
Output is correct |
18 |
Correct |
11 ms |
17620 KB |
Output is correct |
19 |
Correct |
13 ms |
18388 KB |
Output is correct |
20 |
Correct |
9 ms |
17588 KB |
Output is correct |
21 |
Correct |
14 ms |
18388 KB |
Output is correct |
22 |
Correct |
8 ms |
17620 KB |
Output is correct |
23 |
Correct |
13 ms |
18388 KB |
Output is correct |
24 |
Correct |
9 ms |
17596 KB |
Output is correct |
25 |
Correct |
13 ms |
18424 KB |
Output is correct |
26 |
Correct |
7 ms |
17492 KB |
Output is correct |
27 |
Correct |
7 ms |
17556 KB |
Output is correct |
28 |
Correct |
7 ms |
17492 KB |
Output is correct |
29 |
Correct |
1130 ms |
79484 KB |
Output is correct |
30 |
Correct |
529 ms |
75332 KB |
Output is correct |
31 |
Correct |
1330 ms |
96800 KB |
Output is correct |
32 |
Correct |
529 ms |
75324 KB |
Output is correct |
33 |
Correct |
1683 ms |
133292 KB |
Output is correct |
34 |
Correct |
1146 ms |
133896 KB |
Output is correct |
35 |
Correct |
1602 ms |
133284 KB |
Output is correct |
36 |
Correct |
1161 ms |
133896 KB |
Output is correct |
37 |
Correct |
496 ms |
73180 KB |
Output is correct |
38 |
Correct |
727 ms |
124748 KB |
Output is correct |
39 |
Correct |
1153 ms |
130240 KB |
Output is correct |
40 |
Correct |
1115 ms |
130248 KB |
Output is correct |
41 |
Correct |
7 ms |
17492 KB |
Output is correct |
42 |
Correct |
515 ms |
73288 KB |
Output is correct |
43 |
Correct |
725 ms |
124808 KB |
Output is correct |
44 |
Correct |
1133 ms |
130376 KB |
Output is correct |
45 |
Correct |
1125 ms |
130348 KB |
Output is correct |
46 |
Correct |
1244 ms |
127372 KB |
Output is correct |
47 |
Correct |
78 ms |
24620 KB |
Output is correct |
48 |
Correct |
639 ms |
72540 KB |
Output is correct |
49 |
Correct |
943 ms |
75376 KB |
Output is correct |
50 |
Correct |
1674 ms |
130212 KB |
Output is correct |
51 |
Correct |
1713 ms |
130212 KB |
Output is correct |
52 |
Correct |
1726 ms |
130076 KB |
Output is correct |
53 |
Correct |
1658 ms |
130168 KB |
Output is correct |
54 |
Correct |
1834 ms |
130256 KB |
Output is correct |
55 |
Correct |
1690 ms |
129964 KB |
Output is correct |
56 |
Correct |
1725 ms |
130352 KB |
Output is correct |
57 |
Correct |
1927 ms |
130140 KB |
Output is correct |
58 |
Correct |
1977 ms |
130248 KB |
Output is correct |
59 |
Correct |
2003 ms |
130300 KB |
Output is correct |
60 |
Correct |
2017 ms |
130196 KB |
Output is correct |
61 |
Correct |
2245 ms |
130228 KB |
Output is correct |
62 |
Correct |
2085 ms |
130300 KB |
Output is correct |
63 |
Correct |
2165 ms |
130132 KB |
Output is correct |
64 |
Correct |
1025 ms |
130272 KB |
Output is correct |
65 |
Correct |
1610 ms |
130320 KB |
Output is correct |
66 |
Correct |
872 ms |
76568 KB |
Output is correct |
67 |
Correct |
1084 ms |
78696 KB |
Output is correct |
68 |
Correct |
1533 ms |
130084 KB |
Output is correct |
69 |
Correct |
1205 ms |
79556 KB |
Output is correct |
70 |
Correct |
1643 ms |
133008 KB |
Output is correct |
71 |
Correct |
1860 ms |
133308 KB |
Output is correct |
72 |
Correct |
1771 ms |
133324 KB |
Output is correct |
73 |
Correct |
1828 ms |
133380 KB |
Output is correct |
74 |
Correct |
196 ms |
28432 KB |
Output is correct |
75 |
Correct |
1088 ms |
36936 KB |
Output is correct |
76 |
Correct |
1195 ms |
37116 KB |
Output is correct |
77 |
Correct |
214 ms |
28388 KB |
Output is correct |
78 |
Correct |
197 ms |
28424 KB |
Output is correct |
79 |
Correct |
1813 ms |
133304 KB |
Output is correct |
80 |
Correct |
201 ms |
28452 KB |
Output is correct |
81 |
Correct |
1812 ms |
133272 KB |
Output is correct |
82 |
Correct |
203 ms |
28376 KB |
Output is correct |
83 |
Correct |
2071 ms |
133448 KB |
Output is correct |
84 |
Correct |
204 ms |
28420 KB |
Output is correct |
85 |
Correct |
2400 ms |
133612 KB |
Output is correct |
86 |
Correct |
206 ms |
28416 KB |
Output is correct |
87 |
Correct |
2221 ms |
133604 KB |
Output is correct |
88 |
Correct |
215 ms |
28488 KB |
Output is correct |
89 |
Correct |
971 ms |
85028 KB |
Output is correct |