#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(){
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int q;
cin >> q >> d;
for (int i=1;i<=q;i++){
cin >> 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
17492 KB |
Output is correct |
2 |
Correct |
7 ms |
17568 KB |
Output is correct |
3 |
Correct |
8 ms |
17492 KB |
Output is correct |
4 |
Correct |
9 ms |
17492 KB |
Output is correct |
5 |
Correct |
8 ms |
17492 KB |
Output is correct |
6 |
Correct |
11 ms |
18004 KB |
Output is correct |
7 |
Correct |
11 ms |
17952 KB |
Output is correct |
8 |
Correct |
11 ms |
17828 KB |
Output is correct |
9 |
Correct |
11 ms |
17800 KB |
Output is correct |
10 |
Correct |
18 ms |
18516 KB |
Output is correct |
11 |
Correct |
13 ms |
18516 KB |
Output is correct |
12 |
Correct |
14 ms |
18516 KB |
Output is correct |
13 |
Correct |
21 ms |
18564 KB |
Output is correct |
14 |
Correct |
10 ms |
17620 KB |
Output is correct |
15 |
Correct |
11 ms |
17648 KB |
Output is correct |
16 |
Correct |
12 ms |
17712 KB |
Output is correct |
17 |
Correct |
8 ms |
17676 KB |
Output is correct |
18 |
Correct |
12 ms |
17712 KB |
Output is correct |
19 |
Correct |
12 ms |
18516 KB |
Output is correct |
20 |
Correct |
8 ms |
17608 KB |
Output is correct |
21 |
Correct |
14 ms |
18500 KB |
Output is correct |
22 |
Correct |
7 ms |
17652 KB |
Output is correct |
23 |
Correct |
22 ms |
18456 KB |
Output is correct |
24 |
Correct |
8 ms |
17620 KB |
Output is correct |
25 |
Correct |
13 ms |
18516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
17492 KB |
Output is correct |
2 |
Correct |
7 ms |
17492 KB |
Output is correct |
3 |
Correct |
7 ms |
17572 KB |
Output is correct |
4 |
Correct |
1241 ms |
79420 KB |
Output is correct |
5 |
Correct |
562 ms |
75448 KB |
Output is correct |
6 |
Correct |
1566 ms |
96784 KB |
Output is correct |
7 |
Correct |
694 ms |
75432 KB |
Output is correct |
8 |
Correct |
1937 ms |
133288 KB |
Output is correct |
9 |
Correct |
1272 ms |
133960 KB |
Output is correct |
10 |
Correct |
1852 ms |
133292 KB |
Output is correct |
11 |
Correct |
1255 ms |
133888 KB |
Output is correct |
12 |
Correct |
515 ms |
73252 KB |
Output is correct |
13 |
Correct |
741 ms |
124892 KB |
Output is correct |
14 |
Correct |
1264 ms |
130340 KB |
Output is correct |
15 |
Correct |
1194 ms |
130416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
17492 KB |
Output is correct |
2 |
Correct |
538 ms |
73244 KB |
Output is correct |
3 |
Correct |
803 ms |
125020 KB |
Output is correct |
4 |
Correct |
1535 ms |
130412 KB |
Output is correct |
5 |
Correct |
1232 ms |
130616 KB |
Output is correct |
6 |
Correct |
1372 ms |
127436 KB |
Output is correct |
7 |
Correct |
107 ms |
24736 KB |
Output is correct |
8 |
Correct |
820 ms |
72664 KB |
Output is correct |
9 |
Correct |
1247 ms |
75512 KB |
Output is correct |
10 |
Correct |
2083 ms |
130188 KB |
Output is correct |
11 |
Correct |
2119 ms |
130240 KB |
Output is correct |
12 |
Correct |
2313 ms |
130196 KB |
Output is correct |
13 |
Correct |
2432 ms |
130228 KB |
Output is correct |
14 |
Correct |
2180 ms |
130252 KB |
Output is correct |
15 |
Correct |
2411 ms |
129920 KB |
Output is correct |
16 |
Correct |
2459 ms |
130164 KB |
Output is correct |
17 |
Correct |
2284 ms |
130328 KB |
Output is correct |
18 |
Correct |
2554 ms |
130256 KB |
Output is correct |
19 |
Correct |
2594 ms |
130272 KB |
Output is correct |
20 |
Correct |
2720 ms |
130332 KB |
Output is correct |
21 |
Correct |
3176 ms |
130244 KB |
Output is correct |
22 |
Correct |
3044 ms |
130344 KB |
Output is correct |
23 |
Correct |
2563 ms |
130236 KB |
Output is correct |
24 |
Correct |
1233 ms |
130348 KB |
Output is correct |
25 |
Correct |
2133 ms |
130336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
17492 KB |
Output is correct |
2 |
Correct |
7 ms |
17568 KB |
Output is correct |
3 |
Correct |
8 ms |
17492 KB |
Output is correct |
4 |
Correct |
9 ms |
17492 KB |
Output is correct |
5 |
Correct |
8 ms |
17492 KB |
Output is correct |
6 |
Correct |
11 ms |
18004 KB |
Output is correct |
7 |
Correct |
11 ms |
17952 KB |
Output is correct |
8 |
Correct |
11 ms |
17828 KB |
Output is correct |
9 |
Correct |
11 ms |
17800 KB |
Output is correct |
10 |
Correct |
18 ms |
18516 KB |
Output is correct |
11 |
Correct |
13 ms |
18516 KB |
Output is correct |
12 |
Correct |
14 ms |
18516 KB |
Output is correct |
13 |
Correct |
21 ms |
18564 KB |
Output is correct |
14 |
Correct |
10 ms |
17620 KB |
Output is correct |
15 |
Correct |
11 ms |
17648 KB |
Output is correct |
16 |
Correct |
12 ms |
17712 KB |
Output is correct |
17 |
Correct |
8 ms |
17676 KB |
Output is correct |
18 |
Correct |
12 ms |
17712 KB |
Output is correct |
19 |
Correct |
12 ms |
18516 KB |
Output is correct |
20 |
Correct |
8 ms |
17608 KB |
Output is correct |
21 |
Correct |
14 ms |
18500 KB |
Output is correct |
22 |
Correct |
7 ms |
17652 KB |
Output is correct |
23 |
Correct |
22 ms |
18456 KB |
Output is correct |
24 |
Correct |
8 ms |
17620 KB |
Output is correct |
25 |
Correct |
13 ms |
18516 KB |
Output is correct |
26 |
Correct |
8 ms |
17492 KB |
Output is correct |
27 |
Correct |
7 ms |
17492 KB |
Output is correct |
28 |
Correct |
7 ms |
17572 KB |
Output is correct |
29 |
Correct |
1241 ms |
79420 KB |
Output is correct |
30 |
Correct |
562 ms |
75448 KB |
Output is correct |
31 |
Correct |
1566 ms |
96784 KB |
Output is correct |
32 |
Correct |
694 ms |
75432 KB |
Output is correct |
33 |
Correct |
1937 ms |
133288 KB |
Output is correct |
34 |
Correct |
1272 ms |
133960 KB |
Output is correct |
35 |
Correct |
1852 ms |
133292 KB |
Output is correct |
36 |
Correct |
1255 ms |
133888 KB |
Output is correct |
37 |
Correct |
515 ms |
73252 KB |
Output is correct |
38 |
Correct |
741 ms |
124892 KB |
Output is correct |
39 |
Correct |
1264 ms |
130340 KB |
Output is correct |
40 |
Correct |
1194 ms |
130416 KB |
Output is correct |
41 |
Correct |
7 ms |
17492 KB |
Output is correct |
42 |
Correct |
538 ms |
73244 KB |
Output is correct |
43 |
Correct |
803 ms |
125020 KB |
Output is correct |
44 |
Correct |
1535 ms |
130412 KB |
Output is correct |
45 |
Correct |
1232 ms |
130616 KB |
Output is correct |
46 |
Correct |
1372 ms |
127436 KB |
Output is correct |
47 |
Correct |
107 ms |
24736 KB |
Output is correct |
48 |
Correct |
820 ms |
72664 KB |
Output is correct |
49 |
Correct |
1247 ms |
75512 KB |
Output is correct |
50 |
Correct |
2083 ms |
130188 KB |
Output is correct |
51 |
Correct |
2119 ms |
130240 KB |
Output is correct |
52 |
Correct |
2313 ms |
130196 KB |
Output is correct |
53 |
Correct |
2432 ms |
130228 KB |
Output is correct |
54 |
Correct |
2180 ms |
130252 KB |
Output is correct |
55 |
Correct |
2411 ms |
129920 KB |
Output is correct |
56 |
Correct |
2459 ms |
130164 KB |
Output is correct |
57 |
Correct |
2284 ms |
130328 KB |
Output is correct |
58 |
Correct |
2554 ms |
130256 KB |
Output is correct |
59 |
Correct |
2594 ms |
130272 KB |
Output is correct |
60 |
Correct |
2720 ms |
130332 KB |
Output is correct |
61 |
Correct |
3176 ms |
130244 KB |
Output is correct |
62 |
Correct |
3044 ms |
130344 KB |
Output is correct |
63 |
Correct |
2563 ms |
130236 KB |
Output is correct |
64 |
Correct |
1233 ms |
130348 KB |
Output is correct |
65 |
Correct |
2133 ms |
130336 KB |
Output is correct |
66 |
Correct |
930 ms |
76780 KB |
Output is correct |
67 |
Correct |
1101 ms |
78716 KB |
Output is correct |
68 |
Correct |
1316 ms |
130224 KB |
Output is correct |
69 |
Correct |
1232 ms |
79760 KB |
Output is correct |
70 |
Correct |
1674 ms |
133228 KB |
Output is correct |
71 |
Correct |
1778 ms |
133460 KB |
Output is correct |
72 |
Correct |
1748 ms |
133452 KB |
Output is correct |
73 |
Correct |
1804 ms |
133488 KB |
Output is correct |
74 |
Correct |
171 ms |
28556 KB |
Output is correct |
75 |
Correct |
1065 ms |
37104 KB |
Output is correct |
76 |
Correct |
1052 ms |
37208 KB |
Output is correct |
77 |
Correct |
168 ms |
28536 KB |
Output is correct |
78 |
Correct |
174 ms |
28428 KB |
Output is correct |
79 |
Correct |
1893 ms |
133480 KB |
Output is correct |
80 |
Correct |
178 ms |
28472 KB |
Output is correct |
81 |
Correct |
1767 ms |
133408 KB |
Output is correct |
82 |
Correct |
167 ms |
28432 KB |
Output is correct |
83 |
Correct |
2068 ms |
133556 KB |
Output is correct |
84 |
Correct |
173 ms |
28476 KB |
Output is correct |
85 |
Correct |
2190 ms |
133560 KB |
Output is correct |
86 |
Correct |
237 ms |
28432 KB |
Output is correct |
87 |
Correct |
2168 ms |
133568 KB |
Output is correct |
88 |
Correct |
181 ms |
28472 KB |
Output is correct |
89 |
Correct |
919 ms |
85048 KB |
Output is correct |