#include <bits/stdc++.h>
using namespace std;
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int nbit = 17, nmax = 2e5 + 5;
#define lsb(x) (x & -x)
struct AIB {
vector<ll> tree; int len;
void init(int n) {
len = n;
tree.clear(), tree.resize(n + 1, 0);
}
void upd(int x, int val) {
while(x <= len)
tree[x] += val,
x += lsb(x);
return;
}
void upd(int l, int r, int val) {
upd(l, val);
upd(r + 1, -val);
}
int query(int x) {
int sum = 0;
while(x > 0) sum += tree[x], x -= lsb(x);
return sum;
}
};
int n, m;
namespace CartTree {
vector<int> g[nmax];
vector<pii> atrendpoint[nmax];
int p[nmax][nbit], lson[nmax], rson[nmax], pin[nmax], pout[nmax], inp = 1;
static int root;
vector<int> v;
ll dp[nmax], son[nmax];
AIB pinexsum;
void build(vector<int> abogus) {
v = move(abogus);
root = 0;
for(int i = 0; i < n; i++) { //init carttree
int lst = i - 1, lastval = -1;
lson[i] = rson[i] = -1;
while(lst >= 0 && v[lst] <= v[i])
lastval = lst,
lst = p[lst][0];
p[i][0] = lst;
lson[i] = lastval;
if(lastval != -1)
p[lastval][0] = i;
if(lst != -1)
rson[lst] = i;
}
for(int i = 0; i < n; i++) { // init tree
if(lson[i] != -1)
g[i].push_back(lson[i]),
p[lson[i]][0] = i;
if(rson[i] != -1)
g[i].push_back(rson[i]),
p[lson[i]][0] = i;
if(p[i][0] == -1)
root = i;
}
p[root][0] = root;
for(int step = 1; step < nbit; step++)
for(int i = 0; i < n; i++)
p[i][step] = p[p[i][step - 1]][step - 1];
auto dfs = [&](auto &&self, int node) -> void {
pin[node] = inp++;
for(auto x : g[node])
self(self, x);
pout[node] = inp - 1;
};
dfs(dfs, root);
pinexsum.init(n + 1);
}
void addstar(int x, int height, int cost) {
int temp = x;
for(int i = nbit - 1; i >= 0; i--) {
if(v[p[x][i]] < height)
x = p[x][i];
}
atrendpoint[x].emplace_back(temp, cost);
}
void startdp(int node = root) {
for(auto x : g[node])
startdp(x),
son[node] += dp[x];
dp[node] = son[node];
pinexsum.upd(pin[node], pout[node], son[node]);
for(auto [x, cost] : atrendpoint[node])
dp[node] = max(dp[node], (ll)cost + pinexsum.query(pin[x]));
pinexsum.upd(pin[node], pout[node], -dp[node]);
}
}
signed main() {
cin >> n;
vector<int> heights(n);
for(auto &x : heights)
cin >> x;
CartTree::build(heights);
cin >> m;
ll sum = 0;
for(int i = 0, x, y, c; i < m; i++) {
cin >> x >> y >> c;
CartTree::addstar(x - 1, y, c);
sum += c;
}
CartTree::startdp();
cout << sum - CartTree::dp[CartTree::root] << '\n';
}
/*
*
* 6 A 8 x 5
* x x x x
* x x x x x
* x x x x x
* x x 7 x x x x
* x x x x x x x
* x x x x x x x
*
*
*/
Compilation message
constellation3.cpp: In function 'void CartTree::startdp(ll)':
constellation3.cpp:98:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
98 | for(auto [x, cost] : atrendpoint[node])
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9716 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
5 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9740 KB |
Output is correct |
8 |
Correct |
6 ms |
9716 KB |
Output is correct |
9 |
Correct |
7 ms |
9812 KB |
Output is correct |
10 |
Correct |
5 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9716 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9720 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
5 ms |
9812 KB |
Output is correct |
17 |
Correct |
5 ms |
9728 KB |
Output is correct |
18 |
Correct |
6 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9740 KB |
Output is correct |
20 |
Correct |
5 ms |
9712 KB |
Output is correct |
21 |
Correct |
5 ms |
9836 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9716 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
5 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9740 KB |
Output is correct |
8 |
Correct |
6 ms |
9716 KB |
Output is correct |
9 |
Correct |
7 ms |
9812 KB |
Output is correct |
10 |
Correct |
5 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9716 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9720 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
5 ms |
9812 KB |
Output is correct |
17 |
Correct |
5 ms |
9728 KB |
Output is correct |
18 |
Correct |
6 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9740 KB |
Output is correct |
20 |
Correct |
5 ms |
9712 KB |
Output is correct |
21 |
Correct |
5 ms |
9836 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
23 |
Correct |
10 ms |
10252 KB |
Output is correct |
24 |
Correct |
10 ms |
10236 KB |
Output is correct |
25 |
Correct |
9 ms |
10196 KB |
Output is correct |
26 |
Correct |
8 ms |
10196 KB |
Output is correct |
27 |
Correct |
8 ms |
10196 KB |
Output is correct |
28 |
Correct |
8 ms |
10196 KB |
Output is correct |
29 |
Correct |
9 ms |
10196 KB |
Output is correct |
30 |
Correct |
8 ms |
10208 KB |
Output is correct |
31 |
Correct |
8 ms |
10236 KB |
Output is correct |
32 |
Correct |
7 ms |
10324 KB |
Output is correct |
33 |
Correct |
8 ms |
10324 KB |
Output is correct |
34 |
Correct |
8 ms |
10324 KB |
Output is correct |
35 |
Correct |
8 ms |
10324 KB |
Output is correct |
36 |
Correct |
8 ms |
10324 KB |
Output is correct |
37 |
Correct |
8 ms |
10284 KB |
Output is correct |
38 |
Correct |
7 ms |
10452 KB |
Output is correct |
39 |
Correct |
7 ms |
10196 KB |
Output is correct |
40 |
Correct |
8 ms |
10324 KB |
Output is correct |
41 |
Correct |
7 ms |
10236 KB |
Output is correct |
42 |
Correct |
8 ms |
10200 KB |
Output is correct |
43 |
Correct |
8 ms |
10316 KB |
Output is correct |
44 |
Correct |
7 ms |
10244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9716 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
5 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9740 KB |
Output is correct |
8 |
Correct |
6 ms |
9716 KB |
Output is correct |
9 |
Correct |
7 ms |
9812 KB |
Output is correct |
10 |
Correct |
5 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9716 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9720 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
5 ms |
9812 KB |
Output is correct |
17 |
Correct |
5 ms |
9728 KB |
Output is correct |
18 |
Correct |
6 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9740 KB |
Output is correct |
20 |
Correct |
5 ms |
9712 KB |
Output is correct |
21 |
Correct |
5 ms |
9836 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
23 |
Correct |
10 ms |
10252 KB |
Output is correct |
24 |
Correct |
10 ms |
10236 KB |
Output is correct |
25 |
Correct |
9 ms |
10196 KB |
Output is correct |
26 |
Correct |
8 ms |
10196 KB |
Output is correct |
27 |
Correct |
8 ms |
10196 KB |
Output is correct |
28 |
Correct |
8 ms |
10196 KB |
Output is correct |
29 |
Correct |
9 ms |
10196 KB |
Output is correct |
30 |
Correct |
8 ms |
10208 KB |
Output is correct |
31 |
Correct |
8 ms |
10236 KB |
Output is correct |
32 |
Correct |
7 ms |
10324 KB |
Output is correct |
33 |
Correct |
8 ms |
10324 KB |
Output is correct |
34 |
Correct |
8 ms |
10324 KB |
Output is correct |
35 |
Correct |
8 ms |
10324 KB |
Output is correct |
36 |
Correct |
8 ms |
10324 KB |
Output is correct |
37 |
Correct |
8 ms |
10284 KB |
Output is correct |
38 |
Correct |
7 ms |
10452 KB |
Output is correct |
39 |
Correct |
7 ms |
10196 KB |
Output is correct |
40 |
Correct |
8 ms |
10324 KB |
Output is correct |
41 |
Correct |
7 ms |
10236 KB |
Output is correct |
42 |
Correct |
8 ms |
10200 KB |
Output is correct |
43 |
Correct |
8 ms |
10316 KB |
Output is correct |
44 |
Correct |
7 ms |
10244 KB |
Output is correct |
45 |
Correct |
460 ms |
64824 KB |
Output is correct |
46 |
Correct |
416 ms |
64228 KB |
Output is correct |
47 |
Correct |
450 ms |
63688 KB |
Output is correct |
48 |
Correct |
435 ms |
64760 KB |
Output is correct |
49 |
Correct |
428 ms |
63184 KB |
Output is correct |
50 |
Correct |
427 ms |
63204 KB |
Output is correct |
51 |
Correct |
432 ms |
63268 KB |
Output is correct |
52 |
Correct |
471 ms |
64608 KB |
Output is correct |
53 |
Correct |
450 ms |
64460 KB |
Output is correct |
54 |
Correct |
622 ms |
75260 KB |
Output is correct |
55 |
Correct |
598 ms |
71224 KB |
Output is correct |
56 |
Correct |
589 ms |
69104 KB |
Output is correct |
57 |
Correct |
581 ms |
67672 KB |
Output is correct |
58 |
Correct |
513 ms |
72060 KB |
Output is correct |
59 |
Correct |
505 ms |
71828 KB |
Output is correct |
60 |
Correct |
302 ms |
81484 KB |
Output is correct |
61 |
Correct |
427 ms |
64372 KB |
Output is correct |
62 |
Incorrect |
599 ms |
75944 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |