#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
#define g4(a) get<4>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
using iiiii = tuple<int, int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, M, idx, tot, A[200005], X[200005], Y[200005], C[200005], min_l[200005], min_r[200005], lab[200005], L[200005], R[200005], H[200005], ft1[200005], ft2[200005], a[200005], b[200005], sz[200005], anc[25][200005];
stack<int> S;
vector<int> adj[200005], vec[200005];
void construct(int l, int r, int par = -1) {
if (l > r) return;
int cur = ++idx;
if (par != -1) {
adj[par].pb(cur);
}
int mid = -1;
for (int i = l, j = r; i <= j; i++, j--) {
if (min_l[i] < l && min_r[i] > r) {
mid = i;
break;
}
if (min_l[j] < l && min_r[j] > r) {
mid = j;
break;
}
}
assert(mid != -1);
L[cur] = l;
R[cur] = r;
H[cur] = A[mid] + 1;
lab[mid] = cur;
construct(l, mid - 1, cur);
construct(mid + 1, r, cur);
}
int dfs(int n) {
for (int k = 1; k <= 20; k++) {
if (anc[k - 1][n] == -1) break;
anc[k][n] = anc[k - 1][anc[k - 1][n]];
}
for (auto u : adj[n]) {
anc[0][u] = n;
sz[n] += dfs(u);
}
return ++sz[n];
}
inline int ls(int x) { return x & -x; }
void upd1(int l, int r, int v) {
for (; l <= N; l += ls(l)) ft1[l] += v;
for (r++; r <= N; r += ls(r)) ft1[r] -= v;
}
void upd2(int l, int r, int v) {
for (; l <= N; l += ls(l)) ft2[l] += v;
for (r++; r <= N; r += ls(r)) ft2[r] -= v;
}
int qry1(int p) {
int r = 0;
for (; p; p -= ls(p)) r += ft1[p];
return r;
}
int qry2(int p) {
int r = 0;
for (; p; p -= ls(p)) r += ft2[p];
return r;
}
int dp(int n, int e = -1) {
int ret = 0;
for (auto u : adj[n]) ret += dp(u, n);
upd1(n, n + sz[n] - 1, ret);
for (auto i : vec[n])
ret = max(ret, qry1(a[i]) + qry1(b[i]) - qry1(n) - (qry2(a[i]) + qry2(b[i]) - qry2(n)) + C[i]);
upd2(n, n + sz[n] - 1, ret);
return ret;
}
main() {
memset(anc, -1, sizeof anc);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i];
}
cin >> M;
for (int i = 1; i <= M; i++) {
cin >> X[i] >> Y[i] >> C[i];
tot += C[i];
}
for (int i = 1; i <= N; i++) {
while (!S.empty() && A[S.top()] <= A[i]) S.pop();
min_l[i] = (S.empty() ? 0 : S.top());
S.push(i);
}
while (!S.empty()) S.pop();
for (int i = N; i >= 1; i--) {
while (!S.empty() && A[S.top()] <= A[i]) S.pop();
min_r[i] = (S.empty() ? N + 1 : S.top());
S.push(i);
}
construct(1, N);
dfs(1);
for (int i = 1; i <= M; i++) {
assert(H[lab[X[i]]] <= Y[i]);
int cur = lab[X[i]];
for (int k = 20; k >= 0; k--) {
if (anc[k][cur] != -1 && H[anc[k][cur]] <= Y[i]) {
cur = anc[k][cur];
}
}
a[i] = lab[X[i]];
b[i] = cur;
vec[b[i]].pb(i);
}
cout << tot - dp(1) << '\n';
}
Compilation message
constellation3.cpp:104:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
104 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
48980 KB |
Output is correct |
2 |
Correct |
22 ms |
48940 KB |
Output is correct |
3 |
Correct |
22 ms |
49004 KB |
Output is correct |
4 |
Correct |
21 ms |
48976 KB |
Output is correct |
5 |
Correct |
20 ms |
48956 KB |
Output is correct |
6 |
Correct |
21 ms |
48992 KB |
Output is correct |
7 |
Correct |
21 ms |
48980 KB |
Output is correct |
8 |
Correct |
20 ms |
48980 KB |
Output is correct |
9 |
Correct |
23 ms |
48972 KB |
Output is correct |
10 |
Correct |
20 ms |
49012 KB |
Output is correct |
11 |
Correct |
20 ms |
49016 KB |
Output is correct |
12 |
Correct |
21 ms |
49000 KB |
Output is correct |
13 |
Correct |
22 ms |
48904 KB |
Output is correct |
14 |
Correct |
20 ms |
48932 KB |
Output is correct |
15 |
Correct |
20 ms |
49028 KB |
Output is correct |
16 |
Correct |
21 ms |
49004 KB |
Output is correct |
17 |
Correct |
20 ms |
48984 KB |
Output is correct |
18 |
Correct |
21 ms |
49024 KB |
Output is correct |
19 |
Correct |
19 ms |
48980 KB |
Output is correct |
20 |
Correct |
20 ms |
48972 KB |
Output is correct |
21 |
Correct |
21 ms |
48928 KB |
Output is correct |
22 |
Correct |
20 ms |
49000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
48980 KB |
Output is correct |
2 |
Correct |
22 ms |
48940 KB |
Output is correct |
3 |
Correct |
22 ms |
49004 KB |
Output is correct |
4 |
Correct |
21 ms |
48976 KB |
Output is correct |
5 |
Correct |
20 ms |
48956 KB |
Output is correct |
6 |
Correct |
21 ms |
48992 KB |
Output is correct |
7 |
Correct |
21 ms |
48980 KB |
Output is correct |
8 |
Correct |
20 ms |
48980 KB |
Output is correct |
9 |
Correct |
23 ms |
48972 KB |
Output is correct |
10 |
Correct |
20 ms |
49012 KB |
Output is correct |
11 |
Correct |
20 ms |
49016 KB |
Output is correct |
12 |
Correct |
21 ms |
49000 KB |
Output is correct |
13 |
Correct |
22 ms |
48904 KB |
Output is correct |
14 |
Correct |
20 ms |
48932 KB |
Output is correct |
15 |
Correct |
20 ms |
49028 KB |
Output is correct |
16 |
Correct |
21 ms |
49004 KB |
Output is correct |
17 |
Correct |
20 ms |
48984 KB |
Output is correct |
18 |
Correct |
21 ms |
49024 KB |
Output is correct |
19 |
Correct |
19 ms |
48980 KB |
Output is correct |
20 |
Correct |
20 ms |
48972 KB |
Output is correct |
21 |
Correct |
21 ms |
48928 KB |
Output is correct |
22 |
Correct |
20 ms |
49000 KB |
Output is correct |
23 |
Correct |
21 ms |
49164 KB |
Output is correct |
24 |
Correct |
21 ms |
49236 KB |
Output is correct |
25 |
Correct |
22 ms |
49228 KB |
Output is correct |
26 |
Correct |
21 ms |
49280 KB |
Output is correct |
27 |
Correct |
22 ms |
49268 KB |
Output is correct |
28 |
Correct |
22 ms |
49256 KB |
Output is correct |
29 |
Correct |
22 ms |
49272 KB |
Output is correct |
30 |
Correct |
21 ms |
49236 KB |
Output is correct |
31 |
Correct |
23 ms |
49320 KB |
Output is correct |
32 |
Correct |
22 ms |
49396 KB |
Output is correct |
33 |
Correct |
25 ms |
49304 KB |
Output is correct |
34 |
Correct |
21 ms |
49320 KB |
Output is correct |
35 |
Correct |
22 ms |
49268 KB |
Output is correct |
36 |
Correct |
24 ms |
49452 KB |
Output is correct |
37 |
Correct |
23 ms |
49456 KB |
Output is correct |
38 |
Correct |
22 ms |
49492 KB |
Output is correct |
39 |
Correct |
22 ms |
49264 KB |
Output is correct |
40 |
Correct |
23 ms |
49364 KB |
Output is correct |
41 |
Correct |
22 ms |
49236 KB |
Output is correct |
42 |
Correct |
24 ms |
49296 KB |
Output is correct |
43 |
Correct |
23 ms |
49404 KB |
Output is correct |
44 |
Correct |
22 ms |
49236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
48980 KB |
Output is correct |
2 |
Correct |
22 ms |
48940 KB |
Output is correct |
3 |
Correct |
22 ms |
49004 KB |
Output is correct |
4 |
Correct |
21 ms |
48976 KB |
Output is correct |
5 |
Correct |
20 ms |
48956 KB |
Output is correct |
6 |
Correct |
21 ms |
48992 KB |
Output is correct |
7 |
Correct |
21 ms |
48980 KB |
Output is correct |
8 |
Correct |
20 ms |
48980 KB |
Output is correct |
9 |
Correct |
23 ms |
48972 KB |
Output is correct |
10 |
Correct |
20 ms |
49012 KB |
Output is correct |
11 |
Correct |
20 ms |
49016 KB |
Output is correct |
12 |
Correct |
21 ms |
49000 KB |
Output is correct |
13 |
Correct |
22 ms |
48904 KB |
Output is correct |
14 |
Correct |
20 ms |
48932 KB |
Output is correct |
15 |
Correct |
20 ms |
49028 KB |
Output is correct |
16 |
Correct |
21 ms |
49004 KB |
Output is correct |
17 |
Correct |
20 ms |
48984 KB |
Output is correct |
18 |
Correct |
21 ms |
49024 KB |
Output is correct |
19 |
Correct |
19 ms |
48980 KB |
Output is correct |
20 |
Correct |
20 ms |
48972 KB |
Output is correct |
21 |
Correct |
21 ms |
48928 KB |
Output is correct |
22 |
Correct |
20 ms |
49000 KB |
Output is correct |
23 |
Correct |
21 ms |
49164 KB |
Output is correct |
24 |
Correct |
21 ms |
49236 KB |
Output is correct |
25 |
Correct |
22 ms |
49228 KB |
Output is correct |
26 |
Correct |
21 ms |
49280 KB |
Output is correct |
27 |
Correct |
22 ms |
49268 KB |
Output is correct |
28 |
Correct |
22 ms |
49256 KB |
Output is correct |
29 |
Correct |
22 ms |
49272 KB |
Output is correct |
30 |
Correct |
21 ms |
49236 KB |
Output is correct |
31 |
Correct |
23 ms |
49320 KB |
Output is correct |
32 |
Correct |
22 ms |
49396 KB |
Output is correct |
33 |
Correct |
25 ms |
49304 KB |
Output is correct |
34 |
Correct |
21 ms |
49320 KB |
Output is correct |
35 |
Correct |
22 ms |
49268 KB |
Output is correct |
36 |
Correct |
24 ms |
49452 KB |
Output is correct |
37 |
Correct |
23 ms |
49456 KB |
Output is correct |
38 |
Correct |
22 ms |
49492 KB |
Output is correct |
39 |
Correct |
22 ms |
49264 KB |
Output is correct |
40 |
Correct |
23 ms |
49364 KB |
Output is correct |
41 |
Correct |
22 ms |
49236 KB |
Output is correct |
42 |
Correct |
24 ms |
49296 KB |
Output is correct |
43 |
Correct |
23 ms |
49404 KB |
Output is correct |
44 |
Correct |
22 ms |
49236 KB |
Output is correct |
45 |
Correct |
358 ms |
85520 KB |
Output is correct |
46 |
Correct |
400 ms |
84960 KB |
Output is correct |
47 |
Correct |
416 ms |
85052 KB |
Output is correct |
48 |
Correct |
397 ms |
85072 KB |
Output is correct |
49 |
Correct |
375 ms |
84564 KB |
Output is correct |
50 |
Correct |
356 ms |
84388 KB |
Output is correct |
51 |
Correct |
345 ms |
84664 KB |
Output is correct |
52 |
Correct |
378 ms |
85480 KB |
Output is correct |
53 |
Correct |
395 ms |
84932 KB |
Output is correct |
54 |
Correct |
603 ms |
95784 KB |
Output is correct |
55 |
Correct |
588 ms |
92252 KB |
Output is correct |
56 |
Correct |
577 ms |
90156 KB |
Output is correct |
57 |
Correct |
554 ms |
89132 KB |
Output is correct |
58 |
Correct |
493 ms |
99816 KB |
Output is correct |
59 |
Correct |
473 ms |
99716 KB |
Output is correct |
60 |
Correct |
206 ms |
103500 KB |
Output is correct |
61 |
Correct |
313 ms |
84576 KB |
Output is correct |
62 |
Correct |
710 ms |
98340 KB |
Output is correct |
63 |
Correct |
334 ms |
84296 KB |
Output is correct |
64 |
Correct |
315 ms |
83520 KB |
Output is correct |
65 |
Correct |
576 ms |
98452 KB |
Output is correct |
66 |
Correct |
313 ms |
84288 KB |
Output is correct |