//Sylwia Sapkowska
#include <bits/stdc++.h>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
const int oo = 1e18, K = 30;
struct star{
int x, y, c;
void read(){
cin >> x >> y >> c;
}
};
struct TreeMax{
vector<pair<int, int>>tab;
int size = 1;
TreeMax(int n){
while (size < n) size*=2;
tab.assign(2*size, {-oo, -oo});
}
void update(int x, pair<int, int> v){
x += size;
tab[x] = v;
while (x){
x/=2;
tab[x] = max(tab[2*x], tab[2*x+1]);
}
}
int query(int l, int r){
pair<int, int>ans = {-oo, -oo};
l = l+size-1;
r = r+size+1;
while (r-l>1){
if (!(l&1)) ans = max(ans, tab[l+1]);
if (r&1) ans = max(ans, tab[r-1]);
l/=2;r/=2;
}
return ans.second;
}
};
struct TreeSum{
vector<int>tab;
int size = 1;
TreeSum(int n){
while (size < n) size*=2;
tab.resize(2*size);
}
void update(int x, int v){
x += size;
tab[x] += v;
while (x){
x/=2;
tab[x] = tab[2*x] + tab[2*x+1];
}
}
int query(int l, int r){
int ans = 0;
l = l+size-1;
r = r+size+1;
while (r-l>1){
if (!(l&1)) ans += tab[l+1];
if (r&1) ans += tab[r-1];
l/=2;r/=2;
}
return ans;
}
};
void solve(){
int n; cin >> n;
vector<int>a(n+1);
for (int i = 1; i<=n; i++) cin >> a[i];
int m; cin >> m;
vector<star>b(m);
int s = 0;
for (auto &x: b) {
x.read();
s += x.c;
}
sort(b.begin(), b.end(), [](auto x, auto y){return x.x < y.x;});
vector<vector<int>>g(n+1);
vector<vector<int>>paths(n+1);
int c = 0;
TreeMax smax(n+1);
for (int i = 1; i<=n; i++) smax.update(i, {a[i], i});
TreeMax gw(m+1);
for (int i = 0; i<m; i++) gw.update(i, {b[i].y, i});
vector<int>kon(n+1);
function<void(int, int, int)>rek = [&](int l, int r, int pa){
if (l > r) return;
c++;
if (pa != -1) g[pa].emplace_back(c);
int h = smax.query(l, r);
kon[h] = c;
// debug(l, r, h, a[h]);
int L = 0, R = m-1, ll = oo, rr = -oo;
while (R >= L){
int mid = (L+R)/2;
if (b[mid].x >= l){
ll = mid;
R = mid-1;
} else L = mid+1;
}
L = 0, R = m-1;
while (R >= L){
int mid = (L+R)/2;
if (b[mid].x <= r){
rr = mid;
L = mid+1;
} else {
R = mid-1;
}
}
if (ll <= rr){
// debug(l, r, ll, rr);
while (1){
int x = gw.query(ll, rr);
if (x == -oo || b[x].y <= a[h]) break;
paths[c].emplace_back(x);
gw.update(x, {-oo, -oo});
}
}
int p = c;
rek(l, h-1, p);
rek(h+1, r, p);
};
rek(1, n, -1);
vector<int>dp(n+1), pre(n+1), post(n+1);
int t = 0;
function<void(int)>dfs = [&](int v){
pre[v] = t++;
for (auto x: g[v]) dfs(x);
post[v] = t++;
};
// for (int i = 1; i<=n; i++){
// for (auto x: paths[i]){
// cerr << i << " " << kon[b[x].x] << " " << b[x].c << "\n";
// }
// }
dfs(1);
TreeSum seg(2*n+3), dp_sum(2*n+3);
function<void(int, int)>DFS = [&](int v, int pa){
for (auto x: g[v]) {
DFS(x, v);
dp[v] += dp[x];
}
int curr = (pa == -1 ? 0 : dp_sum.query(0, pre[v]-1) - seg.query(0, pre[v]-1));
for (auto x: paths[v]) dp[v] = max(dp[v], seg.query(0, pre[kon[b[x].x]]) - dp_sum.query(0, pre[kon[b[x].x]]) + b[x].c + curr);
if (pa != -1){
seg.update(pre[pa], dp[v]);
seg.update(post[pa], -dp[v]);
}
dp_sum.update(pre[v], dp[v]);
dp_sum.update(post[v], -dp[v]);
};
DFS(1, -1);
// debug(dp);
cout << s-dp[1] << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
448 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
452 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
448 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
452 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
5 ms |
848 KB |
Output is correct |
24 |
Correct |
5 ms |
840 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
4 ms |
852 KB |
Output is correct |
27 |
Correct |
5 ms |
852 KB |
Output is correct |
28 |
Correct |
4 ms |
852 KB |
Output is correct |
29 |
Correct |
4 ms |
852 KB |
Output is correct |
30 |
Correct |
5 ms |
852 KB |
Output is correct |
31 |
Correct |
4 ms |
852 KB |
Output is correct |
32 |
Correct |
4 ms |
1108 KB |
Output is correct |
33 |
Correct |
6 ms |
980 KB |
Output is correct |
34 |
Correct |
4 ms |
980 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
3 ms |
1108 KB |
Output is correct |
37 |
Correct |
4 ms |
980 KB |
Output is correct |
38 |
Correct |
4 ms |
1236 KB |
Output is correct |
39 |
Correct |
3 ms |
852 KB |
Output is correct |
40 |
Correct |
4 ms |
1108 KB |
Output is correct |
41 |
Correct |
5 ms |
852 KB |
Output is correct |
42 |
Correct |
4 ms |
852 KB |
Output is correct |
43 |
Correct |
4 ms |
1108 KB |
Output is correct |
44 |
Correct |
5 ms |
784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
448 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
452 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
5 ms |
848 KB |
Output is correct |
24 |
Correct |
5 ms |
840 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
4 ms |
852 KB |
Output is correct |
27 |
Correct |
5 ms |
852 KB |
Output is correct |
28 |
Correct |
4 ms |
852 KB |
Output is correct |
29 |
Correct |
4 ms |
852 KB |
Output is correct |
30 |
Correct |
5 ms |
852 KB |
Output is correct |
31 |
Correct |
4 ms |
852 KB |
Output is correct |
32 |
Correct |
4 ms |
1108 KB |
Output is correct |
33 |
Correct |
6 ms |
980 KB |
Output is correct |
34 |
Correct |
4 ms |
980 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
3 ms |
1108 KB |
Output is correct |
37 |
Correct |
4 ms |
980 KB |
Output is correct |
38 |
Correct |
4 ms |
1236 KB |
Output is correct |
39 |
Correct |
3 ms |
852 KB |
Output is correct |
40 |
Correct |
4 ms |
1108 KB |
Output is correct |
41 |
Correct |
5 ms |
852 KB |
Output is correct |
42 |
Correct |
4 ms |
852 KB |
Output is correct |
43 |
Correct |
4 ms |
1108 KB |
Output is correct |
44 |
Correct |
5 ms |
784 KB |
Output is correct |
45 |
Correct |
447 ms |
62732 KB |
Output is correct |
46 |
Correct |
366 ms |
62264 KB |
Output is correct |
47 |
Correct |
351 ms |
62156 KB |
Output is correct |
48 |
Correct |
350 ms |
62560 KB |
Output is correct |
49 |
Correct |
320 ms |
61912 KB |
Output is correct |
50 |
Correct |
316 ms |
61772 KB |
Output is correct |
51 |
Correct |
357 ms |
61996 KB |
Output is correct |
52 |
Correct |
331 ms |
62432 KB |
Output is correct |
53 |
Correct |
357 ms |
62368 KB |
Output is correct |
54 |
Correct |
543 ms |
85400 KB |
Output is correct |
55 |
Correct |
546 ms |
78384 KB |
Output is correct |
56 |
Correct |
478 ms |
74656 KB |
Output is correct |
57 |
Correct |
504 ms |
72072 KB |
Output is correct |
58 |
Correct |
397 ms |
78572 KB |
Output is correct |
59 |
Correct |
410 ms |
78428 KB |
Output is correct |
60 |
Correct |
318 ms |
98976 KB |
Output is correct |
61 |
Correct |
324 ms |
62456 KB |
Output is correct |
62 |
Correct |
451 ms |
88520 KB |
Output is correct |
63 |
Correct |
346 ms |
61908 KB |
Output is correct |
64 |
Correct |
308 ms |
61432 KB |
Output is correct |
65 |
Correct |
482 ms |
88992 KB |
Output is correct |
66 |
Correct |
338 ms |
61676 KB |
Output is correct |