#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define MAX 301010
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
struct Star {
ll x, y, c, ban;
Star() :x(0), y(0), c(0), ban(0) {}
Star(ll x, ll y, ll c) :x(x), y(y), c(c), ban(0) {}
};
struct segtree {
vector<ll> l, r, tree;
ll s;
ll type;
void init(ll x = 1) {
if (x >= s) {
l[x] = r[x] = x - s + 1;
return;
}
init(x * 2);
init(x * 2 + 1);
l[x] = l[x * 2];
r[x] = r[x * 2 + 1];
if (type == 1) tree[x] = tree[x * 2] + tree[x * 2 + 1];
else tree[x] = max(tree[x * 2], tree[x * 2 + 1]);
}
void update(ll x, ll a) {
x += s - 1;
tree[x] = a;
x /= 2;
while (x) {
if (type == 1) tree[x] = tree[x * 2] + tree[x * 2 + 1];
else tree[x] = max(tree[x * 2], tree[x * 2 + 1]);
x /= 2;
}
}
ll query(ll low, ll high, ll loc = 1) {
if (l[loc] == low && r[loc] == high) return tree[loc];
if (high <= r[loc * 2]) return query(low, high, loc * 2);
if (low >= l[loc * 2 + 1]) return query(low, high, loc * 2 + 1);
if (type == 1) return query(low, r[loc * 2], loc * 2) + query(l[loc * 2 + 1], high, loc * 2 + 1);
else return max(query(low, r[loc * 2], loc * 2), query(l[loc * 2 + 1], high, loc * 2 + 1));
}
segtree(ll N) {
type = 1;
s = 1LL << (ll)ceil(log2(N));
tree.resize(2 * s + 1);
l.resize(2 * s + 1);
r.resize(2 * s + 1);
}
segtree() {
}
};
ll A[MAX];
ll lc[MAX], rc[MAX];
ll sp[MAX][MAXS];
ll bottom[MAX], revb[MAX];
ll sz[MAX];
ll sum[MAX];
ll ans[MAX];
ll depth[MAX];
Star star[MAX];
vector<ll> cart[MAX];
vector<vector<ll>> chain;
pll cnum[MAX];
vector<segtree> chainseg;
ll S, N, M;
ll cnt;
segtree aseg;
ll make_tree(ll l, ll r, ll num, ll p, ll d) {
sz[num] = r - l + 1;
ll res = aseg.query(l, r);
ll mid = res % 1000000;
bottom[mid] = num;
depth[num] = d;
revb[num] = mid;
sp[num][0] = p;
for (ll i = 1; i < MAXS; i++) sp[num][i] = sp[sp[num][i - 1]][i - 1];
if (l <= mid - 1) lc[num] = make_tree(l, mid - 1, ++S, num, d + 1);
else lc[num] = 0;
if (mid + 1 <= r) rc[num] = make_tree(mid + 1, r, ++S, num, d + 1);
else rc[num] = 0;
return num;
}
void assign_star() {
ll i, j;
for (i = 1; i <= M; i++) {
ll v = bottom[star[i].x];
star[i].ban = v;
for (j = MAXS - 1; j >= 0; j--) {
if (A[revb[sp[v][j]]] < star[i].y) v = sp[v][j];
}
cart[v].push_back(i);
}
}
void make_chain(ll x, ll p = 0) {
chain[cnt].push_back(x);
cnum[x] = { cnt, chain[cnt].size() - 1 };
ll a, b;
a = lc[x], b = rc[x];
if (sz[lc[x]] < sz[rc[x]]) swap(a, b);
if (a) make_chain(a, x);
if (b) {
cnt++;
chain.push_back(vector<ll>());
make_chain(b, x);
}
}
void make_seg() {
for (ll i = 0; i < chain.size(); i++) chainseg.push_back(segtree(chain[i].size()));
for (ll i = 0; i < chain.size(); i++) chainseg[i].init();
}
ll lca(ll u, ll v) {
if (!(u * v)) return -1;
if (u == v) return u;
ll i;
if (depth[u] != depth[v]) {
if (depth[u] > depth[v]) swap(u, v);
for (i = MAXS - 1; i >= 0; i--) if (depth[sp[v][i]] >= depth[u]) v = sp[v][i];
}
if (u == v) return u;
for (i = MAXS - 1; i >= 0; i--) if (sp[u][i] != sp[v][i]) u = sp[u][i], v = sp[v][i];
return sp[u][0];
}
void update(ll x, ll a) {
if (!x) return;
chainseg[cnum[x].first].update(cnum[x].second + 1, a);
}
ll f(ll x) {
ll s;
if (lc[sp[x][0]] == x) s = rc[sp[x][0]];
else s = lc[sp[x][0]];
return s;
}
ll query(ll u, ll v) {
ll ret = 0;
ll l = lca(u, v);
while (cnum[u].first != cnum[l].first) ret += chainseg[cnum[u].first].query(1, cnum[u].second + 1), u = sp[chain[cnum[u].first][0]][0];
while (cnum[v].first != cnum[l].first) ret += chainseg[cnum[v].first].query(1, cnum[v].second + 1), v = sp[chain[cnum[v].first][0]][0];
ret += chainseg[cnum[l].first].query(cnum[l].second + 1, cnum[u].second + 1);
ret += chainseg[cnum[l].first].query(cnum[l].second + 1, cnum[v].second + 1);
ret -= chainseg[cnum[l].first].query(cnum[l].second + 1, cnum[l].second + 1);
return ret;
}
void calc(ll x = 1, ll p = 0) {
if (!x) return;
calc(lc[x]);
calc(rc[x]);
sum[x] = ans[lc[x]] + ans[rc[x]];
ans[x] = sum[x];
for (auto asdf : cart[x]) {
Star st = star[asdf];
ll b = st.ban;
if (b == x) ans[x] = max(sum[x] + st.c, ans[x]);
else {
ll s;
if (lca(b, lc[x]) == lc[x]) s = lc[x];
else s = rc[x];
ans[x] = max(sum[b] + query(b, s) + st.c, ans[x]);
}
}
if (x != 1) update(f(x), ans[x]);
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> N;
A[0] = INF;
ll i;
aseg = segtree(N);
aseg.type = 0;
for (i = 1; i <= N; i++) cin >> A[i], aseg.tree[i + aseg.s - 1] = 1000000 * A[i] + i;
aseg.init();
cin >> M;
ll asdfsdf = 0;
for (i = 1; i <= M; i++) cin >> star[i].x >> star[i].y >> star[i].c, asdfsdf += star[i].c;
S = 1;
make_tree(1, N, 1, 0, 1);
assign_star();
chain.push_back(vector<ll>());
make_chain(1);
make_seg();
calc();
cout << asdfsdf - ans[1] << ln;
}
Compilation message
constellation3.cpp: In function 'void make_seg()':
constellation3.cpp:119:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for (ll i = 0; i < chain.size(); i++) chainseg.push_back(segtree(chain[i].size()));
| ~~^~~~~~~~~~~~~~
constellation3.cpp:120:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (ll i = 0; i < chain.size(); i++) chainseg[i].init();
| ~~^~~~~~~~~~~~~~
constellation3.cpp: In function 'll lca(ll, ll)':
constellation3.cpp:123:10: warning: '*' in boolean context, suggest '&&' instead [-Wint-in-bool-context]
123 | if (!(u * v)) return -1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16972 KB |
Output is correct |
2 |
Correct |
9 ms |
16948 KB |
Output is correct |
3 |
Correct |
9 ms |
16952 KB |
Output is correct |
4 |
Correct |
9 ms |
16948 KB |
Output is correct |
5 |
Correct |
9 ms |
17008 KB |
Output is correct |
6 |
Correct |
10 ms |
17008 KB |
Output is correct |
7 |
Correct |
9 ms |
16972 KB |
Output is correct |
8 |
Correct |
9 ms |
16972 KB |
Output is correct |
9 |
Correct |
9 ms |
16972 KB |
Output is correct |
10 |
Correct |
9 ms |
16952 KB |
Output is correct |
11 |
Correct |
9 ms |
16972 KB |
Output is correct |
12 |
Correct |
9 ms |
16948 KB |
Output is correct |
13 |
Correct |
9 ms |
16996 KB |
Output is correct |
14 |
Correct |
9 ms |
17016 KB |
Output is correct |
15 |
Correct |
9 ms |
16972 KB |
Output is correct |
16 |
Correct |
9 ms |
16972 KB |
Output is correct |
17 |
Correct |
10 ms |
16920 KB |
Output is correct |
18 |
Correct |
9 ms |
16972 KB |
Output is correct |
19 |
Correct |
10 ms |
17048 KB |
Output is correct |
20 |
Correct |
9 ms |
16952 KB |
Output is correct |
21 |
Correct |
9 ms |
16972 KB |
Output is correct |
22 |
Correct |
10 ms |
17004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16972 KB |
Output is correct |
2 |
Correct |
9 ms |
16948 KB |
Output is correct |
3 |
Correct |
9 ms |
16952 KB |
Output is correct |
4 |
Correct |
9 ms |
16948 KB |
Output is correct |
5 |
Correct |
9 ms |
17008 KB |
Output is correct |
6 |
Correct |
10 ms |
17008 KB |
Output is correct |
7 |
Correct |
9 ms |
16972 KB |
Output is correct |
8 |
Correct |
9 ms |
16972 KB |
Output is correct |
9 |
Correct |
9 ms |
16972 KB |
Output is correct |
10 |
Correct |
9 ms |
16952 KB |
Output is correct |
11 |
Correct |
9 ms |
16972 KB |
Output is correct |
12 |
Correct |
9 ms |
16948 KB |
Output is correct |
13 |
Correct |
9 ms |
16996 KB |
Output is correct |
14 |
Correct |
9 ms |
17016 KB |
Output is correct |
15 |
Correct |
9 ms |
16972 KB |
Output is correct |
16 |
Correct |
9 ms |
16972 KB |
Output is correct |
17 |
Correct |
10 ms |
16920 KB |
Output is correct |
18 |
Correct |
9 ms |
16972 KB |
Output is correct |
19 |
Correct |
10 ms |
17048 KB |
Output is correct |
20 |
Correct |
9 ms |
16952 KB |
Output is correct |
21 |
Correct |
9 ms |
16972 KB |
Output is correct |
22 |
Correct |
10 ms |
17004 KB |
Output is correct |
23 |
Correct |
12 ms |
17728 KB |
Output is correct |
24 |
Correct |
12 ms |
17740 KB |
Output is correct |
25 |
Correct |
12 ms |
17740 KB |
Output is correct |
26 |
Correct |
12 ms |
17724 KB |
Output is correct |
27 |
Correct |
12 ms |
17740 KB |
Output is correct |
28 |
Correct |
12 ms |
17740 KB |
Output is correct |
29 |
Correct |
12 ms |
17756 KB |
Output is correct |
30 |
Correct |
11 ms |
17728 KB |
Output is correct |
31 |
Correct |
12 ms |
17740 KB |
Output is correct |
32 |
Correct |
13 ms |
17984 KB |
Output is correct |
33 |
Correct |
13 ms |
17928 KB |
Output is correct |
34 |
Correct |
13 ms |
17896 KB |
Output is correct |
35 |
Correct |
13 ms |
17868 KB |
Output is correct |
36 |
Correct |
12 ms |
17852 KB |
Output is correct |
37 |
Correct |
12 ms |
17816 KB |
Output is correct |
38 |
Correct |
11 ms |
17868 KB |
Output is correct |
39 |
Correct |
12 ms |
17740 KB |
Output is correct |
40 |
Correct |
12 ms |
17844 KB |
Output is correct |
41 |
Correct |
12 ms |
17724 KB |
Output is correct |
42 |
Correct |
12 ms |
17724 KB |
Output is correct |
43 |
Correct |
12 ms |
17832 KB |
Output is correct |
44 |
Correct |
12 ms |
17724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16972 KB |
Output is correct |
2 |
Correct |
9 ms |
16948 KB |
Output is correct |
3 |
Correct |
9 ms |
16952 KB |
Output is correct |
4 |
Correct |
9 ms |
16948 KB |
Output is correct |
5 |
Correct |
9 ms |
17008 KB |
Output is correct |
6 |
Correct |
10 ms |
17008 KB |
Output is correct |
7 |
Correct |
9 ms |
16972 KB |
Output is correct |
8 |
Correct |
9 ms |
16972 KB |
Output is correct |
9 |
Correct |
9 ms |
16972 KB |
Output is correct |
10 |
Correct |
9 ms |
16952 KB |
Output is correct |
11 |
Correct |
9 ms |
16972 KB |
Output is correct |
12 |
Correct |
9 ms |
16948 KB |
Output is correct |
13 |
Correct |
9 ms |
16996 KB |
Output is correct |
14 |
Correct |
9 ms |
17016 KB |
Output is correct |
15 |
Correct |
9 ms |
16972 KB |
Output is correct |
16 |
Correct |
9 ms |
16972 KB |
Output is correct |
17 |
Correct |
10 ms |
16920 KB |
Output is correct |
18 |
Correct |
9 ms |
16972 KB |
Output is correct |
19 |
Correct |
10 ms |
17048 KB |
Output is correct |
20 |
Correct |
9 ms |
16952 KB |
Output is correct |
21 |
Correct |
9 ms |
16972 KB |
Output is correct |
22 |
Correct |
10 ms |
17004 KB |
Output is correct |
23 |
Correct |
12 ms |
17728 KB |
Output is correct |
24 |
Correct |
12 ms |
17740 KB |
Output is correct |
25 |
Correct |
12 ms |
17740 KB |
Output is correct |
26 |
Correct |
12 ms |
17724 KB |
Output is correct |
27 |
Correct |
12 ms |
17740 KB |
Output is correct |
28 |
Correct |
12 ms |
17740 KB |
Output is correct |
29 |
Correct |
12 ms |
17756 KB |
Output is correct |
30 |
Correct |
11 ms |
17728 KB |
Output is correct |
31 |
Correct |
12 ms |
17740 KB |
Output is correct |
32 |
Correct |
13 ms |
17984 KB |
Output is correct |
33 |
Correct |
13 ms |
17928 KB |
Output is correct |
34 |
Correct |
13 ms |
17896 KB |
Output is correct |
35 |
Correct |
13 ms |
17868 KB |
Output is correct |
36 |
Correct |
12 ms |
17852 KB |
Output is correct |
37 |
Correct |
12 ms |
17816 KB |
Output is correct |
38 |
Correct |
11 ms |
17868 KB |
Output is correct |
39 |
Correct |
12 ms |
17740 KB |
Output is correct |
40 |
Correct |
12 ms |
17844 KB |
Output is correct |
41 |
Correct |
12 ms |
17724 KB |
Output is correct |
42 |
Correct |
12 ms |
17724 KB |
Output is correct |
43 |
Correct |
12 ms |
17832 KB |
Output is correct |
44 |
Correct |
12 ms |
17724 KB |
Output is correct |
45 |
Correct |
465 ms |
111808 KB |
Output is correct |
46 |
Correct |
432 ms |
108528 KB |
Output is correct |
47 |
Correct |
484 ms |
107232 KB |
Output is correct |
48 |
Correct |
438 ms |
111856 KB |
Output is correct |
49 |
Correct |
462 ms |
106720 KB |
Output is correct |
50 |
Correct |
450 ms |
106988 KB |
Output is correct |
51 |
Correct |
443 ms |
107116 KB |
Output is correct |
52 |
Correct |
465 ms |
111116 KB |
Output is correct |
53 |
Correct |
449 ms |
111192 KB |
Output is correct |
54 |
Correct |
1102 ms |
132112 KB |
Output is correct |
55 |
Correct |
995 ms |
118572 KB |
Output is correct |
56 |
Correct |
942 ms |
116860 KB |
Output is correct |
57 |
Correct |
1004 ms |
112104 KB |
Output is correct |
58 |
Correct |
797 ms |
107916 KB |
Output is correct |
59 |
Correct |
819 ms |
107420 KB |
Output is correct |
60 |
Correct |
330 ms |
123296 KB |
Output is correct |
61 |
Correct |
528 ms |
106956 KB |
Output is correct |
62 |
Correct |
1081 ms |
122500 KB |
Output is correct |
63 |
Correct |
537 ms |
105940 KB |
Output is correct |
64 |
Correct |
490 ms |
104040 KB |
Output is correct |
65 |
Correct |
1059 ms |
123544 KB |
Output is correct |
66 |
Correct |
542 ms |
104940 KB |
Output is correct |