#include <bits/stdc++.h>
using namespace std;
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int nbit = 18, 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[rson[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 |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9752 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
5 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 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 |
9812 KB |
Output is correct |
18 |
Correct |
5 ms |
9812 KB |
Output is correct |
19 |
Correct |
6 ms |
9684 KB |
Output is correct |
20 |
Correct |
6 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9752 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
5 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 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 |
9812 KB |
Output is correct |
18 |
Correct |
5 ms |
9812 KB |
Output is correct |
19 |
Correct |
6 ms |
9684 KB |
Output is correct |
20 |
Correct |
6 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
23 |
Correct |
7 ms |
10196 KB |
Output is correct |
24 |
Correct |
8 ms |
10228 KB |
Output is correct |
25 |
Correct |
8 ms |
10196 KB |
Output is correct |
26 |
Correct |
7 ms |
10208 KB |
Output is correct |
27 |
Correct |
7 ms |
10196 KB |
Output is correct |
28 |
Correct |
7 ms |
10164 KB |
Output is correct |
29 |
Correct |
7 ms |
10196 KB |
Output is correct |
30 |
Correct |
8 ms |
10196 KB |
Output is correct |
31 |
Correct |
8 ms |
10196 KB |
Output is correct |
32 |
Correct |
9 ms |
10340 KB |
Output is correct |
33 |
Correct |
7 ms |
10324 KB |
Output is correct |
34 |
Correct |
7 ms |
10196 KB |
Output is correct |
35 |
Correct |
8 ms |
10196 KB |
Output is correct |
36 |
Correct |
7 ms |
10292 KB |
Output is correct |
37 |
Correct |
7 ms |
10324 KB |
Output is correct |
38 |
Correct |
7 ms |
10420 KB |
Output is correct |
39 |
Correct |
8 ms |
10196 KB |
Output is correct |
40 |
Correct |
8 ms |
10260 KB |
Output is correct |
41 |
Correct |
7 ms |
10196 KB |
Output is correct |
42 |
Correct |
8 ms |
10196 KB |
Output is correct |
43 |
Correct |
8 ms |
10324 KB |
Output is correct |
44 |
Correct |
7 ms |
10196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9752 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
5 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 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 |
9812 KB |
Output is correct |
18 |
Correct |
5 ms |
9812 KB |
Output is correct |
19 |
Correct |
6 ms |
9684 KB |
Output is correct |
20 |
Correct |
6 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
23 |
Correct |
7 ms |
10196 KB |
Output is correct |
24 |
Correct |
8 ms |
10228 KB |
Output is correct |
25 |
Correct |
8 ms |
10196 KB |
Output is correct |
26 |
Correct |
7 ms |
10208 KB |
Output is correct |
27 |
Correct |
7 ms |
10196 KB |
Output is correct |
28 |
Correct |
7 ms |
10164 KB |
Output is correct |
29 |
Correct |
7 ms |
10196 KB |
Output is correct |
30 |
Correct |
8 ms |
10196 KB |
Output is correct |
31 |
Correct |
8 ms |
10196 KB |
Output is correct |
32 |
Correct |
9 ms |
10340 KB |
Output is correct |
33 |
Correct |
7 ms |
10324 KB |
Output is correct |
34 |
Correct |
7 ms |
10196 KB |
Output is correct |
35 |
Correct |
8 ms |
10196 KB |
Output is correct |
36 |
Correct |
7 ms |
10292 KB |
Output is correct |
37 |
Correct |
7 ms |
10324 KB |
Output is correct |
38 |
Correct |
7 ms |
10420 KB |
Output is correct |
39 |
Correct |
8 ms |
10196 KB |
Output is correct |
40 |
Correct |
8 ms |
10260 KB |
Output is correct |
41 |
Correct |
7 ms |
10196 KB |
Output is correct |
42 |
Correct |
8 ms |
10196 KB |
Output is correct |
43 |
Correct |
8 ms |
10324 KB |
Output is correct |
44 |
Correct |
7 ms |
10196 KB |
Output is correct |
45 |
Correct |
422 ms |
60784 KB |
Output is correct |
46 |
Correct |
414 ms |
60108 KB |
Output is correct |
47 |
Correct |
419 ms |
59472 KB |
Output is correct |
48 |
Correct |
419 ms |
60840 KB |
Output is correct |
49 |
Correct |
409 ms |
59096 KB |
Output is correct |
50 |
Correct |
414 ms |
59460 KB |
Output is correct |
51 |
Correct |
406 ms |
59364 KB |
Output is correct |
52 |
Correct |
427 ms |
60504 KB |
Output is correct |
53 |
Correct |
414 ms |
60236 KB |
Output is correct |
54 |
Correct |
588 ms |
71172 KB |
Output is correct |
55 |
Correct |
588 ms |
67020 KB |
Output is correct |
56 |
Correct |
547 ms |
65216 KB |
Output is correct |
57 |
Correct |
585 ms |
63552 KB |
Output is correct |
58 |
Correct |
462 ms |
68296 KB |
Output is correct |
59 |
Correct |
463 ms |
67480 KB |
Output is correct |
60 |
Correct |
316 ms |
78472 KB |
Output is correct |
61 |
Correct |
400 ms |
60800 KB |
Output is correct |
62 |
Correct |
599 ms |
71852 KB |
Output is correct |
63 |
Correct |
408 ms |
64988 KB |
Output is correct |
64 |
Correct |
397 ms |
64100 KB |
Output is correct |
65 |
Correct |
574 ms |
78020 KB |
Output is correct |
66 |
Correct |
404 ms |
64464 KB |
Output is correct |