#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define endl "\n"
const int N = 2e5 + 5, mod = 1e9 + 7; //!
int t, le_[200 * N], a[N], ri_[200 * N], cn[200 * N], cur, root[N], f[N], p[N], n;
vector<pii> comp[N];
ll tree[200 * N], lazy[200 * N];
void push(int u, int l,int r) {
if(!lazy[u]) return;
tree[u] += lazy[u];
if(l != r) {
if(le_[u]) lazy[le_[u]] += lazy[u];
if(ri_[u]) lazy[ri_[u]] += lazy[u];
}
lazy[u] = 0;
}
void upd(int u, int st, int en, int l,int r, ll v) {
if(!cn[u]) return;
push(u, l, r);
if(l > en || r < st) return;
if(st <= l && r <= en) {
lazy[u] = v;
push(u, l, r);
return;
}
int mid = (l + r) / 2;
upd(le_[u], st, en, l, mid, v); upd(ri_[u], st, en, mid + 1, r, v);
tree[u] = max(tree[le_[u]], tree[ri_[u]]);
}
void upd(int u, int id, int l, int r, ll v) {
if(!u) return;
push(u, l, r);
if(l > id || r < id) return;
if(l == r) {
tree[u] = max(tree[u], v);
cn[u] = 1;
return;
}
int mid = (l + r) / 2;
if(id <= mid) {
if(!le_[u]) le_[u] = ++cur;
} else if(!ri_[u]) ri_[u] = ++cur;
upd(le_[u], id, l, mid, v); upd(ri_[u], id, mid + 1, r, v);
tree[u] = max(tree[le_[u]], tree[ri_[u]]);
cn[u] = cn[le_[u]] + cn[ri_[u]];
}
ll get(int u, int st, int en, int l,int r) {
if(!cn[u]) return 0;
push(u, l, r);
if(l > en || r < st) return 0;
if(st <= l && r <= en) return tree[u];
int mid = (l + r) / 2;
return max(get(le_[u], st, en, l, mid), get(ri_[u], st, en, mid + 1, r));
}
int find(int u) {
return (p[u] == u ? p[u] : p[u] = find(p[u]));
}
void merge(int u, int v, int H) {
u = find(u); v = find(v);
if(u == v) return;
if(comp[v].size() > comp[u].size()) swap(u, v);
p[v] = u;
ll ul = get(root[u], 1, H, 1, n), vl = get(root[v], 1, H, 1, n);
upd(root[u], 1, n, 1, n, vl);
for(int i = 0; i < comp[v].size(); i++) {
if(comp[v][i].f <= H) continue;
upd(root[u], comp[v][i].f, 1, n, get(root[v], 1, comp[v][i].f, 1, n) + ul);
comp[u].push_back(comp[v][i]);
}
comp[v].clear();
}
main() {
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int m;
cin >> n;
vector<pii> h;
for(int i = 1; i <= n; i++) {
cin >> a[i];
h.push_back({a[i], i});
}
cin >> m;
ll call = 0;
while(m--) {
int x, y, w;
cin >> x >> y >> w;
comp[x].push_back({y, w});
call += w;
}
for(int i = 1; i <= n; i++) {
p[i] = i;
root[i] = ++cur;
upd(root[i], 1, 1, n, 0);
for(int j = 0; j < comp[i].size(); j++)
upd(root[i], comp[i][j].f, 1, n, comp[i][j].s);
}
sort(h.begin(), h.end());
for(int i = 0; i < h.size(); i++) {
int id = h[i].s, H = h[i].f;
if(f[id - 1]) merge(id - 1, id, H);
if(f[id + 1]) merge(id + 1, id, H);
f[id] = 1;
}
cout << call - tree[root[find(1)]];
}
Compilation message
constellation3.cpp: In function 'void merge(int, int, int)':
constellation3.cpp:73:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i = 0; i < comp[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
constellation3.cpp: At global scope:
constellation3.cpp:81:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
81 | main() {
| ^~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:102:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int j = 0; j < comp[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~
constellation3.cpp:107:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int i = 0; i < h.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5240 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5172 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5240 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5172 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
23 |
Correct |
6 ms |
6612 KB |
Output is correct |
24 |
Correct |
6 ms |
6636 KB |
Output is correct |
25 |
Correct |
6 ms |
6612 KB |
Output is correct |
26 |
Correct |
7 ms |
6612 KB |
Output is correct |
27 |
Correct |
6 ms |
6612 KB |
Output is correct |
28 |
Correct |
8 ms |
6588 KB |
Output is correct |
29 |
Correct |
7 ms |
6668 KB |
Output is correct |
30 |
Correct |
8 ms |
6656 KB |
Output is correct |
31 |
Correct |
7 ms |
6612 KB |
Output is correct |
32 |
Correct |
6 ms |
6228 KB |
Output is correct |
33 |
Correct |
6 ms |
6484 KB |
Output is correct |
34 |
Correct |
6 ms |
6484 KB |
Output is correct |
35 |
Correct |
6 ms |
6484 KB |
Output is correct |
36 |
Correct |
6 ms |
6200 KB |
Output is correct |
37 |
Correct |
6 ms |
6228 KB |
Output is correct |
38 |
Correct |
6 ms |
5844 KB |
Output is correct |
39 |
Correct |
7 ms |
6356 KB |
Output is correct |
40 |
Correct |
6 ms |
6356 KB |
Output is correct |
41 |
Correct |
8 ms |
6268 KB |
Output is correct |
42 |
Correct |
9 ms |
6356 KB |
Output is correct |
43 |
Correct |
8 ms |
6228 KB |
Output is correct |
44 |
Correct |
6 ms |
6228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5240 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5172 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
23 |
Correct |
6 ms |
6612 KB |
Output is correct |
24 |
Correct |
6 ms |
6636 KB |
Output is correct |
25 |
Correct |
6 ms |
6612 KB |
Output is correct |
26 |
Correct |
7 ms |
6612 KB |
Output is correct |
27 |
Correct |
6 ms |
6612 KB |
Output is correct |
28 |
Correct |
8 ms |
6588 KB |
Output is correct |
29 |
Correct |
7 ms |
6668 KB |
Output is correct |
30 |
Correct |
8 ms |
6656 KB |
Output is correct |
31 |
Correct |
7 ms |
6612 KB |
Output is correct |
32 |
Correct |
6 ms |
6228 KB |
Output is correct |
33 |
Correct |
6 ms |
6484 KB |
Output is correct |
34 |
Correct |
6 ms |
6484 KB |
Output is correct |
35 |
Correct |
6 ms |
6484 KB |
Output is correct |
36 |
Correct |
6 ms |
6200 KB |
Output is correct |
37 |
Correct |
6 ms |
6228 KB |
Output is correct |
38 |
Correct |
6 ms |
5844 KB |
Output is correct |
39 |
Correct |
7 ms |
6356 KB |
Output is correct |
40 |
Correct |
6 ms |
6356 KB |
Output is correct |
41 |
Correct |
8 ms |
6268 KB |
Output is correct |
42 |
Correct |
9 ms |
6356 KB |
Output is correct |
43 |
Correct |
8 ms |
6228 KB |
Output is correct |
44 |
Correct |
6 ms |
6228 KB |
Output is correct |
45 |
Correct |
764 ms |
281304 KB |
Output is correct |
46 |
Correct |
737 ms |
275812 KB |
Output is correct |
47 |
Correct |
811 ms |
280616 KB |
Output is correct |
48 |
Correct |
792 ms |
276916 KB |
Output is correct |
49 |
Correct |
799 ms |
274548 KB |
Output is correct |
50 |
Correct |
726 ms |
272160 KB |
Output is correct |
51 |
Correct |
742 ms |
276880 KB |
Output is correct |
52 |
Correct |
745 ms |
281112 KB |
Output is correct |
53 |
Correct |
867 ms |
276212 KB |
Output is correct |
54 |
Correct |
649 ms |
201852 KB |
Output is correct |
55 |
Correct |
653 ms |
233648 KB |
Output is correct |
56 |
Correct |
683 ms |
239004 KB |
Output is correct |
57 |
Correct |
768 ms |
244800 KB |
Output is correct |
58 |
Correct |
641 ms |
194100 KB |
Output is correct |
59 |
Correct |
645 ms |
193764 KB |
Output is correct |
60 |
Correct |
387 ms |
124640 KB |
Output is correct |
61 |
Correct |
776 ms |
206272 KB |
Output is correct |
62 |
Correct |
653 ms |
206876 KB |
Output is correct |
63 |
Correct |
678 ms |
199312 KB |
Output is correct |
64 |
Correct |
753 ms |
199856 KB |
Output is correct |
65 |
Correct |
597 ms |
205600 KB |
Output is correct |
66 |
Correct |
692 ms |
199508 KB |
Output is correct |