#include "bits/stdc++.h"
using namespace std;
using pii = pair <int, int>;
int a[200010];
map <pii, vector <pii>> cont;
vector <int> g[200010];
vector <pii> star[200010];
vector <int> v[200010];
vector <pii> node;
int disc[200010], fin[200010];
int leaf[200010];
long long dp[200010];
int cur;
void dfs(int x) {
disc[x] = ++cur;
for(auto i : g[x]) {
dfs(i);
}
fin[x] = cur;
}
long long t[200010];
void update(int x, long long val) {
for(int i = x; i <= cur; i += i & (-i)) {
t[i] += val;
}
}
long long query(int x) {
long long ans = 0;
for(int i = x; i > 0; i -= i & (-i)) {
ans += t[i];
}
return ans;
}
void solve(int i) {
dp[i] = 0;
for(auto j : g[i]) {
solve(j);
dp[i] += dp[j];
}
for(auto j : g[i]) {
update(disc[i], dp[j]); update(fin[i] + 1, -dp[j]);
update(disc[j], -dp[j]); update(fin[j] + 1, dp[j]);
}
for(auto j : star[i]) {
int pos = disc[leaf[j.first]];
dp[i] = max(dp[i], query(pos) + j.second);
}
}
int prop[200010 * 4];
void assign(int x, int y, int val, int c, int b, int e) {
if(x <= b && e <= y) {
prop[c] = min(prop[c], val);
return ;
}
if(y < b || e < x) return ;
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
assign(x, y, val, l, b, m);
assign(x, y, val, r, m + 1, e);
}
int get(int x, int c, int b, int e) {
if(b == e) return prop[c];
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
if(x <= m) return min(prop[c], get(x, l, b, m));
else return min(prop[c], get(x, r, m + 1, e));
}
int x[200010], y[200010], c[200010];
int l[200010], r[200010];
vector <pii> mono;
int search(int l, int r, int val) {
if(l == r) {
return mono[l].second;
}
int m = (l + r + 1) >> 1;
if(mono[m].first >= val) return search(m, r, val);
else return search(l, m - 1, val);
}
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int m;
scanf("%d", &m);
long long total = 0;
for(int i = 1; i <= m; i++) {
scanf("%d %d %d", &x[i], &y[i], &c[i]);
total += c[i];
v[x[i]].emplace_back(i);
}
mono.emplace_back(INT_MAX, 0);
for(int i = 1; i <= n; i++) {
while(mono.back().first <= a[i]) {
mono.pop_back();
}
mono.emplace_back(a[i], i);
for(auto j : v[i]) {
l[j] = search(0, mono.size() - 1, y[j]) + 1;
}
}
mono.resize(1);
mono[0].second = n + 1;
for(int i = n; i >= 1; i--) {
while(mono.back().first <= a[i]) {
mono.pop_back();
}
mono.emplace_back(a[i], i);
for(auto j : v[i]) {
r[j] = search(0, mono.size() - 1, y[j]) - 1;
}
}
for(int i = 1; i <= m; i++) {
cont[pii(l[i], r[i])].push_back(pii(x[i], c[i]));
}
for(auto i : cont) {
node.push_back(i.first);
}
sort(node.begin(), node.end(), [] (pii p, pii q) { return (p.second - p.first) < (q.second - q.first); });
set <pii> active;
memset(leaf, -1, sizeof leaf);
memset(prop, 63, sizeof prop);
for(int id = 0; id < node.size(); id++) {
while(true) {
auto it = active.lower_bound(pii(node[id].first, -1));
if(it != active.end() && it -> first <= node[id].second) {
g[id].push_back(it -> second);
active.erase(it);
} else break;
}
active.insert(pii(node[id].first, id));
star[id] = cont[node[id]];
assign(node[id].first, node[id].second, id, 1, 1, n);
}
int root = node.size();
node.emplace_back(1, n);
for(auto i : active) {
g[root].push_back(i.second);
}
assign(1, n, root, 1, 1, n);
for(int i = 1; i <= n; i++) {
leaf[i] = get(i, 1, 1, n);
}
dfs(root);
solve(root);
printf("%lld\n", total - dp[root]);
return 0;
}
Compilation message
constellation3.cpp: In function 'int main(int, const char**)':
constellation3.cpp:132:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int id = 0; id < node.size(); id++) {
~~~^~~~~~~~~~~~~
constellation3.cpp:89:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
constellation3.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
constellation3.cpp:94:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &m);
~~~~~^~~~~~~~~~
constellation3.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &x[i], &y[i], &c[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
18432 KB |
Output is correct |
2 |
Correct |
11 ms |
18432 KB |
Output is correct |
3 |
Correct |
15 ms |
18432 KB |
Output is correct |
4 |
Correct |
11 ms |
18432 KB |
Output is correct |
5 |
Correct |
15 ms |
18432 KB |
Output is correct |
6 |
Correct |
12 ms |
18412 KB |
Output is correct |
7 |
Correct |
11 ms |
18432 KB |
Output is correct |
8 |
Correct |
11 ms |
18432 KB |
Output is correct |
9 |
Correct |
12 ms |
18432 KB |
Output is correct |
10 |
Correct |
11 ms |
18432 KB |
Output is correct |
11 |
Correct |
11 ms |
18432 KB |
Output is correct |
12 |
Correct |
11 ms |
18432 KB |
Output is correct |
13 |
Correct |
11 ms |
18432 KB |
Output is correct |
14 |
Correct |
11 ms |
18432 KB |
Output is correct |
15 |
Correct |
11 ms |
18432 KB |
Output is correct |
16 |
Correct |
11 ms |
18432 KB |
Output is correct |
17 |
Correct |
11 ms |
18432 KB |
Output is correct |
18 |
Correct |
11 ms |
18432 KB |
Output is correct |
19 |
Correct |
11 ms |
18432 KB |
Output is correct |
20 |
Correct |
13 ms |
18432 KB |
Output is correct |
21 |
Correct |
12 ms |
18432 KB |
Output is correct |
22 |
Correct |
11 ms |
18432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
18432 KB |
Output is correct |
2 |
Correct |
11 ms |
18432 KB |
Output is correct |
3 |
Correct |
15 ms |
18432 KB |
Output is correct |
4 |
Correct |
11 ms |
18432 KB |
Output is correct |
5 |
Correct |
15 ms |
18432 KB |
Output is correct |
6 |
Correct |
12 ms |
18412 KB |
Output is correct |
7 |
Correct |
11 ms |
18432 KB |
Output is correct |
8 |
Correct |
11 ms |
18432 KB |
Output is correct |
9 |
Correct |
12 ms |
18432 KB |
Output is correct |
10 |
Correct |
11 ms |
18432 KB |
Output is correct |
11 |
Correct |
11 ms |
18432 KB |
Output is correct |
12 |
Correct |
11 ms |
18432 KB |
Output is correct |
13 |
Correct |
11 ms |
18432 KB |
Output is correct |
14 |
Correct |
11 ms |
18432 KB |
Output is correct |
15 |
Correct |
11 ms |
18432 KB |
Output is correct |
16 |
Correct |
11 ms |
18432 KB |
Output is correct |
17 |
Correct |
11 ms |
18432 KB |
Output is correct |
18 |
Correct |
11 ms |
18432 KB |
Output is correct |
19 |
Correct |
11 ms |
18432 KB |
Output is correct |
20 |
Correct |
13 ms |
18432 KB |
Output is correct |
21 |
Correct |
12 ms |
18432 KB |
Output is correct |
22 |
Correct |
11 ms |
18432 KB |
Output is correct |
23 |
Correct |
13 ms |
18688 KB |
Output is correct |
24 |
Correct |
14 ms |
18688 KB |
Output is correct |
25 |
Correct |
18 ms |
18688 KB |
Output is correct |
26 |
Correct |
15 ms |
18688 KB |
Output is correct |
27 |
Correct |
18 ms |
18688 KB |
Output is correct |
28 |
Correct |
14 ms |
18688 KB |
Output is correct |
29 |
Correct |
15 ms |
18688 KB |
Output is correct |
30 |
Correct |
14 ms |
18688 KB |
Output is correct |
31 |
Correct |
16 ms |
18708 KB |
Output is correct |
32 |
Correct |
14 ms |
18688 KB |
Output is correct |
33 |
Correct |
13 ms |
18688 KB |
Output is correct |
34 |
Correct |
18 ms |
18816 KB |
Output is correct |
35 |
Correct |
14 ms |
18688 KB |
Output is correct |
36 |
Correct |
14 ms |
18560 KB |
Output is correct |
37 |
Correct |
13 ms |
18612 KB |
Output is correct |
38 |
Correct |
13 ms |
18816 KB |
Output is correct |
39 |
Correct |
14 ms |
18560 KB |
Output is correct |
40 |
Correct |
13 ms |
18688 KB |
Output is correct |
41 |
Correct |
13 ms |
18560 KB |
Output is correct |
42 |
Correct |
13 ms |
18560 KB |
Output is correct |
43 |
Correct |
15 ms |
18688 KB |
Output is correct |
44 |
Correct |
13 ms |
18560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
18432 KB |
Output is correct |
2 |
Correct |
11 ms |
18432 KB |
Output is correct |
3 |
Correct |
15 ms |
18432 KB |
Output is correct |
4 |
Correct |
11 ms |
18432 KB |
Output is correct |
5 |
Correct |
15 ms |
18432 KB |
Output is correct |
6 |
Correct |
12 ms |
18412 KB |
Output is correct |
7 |
Correct |
11 ms |
18432 KB |
Output is correct |
8 |
Correct |
11 ms |
18432 KB |
Output is correct |
9 |
Correct |
12 ms |
18432 KB |
Output is correct |
10 |
Correct |
11 ms |
18432 KB |
Output is correct |
11 |
Correct |
11 ms |
18432 KB |
Output is correct |
12 |
Correct |
11 ms |
18432 KB |
Output is correct |
13 |
Correct |
11 ms |
18432 KB |
Output is correct |
14 |
Correct |
11 ms |
18432 KB |
Output is correct |
15 |
Correct |
11 ms |
18432 KB |
Output is correct |
16 |
Correct |
11 ms |
18432 KB |
Output is correct |
17 |
Correct |
11 ms |
18432 KB |
Output is correct |
18 |
Correct |
11 ms |
18432 KB |
Output is correct |
19 |
Correct |
11 ms |
18432 KB |
Output is correct |
20 |
Correct |
13 ms |
18432 KB |
Output is correct |
21 |
Correct |
12 ms |
18432 KB |
Output is correct |
22 |
Correct |
11 ms |
18432 KB |
Output is correct |
23 |
Correct |
13 ms |
18688 KB |
Output is correct |
24 |
Correct |
14 ms |
18688 KB |
Output is correct |
25 |
Correct |
18 ms |
18688 KB |
Output is correct |
26 |
Correct |
15 ms |
18688 KB |
Output is correct |
27 |
Correct |
18 ms |
18688 KB |
Output is correct |
28 |
Correct |
14 ms |
18688 KB |
Output is correct |
29 |
Correct |
15 ms |
18688 KB |
Output is correct |
30 |
Correct |
14 ms |
18688 KB |
Output is correct |
31 |
Correct |
16 ms |
18708 KB |
Output is correct |
32 |
Correct |
14 ms |
18688 KB |
Output is correct |
33 |
Correct |
13 ms |
18688 KB |
Output is correct |
34 |
Correct |
18 ms |
18816 KB |
Output is correct |
35 |
Correct |
14 ms |
18688 KB |
Output is correct |
36 |
Correct |
14 ms |
18560 KB |
Output is correct |
37 |
Correct |
13 ms |
18612 KB |
Output is correct |
38 |
Correct |
13 ms |
18816 KB |
Output is correct |
39 |
Correct |
14 ms |
18560 KB |
Output is correct |
40 |
Correct |
13 ms |
18688 KB |
Output is correct |
41 |
Correct |
13 ms |
18560 KB |
Output is correct |
42 |
Correct |
13 ms |
18560 KB |
Output is correct |
43 |
Correct |
15 ms |
18688 KB |
Output is correct |
44 |
Correct |
13 ms |
18560 KB |
Output is correct |
45 |
Correct |
506 ms |
51564 KB |
Output is correct |
46 |
Correct |
503 ms |
51048 KB |
Output is correct |
47 |
Correct |
506 ms |
51692 KB |
Output is correct |
48 |
Correct |
485 ms |
50928 KB |
Output is correct |
49 |
Correct |
489 ms |
51052 KB |
Output is correct |
50 |
Correct |
484 ms |
50672 KB |
Output is correct |
51 |
Correct |
482 ms |
51312 KB |
Output is correct |
52 |
Correct |
514 ms |
51856 KB |
Output is correct |
53 |
Correct |
490 ms |
51008 KB |
Output is correct |
54 |
Correct |
492 ms |
54820 KB |
Output is correct |
55 |
Correct |
447 ms |
51312 KB |
Output is correct |
56 |
Correct |
372 ms |
48592 KB |
Output is correct |
57 |
Correct |
353 ms |
47088 KB |
Output is correct |
58 |
Correct |
238 ms |
36016 KB |
Output is correct |
59 |
Correct |
234 ms |
36216 KB |
Output is correct |
60 |
Correct |
254 ms |
58872 KB |
Output is correct |
61 |
Correct |
325 ms |
41708 KB |
Output is correct |
62 |
Correct |
412 ms |
54840 KB |
Output is correct |
63 |
Correct |
310 ms |
41472 KB |
Output is correct |
64 |
Correct |
298 ms |
40944 KB |
Output is correct |
65 |
Correct |
416 ms |
54628 KB |
Output is correct |
66 |
Correct |
287 ms |
40952 KB |
Output is correct |