#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[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])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9804 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 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 |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 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 |
9824 KB |
Output is correct |
13 |
Correct |
6 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 |
9828 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 |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
8 ms |
9796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9804 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 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 |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 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 |
9824 KB |
Output is correct |
13 |
Correct |
6 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 |
9828 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 |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
8 ms |
9796 KB |
Output is correct |
23 |
Correct |
9 ms |
10196 KB |
Output is correct |
24 |
Correct |
8 ms |
10196 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 |
10140 KB |
Output is correct |
28 |
Correct |
7 ms |
10196 KB |
Output is correct |
29 |
Correct |
8 ms |
10196 KB |
Output is correct |
30 |
Correct |
10 ms |
10196 KB |
Output is correct |
31 |
Correct |
10 ms |
10196 KB |
Output is correct |
32 |
Correct |
10 ms |
10324 KB |
Output is correct |
33 |
Correct |
8 ms |
10196 KB |
Output is correct |
34 |
Correct |
8 ms |
10212 KB |
Output is correct |
35 |
Correct |
8 ms |
10284 KB |
Output is correct |
36 |
Correct |
9 ms |
10324 KB |
Output is correct |
37 |
Correct |
7 ms |
10324 KB |
Output is correct |
38 |
Correct |
10 ms |
10324 KB |
Output is correct |
39 |
Correct |
11 ms |
10196 KB |
Output is correct |
40 |
Correct |
7 ms |
10336 KB |
Output is correct |
41 |
Correct |
10 ms |
10196 KB |
Output is correct |
42 |
Correct |
11 ms |
10152 KB |
Output is correct |
43 |
Correct |
7 ms |
10324 KB |
Output is correct |
44 |
Correct |
7 ms |
10156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9804 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 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 |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 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 |
9824 KB |
Output is correct |
13 |
Correct |
6 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 |
9828 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 |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
8 ms |
9796 KB |
Output is correct |
23 |
Correct |
9 ms |
10196 KB |
Output is correct |
24 |
Correct |
8 ms |
10196 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 |
10140 KB |
Output is correct |
28 |
Correct |
7 ms |
10196 KB |
Output is correct |
29 |
Correct |
8 ms |
10196 KB |
Output is correct |
30 |
Correct |
10 ms |
10196 KB |
Output is correct |
31 |
Correct |
10 ms |
10196 KB |
Output is correct |
32 |
Correct |
10 ms |
10324 KB |
Output is correct |
33 |
Correct |
8 ms |
10196 KB |
Output is correct |
34 |
Correct |
8 ms |
10212 KB |
Output is correct |
35 |
Correct |
8 ms |
10284 KB |
Output is correct |
36 |
Correct |
9 ms |
10324 KB |
Output is correct |
37 |
Correct |
7 ms |
10324 KB |
Output is correct |
38 |
Correct |
10 ms |
10324 KB |
Output is correct |
39 |
Correct |
11 ms |
10196 KB |
Output is correct |
40 |
Correct |
7 ms |
10336 KB |
Output is correct |
41 |
Correct |
10 ms |
10196 KB |
Output is correct |
42 |
Correct |
11 ms |
10152 KB |
Output is correct |
43 |
Correct |
7 ms |
10324 KB |
Output is correct |
44 |
Correct |
7 ms |
10156 KB |
Output is correct |
45 |
Correct |
461 ms |
59252 KB |
Output is correct |
46 |
Correct |
445 ms |
58612 KB |
Output is correct |
47 |
Correct |
490 ms |
57920 KB |
Output is correct |
48 |
Correct |
444 ms |
59340 KB |
Output is correct |
49 |
Correct |
463 ms |
57640 KB |
Output is correct |
50 |
Correct |
416 ms |
57596 KB |
Output is correct |
51 |
Correct |
504 ms |
57856 KB |
Output is correct |
52 |
Correct |
449 ms |
58732 KB |
Output is correct |
53 |
Correct |
494 ms |
58740 KB |
Output is correct |
54 |
Correct |
643 ms |
69700 KB |
Output is correct |
55 |
Correct |
615 ms |
65520 KB |
Output is correct |
56 |
Correct |
695 ms |
63640 KB |
Output is correct |
57 |
Correct |
710 ms |
62060 KB |
Output is correct |
58 |
Correct |
508 ms |
66720 KB |
Output is correct |
59 |
Correct |
473 ms |
66020 KB |
Output is correct |
60 |
Correct |
319 ms |
77000 KB |
Output is correct |
61 |
Correct |
403 ms |
59292 KB |
Output is correct |
62 |
Incorrect |
634 ms |
70376 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |