/* https://oj.uz/submission/286391 */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 10;
const int L = 20;
int n, m, a[N], x[N], y[N], c[N];
int logs[N], dfn[N], dfo[N], T, ls[N], rs[N];
pair<int, int> mx[N][L];
vector<int> ids[N];
ll dp[N];
pair<int, int> qmax(int l, int r) {
int k = logs[r - l + 1];
return max(mx[l][k], mx[r - (1 << k) + 1][k]);
}
void buildSparse() {
for (int i = 2; i <= n; i++) {
logs[i] = logs[i >> 1] + 1;
}
for (int i = 1; i <= n; i++) {
mx[i][0] = make_pair(a[i], i);
}
for (int j = 1; j < L; j++) {
for (int i = 1; i + (1 << j) <= n + 1; i++) {
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
}
}
}
void dfs(int &v, int tl, int tr) {
if (tl + 1 >= tr) {
v = 0;
return;
}
v = qmax(tl + 1, tr - 1).second;
dfs(ls[v], tl, v);
dfs(rs[v], v, tr);
}
const int M = 8 * N;
ll sum[M];
void modify(int v, int tl, int tr, int ql, int qr, ll x) {
if (tl > tr || tl > qr || tr < ql) {
return;
}
if (ql <= tl && tr <= qr) {
sum[v] += x;
return;
}
int mid = tl + tr >> 1;
modify(v * 2, tl, mid, ql, qr, x);
modify(v * 2 + 1, mid + 1, tr, ql, qr, x);
}
ll query(int v, int tl, int tr, int qi) {
if (tl == tr) {
return sum[v];
}
int mid = tl + tr >> 1;
if (qi <= mid) {
return query(v * 2, tl, mid, qi) + sum[v];
} else {
return query(v * 2 + 1, mid + 1, tr, qi) + sum[v];
}
}
void solve(int v) {
if (!v) {
return;
}
dfn[v] = ++T;
solve(ls[v]);
solve(rs[v]);
dfo[v] = T;
dp[v] = dp[ls[v]] + dp[rs[v]];
for (int i : ids[v]) {
dp[v] = max(dp[v], dp[ls[v]] + dp[rs[v]] + c[i] + query(1, 1, n, dfn[x[i]]));
}
modify(1, 1, n, dfn[v], dfo[v], dp[ls[v]] + dp[rs[v]] - dp[v]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
buildSparse();
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x[i], &y[i], &c[i]);
int low = 1, high = x[i], from = 0, to = n + 1;
while (low <= high) {
int mid = low + high >> 1;
if (qmax(mid, x[i]).first >= y[i]) {
from = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
low = x[i], high = n;
while (low <= high) {
int mid = low + high >> 1;
if (qmax(x[i], mid).first >= y[i]) {
to = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
ids[qmax(from + 1, to - 1).second].push_back(i);
}
int root;
dfs(root, 0, n + 1);
solve(root);
ll sum = 0;
for (int i = 1; i <= m; i++) {
sum += c[i];
}
printf("%lld\n", sum - dp[root]);
return 0;
}
Compilation message
constellation3.cpp: In function 'void modify(int, int, int, int, int, long long int)':
constellation3.cpp:48:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
48 | int mid = tl + tr >> 1;
| ~~~^~~~
constellation3.cpp: In function 'long long int query(int, int, int, int)':
constellation3.cpp:56:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = tl + tr >> 1;
| ~~~^~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:88:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
88 | int mid = low + high >> 1;
| ~~~~^~~~~~
constellation3.cpp:98:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
98 | int mid = low + high >> 1;
| ~~~~^~~~~~
constellation3.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
constellation3.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
constellation3.cpp:83:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d", &m);
| ~~~~~^~~~~~~~~~
constellation3.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | scanf("%d%d%d", &x[i], &y[i], &c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5152 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5156 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5112 KB |
Output is correct |
21 |
Correct |
2 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5152 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5156 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5112 KB |
Output is correct |
21 |
Correct |
2 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5460 KB |
Output is correct |
24 |
Correct |
4 ms |
5460 KB |
Output is correct |
25 |
Correct |
5 ms |
5588 KB |
Output is correct |
26 |
Correct |
4 ms |
5460 KB |
Output is correct |
27 |
Correct |
4 ms |
5540 KB |
Output is correct |
28 |
Correct |
4 ms |
5460 KB |
Output is correct |
29 |
Correct |
4 ms |
5460 KB |
Output is correct |
30 |
Correct |
4 ms |
5460 KB |
Output is correct |
31 |
Correct |
4 ms |
5460 KB |
Output is correct |
32 |
Correct |
5 ms |
5588 KB |
Output is correct |
33 |
Correct |
4 ms |
5588 KB |
Output is correct |
34 |
Correct |
4 ms |
5588 KB |
Output is correct |
35 |
Correct |
5 ms |
5588 KB |
Output is correct |
36 |
Correct |
4 ms |
5544 KB |
Output is correct |
37 |
Correct |
4 ms |
5588 KB |
Output is correct |
38 |
Correct |
4 ms |
5716 KB |
Output is correct |
39 |
Correct |
5 ms |
5460 KB |
Output is correct |
40 |
Correct |
4 ms |
5588 KB |
Output is correct |
41 |
Correct |
4 ms |
5544 KB |
Output is correct |
42 |
Correct |
4 ms |
5460 KB |
Output is correct |
43 |
Correct |
4 ms |
5588 KB |
Output is correct |
44 |
Correct |
4 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5152 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5156 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5112 KB |
Output is correct |
21 |
Correct |
2 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5460 KB |
Output is correct |
24 |
Correct |
4 ms |
5460 KB |
Output is correct |
25 |
Correct |
5 ms |
5588 KB |
Output is correct |
26 |
Correct |
4 ms |
5460 KB |
Output is correct |
27 |
Correct |
4 ms |
5540 KB |
Output is correct |
28 |
Correct |
4 ms |
5460 KB |
Output is correct |
29 |
Correct |
4 ms |
5460 KB |
Output is correct |
30 |
Correct |
4 ms |
5460 KB |
Output is correct |
31 |
Correct |
4 ms |
5460 KB |
Output is correct |
32 |
Correct |
5 ms |
5588 KB |
Output is correct |
33 |
Correct |
4 ms |
5588 KB |
Output is correct |
34 |
Correct |
4 ms |
5588 KB |
Output is correct |
35 |
Correct |
5 ms |
5588 KB |
Output is correct |
36 |
Correct |
4 ms |
5544 KB |
Output is correct |
37 |
Correct |
4 ms |
5588 KB |
Output is correct |
38 |
Correct |
4 ms |
5716 KB |
Output is correct |
39 |
Correct |
5 ms |
5460 KB |
Output is correct |
40 |
Correct |
4 ms |
5588 KB |
Output is correct |
41 |
Correct |
4 ms |
5544 KB |
Output is correct |
42 |
Correct |
4 ms |
5460 KB |
Output is correct |
43 |
Correct |
4 ms |
5588 KB |
Output is correct |
44 |
Correct |
4 ms |
5460 KB |
Output is correct |
45 |
Correct |
447 ms |
57212 KB |
Output is correct |
46 |
Correct |
441 ms |
56808 KB |
Output is correct |
47 |
Correct |
459 ms |
56328 KB |
Output is correct |
48 |
Correct |
435 ms |
57212 KB |
Output is correct |
49 |
Correct |
452 ms |
55872 KB |
Output is correct |
50 |
Correct |
440 ms |
55904 KB |
Output is correct |
51 |
Correct |
436 ms |
55956 KB |
Output is correct |
52 |
Correct |
459 ms |
57304 KB |
Output is correct |
53 |
Correct |
442 ms |
56908 KB |
Output is correct |
54 |
Correct |
529 ms |
67380 KB |
Output is correct |
55 |
Correct |
511 ms |
63380 KB |
Output is correct |
56 |
Correct |
516 ms |
61128 KB |
Output is correct |
57 |
Correct |
542 ms |
59724 KB |
Output is correct |
58 |
Correct |
471 ms |
61336 KB |
Output is correct |
59 |
Correct |
464 ms |
61348 KB |
Output is correct |
60 |
Correct |
183 ms |
72268 KB |
Output is correct |
61 |
Correct |
523 ms |
55892 KB |
Output is correct |
62 |
Correct |
578 ms |
67384 KB |
Output is correct |
63 |
Correct |
442 ms |
55112 KB |
Output is correct |
64 |
Correct |
452 ms |
54332 KB |
Output is correct |
65 |
Correct |
512 ms |
67772 KB |
Output is correct |
66 |
Correct |
463 ms |
54624 KB |
Output is correct |