//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 |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 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 |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 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 |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
852 KB |
Output is correct |
24 |
Correct |
3 ms |
852 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
3 ms |
852 KB |
Output is correct |
27 |
Correct |
3 ms |
856 KB |
Output is correct |
28 |
Correct |
3 ms |
852 KB |
Output is correct |
29 |
Correct |
4 ms |
852 KB |
Output is correct |
30 |
Correct |
3 ms |
852 KB |
Output is correct |
31 |
Correct |
3 ms |
852 KB |
Output is correct |
32 |
Correct |
4 ms |
980 KB |
Output is correct |
33 |
Correct |
3 ms |
1036 KB |
Output is correct |
34 |
Correct |
3 ms |
980 KB |
Output is correct |
35 |
Correct |
3 ms |
852 KB |
Output is correct |
36 |
Correct |
4 ms |
1072 KB |
Output is correct |
37 |
Correct |
3 ms |
980 KB |
Output is correct |
38 |
Correct |
3 ms |
1236 KB |
Output is correct |
39 |
Correct |
3 ms |
852 KB |
Output is correct |
40 |
Correct |
3 ms |
1108 KB |
Output is correct |
41 |
Correct |
3 ms |
852 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
3 ms |
1108 KB |
Output is correct |
44 |
Correct |
3 ms |
868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 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 |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
852 KB |
Output is correct |
24 |
Correct |
3 ms |
852 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
3 ms |
852 KB |
Output is correct |
27 |
Correct |
3 ms |
856 KB |
Output is correct |
28 |
Correct |
3 ms |
852 KB |
Output is correct |
29 |
Correct |
4 ms |
852 KB |
Output is correct |
30 |
Correct |
3 ms |
852 KB |
Output is correct |
31 |
Correct |
3 ms |
852 KB |
Output is correct |
32 |
Correct |
4 ms |
980 KB |
Output is correct |
33 |
Correct |
3 ms |
1036 KB |
Output is correct |
34 |
Correct |
3 ms |
980 KB |
Output is correct |
35 |
Correct |
3 ms |
852 KB |
Output is correct |
36 |
Correct |
4 ms |
1072 KB |
Output is correct |
37 |
Correct |
3 ms |
980 KB |
Output is correct |
38 |
Correct |
3 ms |
1236 KB |
Output is correct |
39 |
Correct |
3 ms |
852 KB |
Output is correct |
40 |
Correct |
3 ms |
1108 KB |
Output is correct |
41 |
Correct |
3 ms |
852 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
3 ms |
1108 KB |
Output is correct |
44 |
Correct |
3 ms |
868 KB |
Output is correct |
45 |
Correct |
332 ms |
62668 KB |
Output is correct |
46 |
Correct |
311 ms |
62124 KB |
Output is correct |
47 |
Correct |
325 ms |
62096 KB |
Output is correct |
48 |
Correct |
321 ms |
62580 KB |
Output is correct |
49 |
Correct |
300 ms |
61740 KB |
Output is correct |
50 |
Correct |
312 ms |
61588 KB |
Output is correct |
51 |
Correct |
299 ms |
61956 KB |
Output is correct |
52 |
Correct |
328 ms |
62436 KB |
Output is correct |
53 |
Correct |
301 ms |
62164 KB |
Output is correct |
54 |
Correct |
452 ms |
85240 KB |
Output is correct |
55 |
Correct |
470 ms |
78208 KB |
Output is correct |
56 |
Correct |
434 ms |
74504 KB |
Output is correct |
57 |
Correct |
419 ms |
71824 KB |
Output is correct |
58 |
Correct |
387 ms |
78512 KB |
Output is correct |
59 |
Correct |
379 ms |
78540 KB |
Output is correct |
60 |
Correct |
322 ms |
98928 KB |
Output is correct |
61 |
Correct |
320 ms |
62176 KB |
Output is correct |
62 |
Correct |
455 ms |
88484 KB |
Output is correct |
63 |
Correct |
313 ms |
61732 KB |
Output is correct |
64 |
Correct |
305 ms |
61460 KB |
Output is correct |
65 |
Correct |
435 ms |
88908 KB |
Output is correct |
66 |
Correct |
318 ms |
61516 KB |
Output is correct |