#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);
}
for (int k=1;k<=2;k++){
C[k].push_back(0);
sort(C[k].begin(), C[k].end());
C[k].erase(unique(C[k].begin(), C[k].end()), C[k].end());
}
int ant = C[1].size()<C[2].size()?1:2;
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 |
17456 KB |
Output is correct |
3 |
Correct |
7 ms |
17492 KB |
Output is correct |
4 |
Correct |
7 ms |
17532 KB |
Output is correct |
5 |
Correct |
6 ms |
17492 KB |
Output is correct |
6 |
Correct |
10 ms |
18016 KB |
Output is correct |
7 |
Correct |
9 ms |
17912 KB |
Output is correct |
8 |
Correct |
8 ms |
17748 KB |
Output is correct |
9 |
Correct |
10 ms |
17780 KB |
Output is correct |
10 |
Correct |
14 ms |
18436 KB |
Output is correct |
11 |
Correct |
12 ms |
18388 KB |
Output is correct |
12 |
Correct |
13 ms |
18392 KB |
Output is correct |
13 |
Correct |
13 ms |
18428 KB |
Output is correct |
14 |
Correct |
8 ms |
17620 KB |
Output is correct |
15 |
Correct |
9 ms |
17620 KB |
Output is correct |
16 |
Correct |
11 ms |
17640 KB |
Output is correct |
17 |
Correct |
8 ms |
17620 KB |
Output is correct |
18 |
Correct |
8 ms |
17620 KB |
Output is correct |
19 |
Correct |
13 ms |
18428 KB |
Output is correct |
20 |
Correct |
8 ms |
17620 KB |
Output is correct |
21 |
Correct |
15 ms |
18444 KB |
Output is correct |
22 |
Correct |
8 ms |
17620 KB |
Output is correct |
23 |
Correct |
16 ms |
18432 KB |
Output is correct |
24 |
Correct |
32 ms |
17540 KB |
Output is correct |
25 |
Correct |
28 ms |
18436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
17504 KB |
Output is correct |
2 |
Correct |
8 ms |
17492 KB |
Output is correct |
3 |
Correct |
7 ms |
17496 KB |
Output is correct |
4 |
Correct |
1370 ms |
79284 KB |
Output is correct |
5 |
Correct |
524 ms |
75228 KB |
Output is correct |
6 |
Correct |
1355 ms |
96688 KB |
Output is correct |
7 |
Correct |
531 ms |
75388 KB |
Output is correct |
8 |
Correct |
1666 ms |
133308 KB |
Output is correct |
9 |
Correct |
1129 ms |
134048 KB |
Output is correct |
10 |
Correct |
1617 ms |
133312 KB |
Output is correct |
11 |
Correct |
1209 ms |
133840 KB |
Output is correct |
12 |
Correct |
538 ms |
73120 KB |
Output is correct |
13 |
Correct |
732 ms |
124780 KB |
Output is correct |
14 |
Correct |
1182 ms |
130428 KB |
Output is correct |
15 |
Correct |
1161 ms |
130316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
17492 KB |
Output is correct |
2 |
Correct |
491 ms |
73172 KB |
Output is correct |
3 |
Correct |
726 ms |
124844 KB |
Output is correct |
4 |
Correct |
1243 ms |
130352 KB |
Output is correct |
5 |
Correct |
1129 ms |
130328 KB |
Output is correct |
6 |
Correct |
1310 ms |
127324 KB |
Output is correct |
7 |
Correct |
76 ms |
24652 KB |
Output is correct |
8 |
Correct |
605 ms |
72568 KB |
Output is correct |
9 |
Correct |
953 ms |
75384 KB |
Output is correct |
10 |
Correct |
1778 ms |
130104 KB |
Output is correct |
11 |
Correct |
1734 ms |
130152 KB |
Output is correct |
12 |
Correct |
1693 ms |
130156 KB |
Output is correct |
13 |
Correct |
1822 ms |
130096 KB |
Output is correct |
14 |
Correct |
1714 ms |
130172 KB |
Output is correct |
15 |
Correct |
1640 ms |
130004 KB |
Output is correct |
16 |
Correct |
1683 ms |
130064 KB |
Output is correct |
17 |
Correct |
1893 ms |
130252 KB |
Output is correct |
18 |
Correct |
2035 ms |
130308 KB |
Output is correct |
19 |
Correct |
2009 ms |
130400 KB |
Output is correct |
20 |
Correct |
2013 ms |
130292 KB |
Output is correct |
21 |
Correct |
2117 ms |
130212 KB |
Output is correct |
22 |
Correct |
2161 ms |
130268 KB |
Output is correct |
23 |
Correct |
2117 ms |
130248 KB |
Output is correct |
24 |
Correct |
1150 ms |
130180 KB |
Output is correct |
25 |
Correct |
1761 ms |
130148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
17492 KB |
Output is correct |
2 |
Correct |
7 ms |
17456 KB |
Output is correct |
3 |
Correct |
7 ms |
17492 KB |
Output is correct |
4 |
Correct |
7 ms |
17532 KB |
Output is correct |
5 |
Correct |
6 ms |
17492 KB |
Output is correct |
6 |
Correct |
10 ms |
18016 KB |
Output is correct |
7 |
Correct |
9 ms |
17912 KB |
Output is correct |
8 |
Correct |
8 ms |
17748 KB |
Output is correct |
9 |
Correct |
10 ms |
17780 KB |
Output is correct |
10 |
Correct |
14 ms |
18436 KB |
Output is correct |
11 |
Correct |
12 ms |
18388 KB |
Output is correct |
12 |
Correct |
13 ms |
18392 KB |
Output is correct |
13 |
Correct |
13 ms |
18428 KB |
Output is correct |
14 |
Correct |
8 ms |
17620 KB |
Output is correct |
15 |
Correct |
9 ms |
17620 KB |
Output is correct |
16 |
Correct |
11 ms |
17640 KB |
Output is correct |
17 |
Correct |
8 ms |
17620 KB |
Output is correct |
18 |
Correct |
8 ms |
17620 KB |
Output is correct |
19 |
Correct |
13 ms |
18428 KB |
Output is correct |
20 |
Correct |
8 ms |
17620 KB |
Output is correct |
21 |
Correct |
15 ms |
18444 KB |
Output is correct |
22 |
Correct |
8 ms |
17620 KB |
Output is correct |
23 |
Correct |
16 ms |
18432 KB |
Output is correct |
24 |
Correct |
32 ms |
17540 KB |
Output is correct |
25 |
Correct |
28 ms |
18436 KB |
Output is correct |
26 |
Correct |
7 ms |
17504 KB |
Output is correct |
27 |
Correct |
8 ms |
17492 KB |
Output is correct |
28 |
Correct |
7 ms |
17496 KB |
Output is correct |
29 |
Correct |
1370 ms |
79284 KB |
Output is correct |
30 |
Correct |
524 ms |
75228 KB |
Output is correct |
31 |
Correct |
1355 ms |
96688 KB |
Output is correct |
32 |
Correct |
531 ms |
75388 KB |
Output is correct |
33 |
Correct |
1666 ms |
133308 KB |
Output is correct |
34 |
Correct |
1129 ms |
134048 KB |
Output is correct |
35 |
Correct |
1617 ms |
133312 KB |
Output is correct |
36 |
Correct |
1209 ms |
133840 KB |
Output is correct |
37 |
Correct |
538 ms |
73120 KB |
Output is correct |
38 |
Correct |
732 ms |
124780 KB |
Output is correct |
39 |
Correct |
1182 ms |
130428 KB |
Output is correct |
40 |
Correct |
1161 ms |
130316 KB |
Output is correct |
41 |
Correct |
8 ms |
17492 KB |
Output is correct |
42 |
Correct |
491 ms |
73172 KB |
Output is correct |
43 |
Correct |
726 ms |
124844 KB |
Output is correct |
44 |
Correct |
1243 ms |
130352 KB |
Output is correct |
45 |
Correct |
1129 ms |
130328 KB |
Output is correct |
46 |
Correct |
1310 ms |
127324 KB |
Output is correct |
47 |
Correct |
76 ms |
24652 KB |
Output is correct |
48 |
Correct |
605 ms |
72568 KB |
Output is correct |
49 |
Correct |
953 ms |
75384 KB |
Output is correct |
50 |
Correct |
1778 ms |
130104 KB |
Output is correct |
51 |
Correct |
1734 ms |
130152 KB |
Output is correct |
52 |
Correct |
1693 ms |
130156 KB |
Output is correct |
53 |
Correct |
1822 ms |
130096 KB |
Output is correct |
54 |
Correct |
1714 ms |
130172 KB |
Output is correct |
55 |
Correct |
1640 ms |
130004 KB |
Output is correct |
56 |
Correct |
1683 ms |
130064 KB |
Output is correct |
57 |
Correct |
1893 ms |
130252 KB |
Output is correct |
58 |
Correct |
2035 ms |
130308 KB |
Output is correct |
59 |
Correct |
2009 ms |
130400 KB |
Output is correct |
60 |
Correct |
2013 ms |
130292 KB |
Output is correct |
61 |
Correct |
2117 ms |
130212 KB |
Output is correct |
62 |
Correct |
2161 ms |
130268 KB |
Output is correct |
63 |
Correct |
2117 ms |
130248 KB |
Output is correct |
64 |
Correct |
1150 ms |
130180 KB |
Output is correct |
65 |
Correct |
1761 ms |
130148 KB |
Output is correct |
66 |
Correct |
932 ms |
76572 KB |
Output is correct |
67 |
Correct |
1364 ms |
78608 KB |
Output is correct |
68 |
Correct |
1637 ms |
130076 KB |
Output is correct |
69 |
Correct |
1519 ms |
79548 KB |
Output is correct |
70 |
Correct |
1936 ms |
132976 KB |
Output is correct |
71 |
Correct |
1890 ms |
133296 KB |
Output is correct |
72 |
Correct |
1982 ms |
133244 KB |
Output is correct |
73 |
Correct |
2153 ms |
133232 KB |
Output is correct |
74 |
Correct |
339 ms |
28352 KB |
Output is correct |
75 |
Correct |
1308 ms |
36952 KB |
Output is correct |
76 |
Correct |
1380 ms |
36992 KB |
Output is correct |
77 |
Correct |
266 ms |
28340 KB |
Output is correct |
78 |
Correct |
281 ms |
28384 KB |
Output is correct |
79 |
Correct |
2021 ms |
133428 KB |
Output is correct |
80 |
Correct |
291 ms |
28416 KB |
Output is correct |
81 |
Correct |
2009 ms |
133264 KB |
Output is correct |
82 |
Correct |
280 ms |
28588 KB |
Output is correct |
83 |
Correct |
2512 ms |
133588 KB |
Output is correct |
84 |
Correct |
273 ms |
28592 KB |
Output is correct |
85 |
Correct |
2398 ms |
133772 KB |
Output is correct |
86 |
Correct |
322 ms |
28664 KB |
Output is correct |
87 |
Correct |
2662 ms |
133780 KB |
Output is correct |
88 |
Correct |
353 ms |
28776 KB |
Output is correct |
89 |
Correct |
1095 ms |
85288 KB |
Output is correct |