#include <cstdio>
#include <algorithm>
#include <vector>
typedef long long LL;
const int MN = 200005, MM = 200005, MS = 4000005;
int N, A[MN], Root;
std::vector<int> G[MN];
void BuildCartesianTree() {
static int stk[MN], tp, x;
auto Add = [](int u, int v) {
if (v) G[u].push_back(v);
};
for (int i = 1; i <= N; ++i) {
for (x = 0; tp && A[stk[tp]] < A[i]; --tp)
Add(stk[tp], x), x = stk[tp];
Add(i, x), stk[++tp] = i;
}
for (x = 0; tp; --tp)
Add(stk[tp], x), x = stk[tp];
Root = x;
}
int dep[MN], par[MN][18];
void DFS(int u, int pa) {
dep[u] = dep[par[u][0] = pa] + 1;
for (int j = 0; 2 << j < dep[u]; ++j)
par[u][j + 1] = par[par[u][j]][j];
for (int v : G[u]) DFS(v, u);
}
int M, w[MM];
std::vector<int> V0[MN], V1[MN];
#define li lc[i]
#define ri rc[i]
#define mid ((l + r) >> 1)
#define ls li, l, mid
#define rs ri, mid + 1, r
int lc[MS], rc[MS], cnt;
LL tg[MS];
inline void P(int i, LL x) { if (i) tg[i] += x; }
inline void Pushdown(int i) { if (tg[i]) P(li, tg[i]), P(ri, tg[i]), tg[i] = 0; }
void Mdf(int &i, int l, int r, int p, LL x) {
if (!i) i = ++cnt;
if (l == r) return tg[i] = x, void();
Pushdown(i), p <= mid ? Mdf(ls, p, x) : Mdf(rs, p, x);
}
LL Qur(int i, int l, int r, int p) {
if (l == r) return tg[i];
Pushdown(i);
return p <= mid ? Qur(ls, p) : Qur(rs, p);
}
void Merge(int &i, int l, int r, int i2) {
if (!i2) return ;
if (!i) return i = i2, void();
Pushdown(i), Pushdown(i2);
Merge(ls, lc[i2]), Merge(rs, rc[i2]);
}
int rt[MN]; LL dp[MN], Sum;
void Solve(int u) {
for (int v : G[u]) Solve(v), dp[u] += dp[v];
for (int v : G[u]) P(rt[v], dp[u] - dp[v]), Merge(rt[u], 1, M, rt[v]);
for (int i : V0[u]) Mdf(rt[u], 1, M, i, w[i] + dp[u]);
for (int i : V1[u]) dp[u] = std::max(dp[u], Qur(rt[u], 1, M, i));
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) scanf("%d", &A[i]);
BuildCartesianTree();
DFS(Root, 0);
A[0] = N;
scanf("%d", &M);
for (int i = 1; i <= M; ++i) {
int x, y, z;
scanf("%d%d%d", &x, &y, &w[i]), z = x;
for (int j = 17; j >= 0; --j)
if (y > A[par[z][j]]) z = par[z][j];
V0[x].push_back(i);
V1[z].push_back(i);
Sum += w[i];
}
Solve(Root);
printf("%lld\n", Sum - dp[Root]);
return 0;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
constellation3.cpp:73:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= N; ++i) scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
constellation3.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &M);
~~~~~^~~~~~~~~~
constellation3.cpp:80:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &x, &y, &w[i]), z = x;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14592 KB |
Output is correct |
3 |
Correct |
14 ms |
14592 KB |
Output is correct |
4 |
Correct |
13 ms |
14592 KB |
Output is correct |
5 |
Correct |
13 ms |
14592 KB |
Output is correct |
6 |
Correct |
13 ms |
14592 KB |
Output is correct |
7 |
Correct |
13 ms |
14592 KB |
Output is correct |
8 |
Correct |
13 ms |
14592 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14592 KB |
Output is correct |
11 |
Correct |
13 ms |
14592 KB |
Output is correct |
12 |
Correct |
13 ms |
14592 KB |
Output is correct |
13 |
Correct |
13 ms |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14592 KB |
Output is correct |
16 |
Correct |
13 ms |
14592 KB |
Output is correct |
17 |
Correct |
13 ms |
14592 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14592 KB |
Output is correct |
20 |
Correct |
13 ms |
14592 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
13 ms |
14592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14592 KB |
Output is correct |
3 |
Correct |
14 ms |
14592 KB |
Output is correct |
4 |
Correct |
13 ms |
14592 KB |
Output is correct |
5 |
Correct |
13 ms |
14592 KB |
Output is correct |
6 |
Correct |
13 ms |
14592 KB |
Output is correct |
7 |
Correct |
13 ms |
14592 KB |
Output is correct |
8 |
Correct |
13 ms |
14592 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14592 KB |
Output is correct |
11 |
Correct |
13 ms |
14592 KB |
Output is correct |
12 |
Correct |
13 ms |
14592 KB |
Output is correct |
13 |
Correct |
13 ms |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14592 KB |
Output is correct |
16 |
Correct |
13 ms |
14592 KB |
Output is correct |
17 |
Correct |
13 ms |
14592 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14592 KB |
Output is correct |
20 |
Correct |
13 ms |
14592 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
13 ms |
14592 KB |
Output is correct |
23 |
Correct |
16 ms |
14976 KB |
Output is correct |
24 |
Correct |
16 ms |
15104 KB |
Output is correct |
25 |
Correct |
16 ms |
15104 KB |
Output is correct |
26 |
Correct |
17 ms |
15104 KB |
Output is correct |
27 |
Correct |
16 ms |
15104 KB |
Output is correct |
28 |
Correct |
16 ms |
15104 KB |
Output is correct |
29 |
Correct |
15 ms |
15104 KB |
Output is correct |
30 |
Correct |
16 ms |
15104 KB |
Output is correct |
31 |
Correct |
15 ms |
15104 KB |
Output is correct |
32 |
Correct |
15 ms |
14976 KB |
Output is correct |
33 |
Correct |
16 ms |
15104 KB |
Output is correct |
34 |
Correct |
16 ms |
15104 KB |
Output is correct |
35 |
Correct |
16 ms |
15104 KB |
Output is correct |
36 |
Correct |
15 ms |
14968 KB |
Output is correct |
37 |
Correct |
15 ms |
14976 KB |
Output is correct |
38 |
Correct |
15 ms |
14976 KB |
Output is correct |
39 |
Correct |
16 ms |
15104 KB |
Output is correct |
40 |
Correct |
15 ms |
14976 KB |
Output is correct |
41 |
Correct |
15 ms |
15104 KB |
Output is correct |
42 |
Correct |
16 ms |
15104 KB |
Output is correct |
43 |
Correct |
15 ms |
14976 KB |
Output is correct |
44 |
Correct |
16 ms |
15104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14592 KB |
Output is correct |
3 |
Correct |
14 ms |
14592 KB |
Output is correct |
4 |
Correct |
13 ms |
14592 KB |
Output is correct |
5 |
Correct |
13 ms |
14592 KB |
Output is correct |
6 |
Correct |
13 ms |
14592 KB |
Output is correct |
7 |
Correct |
13 ms |
14592 KB |
Output is correct |
8 |
Correct |
13 ms |
14592 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14592 KB |
Output is correct |
11 |
Correct |
13 ms |
14592 KB |
Output is correct |
12 |
Correct |
13 ms |
14592 KB |
Output is correct |
13 |
Correct |
13 ms |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14592 KB |
Output is correct |
16 |
Correct |
13 ms |
14592 KB |
Output is correct |
17 |
Correct |
13 ms |
14592 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14592 KB |
Output is correct |
20 |
Correct |
13 ms |
14592 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
13 ms |
14592 KB |
Output is correct |
23 |
Correct |
16 ms |
14976 KB |
Output is correct |
24 |
Correct |
16 ms |
15104 KB |
Output is correct |
25 |
Correct |
16 ms |
15104 KB |
Output is correct |
26 |
Correct |
17 ms |
15104 KB |
Output is correct |
27 |
Correct |
16 ms |
15104 KB |
Output is correct |
28 |
Correct |
16 ms |
15104 KB |
Output is correct |
29 |
Correct |
15 ms |
15104 KB |
Output is correct |
30 |
Correct |
16 ms |
15104 KB |
Output is correct |
31 |
Correct |
15 ms |
15104 KB |
Output is correct |
32 |
Correct |
15 ms |
14976 KB |
Output is correct |
33 |
Correct |
16 ms |
15104 KB |
Output is correct |
34 |
Correct |
16 ms |
15104 KB |
Output is correct |
35 |
Correct |
16 ms |
15104 KB |
Output is correct |
36 |
Correct |
15 ms |
14968 KB |
Output is correct |
37 |
Correct |
15 ms |
14976 KB |
Output is correct |
38 |
Correct |
15 ms |
14976 KB |
Output is correct |
39 |
Correct |
16 ms |
15104 KB |
Output is correct |
40 |
Correct |
15 ms |
14976 KB |
Output is correct |
41 |
Correct |
15 ms |
15104 KB |
Output is correct |
42 |
Correct |
16 ms |
15104 KB |
Output is correct |
43 |
Correct |
15 ms |
14976 KB |
Output is correct |
44 |
Correct |
16 ms |
15104 KB |
Output is correct |
45 |
Correct |
549 ms |
98680 KB |
Output is correct |
46 |
Correct |
537 ms |
96888 KB |
Output is correct |
47 |
Correct |
559 ms |
98936 KB |
Output is correct |
48 |
Correct |
534 ms |
97144 KB |
Output is correct |
49 |
Correct |
538 ms |
96888 KB |
Output is correct |
50 |
Correct |
538 ms |
96000 KB |
Output is correct |
51 |
Correct |
547 ms |
97656 KB |
Output is correct |
52 |
Correct |
553 ms |
99064 KB |
Output is correct |
53 |
Correct |
536 ms |
96968 KB |
Output is correct |
54 |
Correct |
702 ms |
80380 KB |
Output is correct |
55 |
Correct |
723 ms |
84856 KB |
Output is correct |
56 |
Correct |
699 ms |
86912 KB |
Output is correct |
57 |
Correct |
706 ms |
88824 KB |
Output is correct |
58 |
Correct |
526 ms |
76904 KB |
Output is correct |
59 |
Correct |
547 ms |
77176 KB |
Output is correct |
60 |
Correct |
223 ms |
68984 KB |
Output is correct |
61 |
Correct |
541 ms |
95612 KB |
Output is correct |
62 |
Correct |
631 ms |
74744 KB |
Output is correct |
63 |
Correct |
542 ms |
96584 KB |
Output is correct |
64 |
Correct |
518 ms |
93760 KB |
Output is correct |
65 |
Correct |
618 ms |
74488 KB |
Output is correct |
66 |
Correct |
532 ms |
96760 KB |
Output is correct |