#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2e5 + 5, maxN = 4e5 + 5, INF = 1e18;
struct range {
int l, r;
bool operator < (const range &b) const {
if (r - l != b.r - b.l) return r - l < b.r - b.l;
if (l != b.l) return l < b.l;
return r < b.r;
}
bool operator == (const range &b) const {
return (l == b.l && r == b.r);
}
};
struct node {
int x, y, c;
bool operator < (const node &b) const {
if (y!=b.y) return y < b.y;
if (x!=b.x) return x < b.x;
return c < b.c;
}
};
vector<node> star;
int dc1(node x) {return lower_bound(star.begin(), star.end(), x) - star.begin();}
vector<range> ranges;
int dc2(range x) {return lower_bound(ranges.begin(), ranges.end(), x) - ranges.begin();}
int sum[maxn];
vector<pair<int,int>> sub[maxn];
int ansR[maxn], ansS[maxN];
int worth(int x, int y, int R) {
int val = sum[R];
auto [l, r] = *prev(upper_bound(sub[R].begin(), sub[R].end(), make_pair(x, INF)));
if (l <= x && x <= r) {
int ID = dc2({l, r});
val -= ansR[ID];
val += worth(x, y, ID);
// if (x == 5 && y == 8) cout << "test " << ansS[dc1({x, A[x]+1, 0})] << endl;
// cout << "Test " << x << " " << A[x]+1 << " " << 0 << " " << ansS[dc1({x, A[x]+1, 0})] << endl;
}
return val;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
pair<int,int> a[n+1];
int A[n+1];
for (int i=1;i<=n;i++) {
cin >> a[i].first;
a[i].second = i;
A[i] = a[i].first;
}
sort(a+1, a+n+1);
int m, SUM = 0;
cin >> m;
for (int i=1;i<=m;i++) {
int x, y, c;
cin >> x >> y >> c;
star.push_back({x, y, c});
SUM += c;
}
for (int i=1;i<=n;i++) star.push_back({a[i].second, a[i].first + 1, 0});
set<int> s;
for (int i=0;i<=n+1;i++) s.insert(i);
int last = 1;
for (int i=1;i<=n;i++) {
for (; a[last].first < a[i].first; last++) {
auto it = s.upper_bound(a[last].second);
ranges.push_back({*prev(it) + 1, *it - 1});
// cout << "1 " << a[last].second << " " << ranges.back().first << " " << ranges.back().second << endl;
}
s.erase(a[i].second);
}
for (; last<=n; last++) {
auto it = s.upper_bound(a[last].second);
ranges.push_back({*prev(it) + 1, *it - 1});
// cout << "2 " << a[last].second << " " << ranges.back().first << " " << ranges.back().second << endl;
}
sort(ranges.begin(), ranges.end()); ranges.erase(unique(ranges.begin(), ranges.end()), ranges.end());
int sz = ranges.size();
sort(star.begin(), star.end());
vector<node> mem[sz];
for (int i=0;i<sz;i++) sub[i] = {{0, 0}, {ranges[i].l, -1}};
for (int i=1;i<=n;i++) s.insert(i);
last = 1;
for (auto [x, y, c]:star) {
// cout << x << " " << y << " " << c << endl;
for (;last <= n && a[last].first < y; last++) s.erase(a[last].second);
auto it = s.lower_bound(x);
int l = *prev(it) + 1, r = *it - 1;
int id = dc2({l, r});
if (c!=0) mem[id].push_back({x, y, c});
// cout << id << endl;
if (c==0) {
if (x-1>=sub[id].back().first) {
sub[id].back().second = x-1;
sub[id].push_back({x+1, -1});
} else sub[id].back().first = x+1;
}
}
for (int i=0;i<sz;i++) {
if (sub[i].back().first <= ranges[i].r) sub[i].back().second = ranges[i].r;
else sub[i].pop_back();
sub[i].push_back({INF, INF});
}
for (int i=0;i<sz;i++) {
sum[i] = 0;
for (auto [l, r]:sub[i]) {
if (l!=0 && l!=INF) sum[i] += ansR[dc2({l, r})];
}
ansR[i] = sum[i];
// cout << ranges[i].l << " " << ranges[i].r << " " << sum << endl;
for (auto [x, y, c]:mem[i]) {
int id = dc1({x, y, c});
ansS[id] = worth(x, y, i) + c;
ansR[i] = max(ansS[id], ansR[i]);
}
}
cout << SUM - ansR[dc2({1, n})] << endl;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:50:9: warning: variable 'A' set but not used [-Wunused-but-set-variable]
50 | int A[n+1];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5224 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
5228 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5224 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
5228 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
5 ms |
5724 KB |
Output is correct |
24 |
Correct |
5 ms |
5748 KB |
Output is correct |
25 |
Correct |
5 ms |
5724 KB |
Output is correct |
26 |
Correct |
5 ms |
5724 KB |
Output is correct |
27 |
Correct |
6 ms |
5724 KB |
Output is correct |
28 |
Correct |
5 ms |
5772 KB |
Output is correct |
29 |
Correct |
5 ms |
5704 KB |
Output is correct |
30 |
Correct |
5 ms |
5724 KB |
Output is correct |
31 |
Correct |
5 ms |
6016 KB |
Output is correct |
32 |
Correct |
22 ms |
5696 KB |
Output is correct |
33 |
Correct |
14 ms |
5724 KB |
Output is correct |
34 |
Correct |
13 ms |
5468 KB |
Output is correct |
35 |
Correct |
10 ms |
5724 KB |
Output is correct |
36 |
Correct |
3 ms |
5468 KB |
Output is correct |
37 |
Correct |
3 ms |
5468 KB |
Output is correct |
38 |
Correct |
3 ms |
5724 KB |
Output is correct |
39 |
Correct |
5 ms |
5512 KB |
Output is correct |
40 |
Correct |
16 ms |
5444 KB |
Output is correct |
41 |
Correct |
4 ms |
5468 KB |
Output is correct |
42 |
Correct |
4 ms |
5468 KB |
Output is correct |
43 |
Correct |
17 ms |
5660 KB |
Output is correct |
44 |
Correct |
6 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5224 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
5228 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
5 ms |
5724 KB |
Output is correct |
24 |
Correct |
5 ms |
5748 KB |
Output is correct |
25 |
Correct |
5 ms |
5724 KB |
Output is correct |
26 |
Correct |
5 ms |
5724 KB |
Output is correct |
27 |
Correct |
6 ms |
5724 KB |
Output is correct |
28 |
Correct |
5 ms |
5772 KB |
Output is correct |
29 |
Correct |
5 ms |
5704 KB |
Output is correct |
30 |
Correct |
5 ms |
5724 KB |
Output is correct |
31 |
Correct |
5 ms |
6016 KB |
Output is correct |
32 |
Correct |
22 ms |
5696 KB |
Output is correct |
33 |
Correct |
14 ms |
5724 KB |
Output is correct |
34 |
Correct |
13 ms |
5468 KB |
Output is correct |
35 |
Correct |
10 ms |
5724 KB |
Output is correct |
36 |
Correct |
3 ms |
5468 KB |
Output is correct |
37 |
Correct |
3 ms |
5468 KB |
Output is correct |
38 |
Correct |
3 ms |
5724 KB |
Output is correct |
39 |
Correct |
5 ms |
5512 KB |
Output is correct |
40 |
Correct |
16 ms |
5444 KB |
Output is correct |
41 |
Correct |
4 ms |
5468 KB |
Output is correct |
42 |
Correct |
4 ms |
5468 KB |
Output is correct |
43 |
Correct |
17 ms |
5660 KB |
Output is correct |
44 |
Correct |
6 ms |
5468 KB |
Output is correct |
45 |
Correct |
598 ms |
62760 KB |
Output is correct |
46 |
Correct |
578 ms |
62116 KB |
Output is correct |
47 |
Correct |
615 ms |
61808 KB |
Output is correct |
48 |
Correct |
610 ms |
62372 KB |
Output is correct |
49 |
Correct |
650 ms |
61092 KB |
Output is correct |
50 |
Correct |
573 ms |
61088 KB |
Output is correct |
51 |
Correct |
579 ms |
61352 KB |
Output is correct |
52 |
Correct |
593 ms |
62704 KB |
Output is correct |
53 |
Correct |
593 ms |
62168 KB |
Output is correct |
54 |
Execution timed out |
1541 ms |
55204 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |