#include <bits/stdc++.h>
#define Loop(x, l, r) for (ll x = (l); x < (r); ++x)
#define LoopR(x, l, r) for (ll x = (r)-1; x >= (l); --x)
typedef long long ll;
typedef std::pair<int,int> pii;
using namespace std;
struct node {
int l, r;
int pos;
ll C;
vector<node *> adj;
ll dp;
int st, ft;
};
vector<node> nds;
const int N = 200'010;
pii a[N];
struct star {
int x, y;
ll C;
} stars[N];
int n, m;
namespace segmx {
int a[2*N];
void up(int l, int r, int x) {
l += N;
r += N;
while (l < r) {
if (l&1) { a[l] = max(a[l], x); ++l; }
if (r&1) { --r; a[r] = max(a[r], x); }
l /= 2;
r /= 2;
}
}
int get(int p) {
int ans = 0;
p += N;
while (p) {
ans = max(ans, a[p]);
p /= 2;
}
return ans;
}
}
namespace segadd {
ll a[2*N];
void up(int l, int r, ll x) {
l += N;
r += N;
while (l < r) {
if (l&1) { a[l] = a[l]+x; ++l; }
if (r&1) { --r; a[r] = a[r]+x; }
l /= 2;
r /= 2;
}
}
ll get(int p) {
ll ans = 0;
p += N;
while (p) {
ans += a[p];
p /= 2;
}
return ans;
}
}
void mk_nodes()
{
sort(stars, stars+m, [](star &a, star &b) {
return a.y < b.y;
});
sort(a, a+n);
set<int> cur_buildings = {-1, n};
int p = n-1;
LoopR (i,0,m) {
while (p >= 0 && a[p].first > stars[i].y)
cur_buildings.insert(a[p--].second);
int l = *--cur_buildings.lower_bound(stars[i].x)+1;
int r = *cur_buildings.upper_bound(stars[i].x);
node dard = {
.l = l,
.r = r,
.pos = stars[i].x,
.C = stars[i].C,
.adj = {},
.dp = 0,
.st = 0,
.ft = 0,
};
nds.push_back(dard);
}
sort(nds.begin(), nds.end(), [](node &a, node &b) {
return a.l < b.l || (a.l == b.l && a.r > b.r);
});
}
void process(node *t)
{
node *c = NULL;
t->dp = 0;
for (auto u : t->adj) {
t->dp += u->dp;
if (t->pos < u->l || u->r <= t->pos)
continue;
assert(c == NULL);
c = u;
}
ll x = t->dp + t->C;
if (c) {
x -= c->dp;
int pos = segmx::get(t->pos);
//cout << t->l << ' ' << t->pos << ' ' << t->r << " vv " << pos << '\n';
x += segadd::get(nds[pos].st);
}
for (auto u : t->adj)
segadd::up(u->st, u->ft, t->dp - u->dp);
segadd::up(t->st, t->st+1, t->dp);
t->dp = max(t->dp, x);
}
void dfs_stft(node *t, int &pos)
{
t->st = pos++;
for (node *u : t->adj)
dfs_stft(u, pos);
t->ft = pos;
}
void dfs_dp(node *t)
{
for (node *u : t->adj)
dfs_dp(u);
process(t);
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n;
Loop (i,0,n) {
cin >> a[i].first;
a[i].second = i;
}
cin >> m;
ll sumC = 0;
Loop (i,0,m) {
cin >> stars[i].x >> stars[i].y >> stars[i].C;
sumC += stars[i].C;
--stars[i].x; --stars[i].y;
//cout << stars[i].x << ' ' << stars[i].y << '\n';
}
mk_nodes();
vector<node *> vec;
vector<node *> rts;
Loop (i,0,nds.size()) {
node *t = &nds[i];
while (vec.size() && vec.back()->r <= t->l)
vec.pop_back();
if (vec.size())
vec.back()->adj.push_back(t);
else
rts.push_back(t);
vec.push_back(t);
segmx::up(t->l, t->r, i);
}
int dard = 0;
for (auto t : rts)
dfs_stft(t, dard);
ll ans = 0;
for (auto t : rts) {
dfs_dp(t);
ans += t->dp;
}
Loop (i,0,nds.size()) {
//cout << nds[i].l << ' ' << nds[i].pos << ' ' << nds[i].r << " -> " << nds[i].dp << '\n';
}
cout << sumC - ans << '\n';
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:2:42: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
2 | #define Loop(x, l, r) for (ll x = (l); x < (r); ++x)
| ^
constellation3.cpp:163:2: note: in expansion of macro 'Loop'
163 | Loop (i,0,nds.size()) {
| ^~~~
constellation3.cpp:2:42: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
2 | #define Loop(x, l, r) for (ll x = (l); x < (r); ++x)
| ^
constellation3.cpp:182:2: note: in expansion of macro 'Loop'
182 | Loop (i,0,nds.size()) {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
452 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
452 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
720 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
2 ms |
724 KB |
Output is correct |
30 |
Correct |
2 ms |
724 KB |
Output is correct |
31 |
Correct |
3 ms |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
724 KB |
Output is correct |
33 |
Correct |
2 ms |
852 KB |
Output is correct |
34 |
Correct |
2 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
852 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
852 KB |
Output is correct |
38 |
Correct |
2 ms |
852 KB |
Output is correct |
39 |
Correct |
2 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
784 KB |
Output is correct |
42 |
Correct |
2 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
816 KB |
Output is correct |
44 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
452 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
720 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
2 ms |
724 KB |
Output is correct |
30 |
Correct |
2 ms |
724 KB |
Output is correct |
31 |
Correct |
3 ms |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
724 KB |
Output is correct |
33 |
Correct |
2 ms |
852 KB |
Output is correct |
34 |
Correct |
2 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
852 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
852 KB |
Output is correct |
38 |
Correct |
2 ms |
852 KB |
Output is correct |
39 |
Correct |
2 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
784 KB |
Output is correct |
42 |
Correct |
2 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
816 KB |
Output is correct |
44 |
Correct |
2 ms |
724 KB |
Output is correct |
45 |
Correct |
250 ms |
36752 KB |
Output is correct |
46 |
Correct |
248 ms |
36088 KB |
Output is correct |
47 |
Correct |
296 ms |
36892 KB |
Output is correct |
48 |
Correct |
243 ms |
36168 KB |
Output is correct |
49 |
Correct |
247 ms |
36096 KB |
Output is correct |
50 |
Correct |
248 ms |
35740 KB |
Output is correct |
51 |
Correct |
248 ms |
36260 KB |
Output is correct |
52 |
Correct |
246 ms |
36996 KB |
Output is correct |
53 |
Correct |
244 ms |
36148 KB |
Output is correct |
54 |
Correct |
238 ms |
42048 KB |
Output is correct |
55 |
Correct |
243 ms |
41960 KB |
Output is correct |
56 |
Correct |
228 ms |
41300 KB |
Output is correct |
57 |
Correct |
227 ms |
41432 KB |
Output is correct |
58 |
Correct |
186 ms |
40868 KB |
Output is correct |
59 |
Correct |
192 ms |
41060 KB |
Output is correct |
60 |
Correct |
185 ms |
40592 KB |
Output is correct |
61 |
Correct |
212 ms |
37080 KB |
Output is correct |
62 |
Correct |
205 ms |
41376 KB |
Output is correct |
63 |
Correct |
188 ms |
37256 KB |
Output is correct |
64 |
Correct |
193 ms |
35984 KB |
Output is correct |
65 |
Correct |
240 ms |
40736 KB |
Output is correct |
66 |
Correct |
203 ms |
37660 KB |
Output is correct |