#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define MASK(i) (1LL<<(i))
#define BIT(mask,i) (((mask) >> (i))&1LL)
const ll MAXN = 2e5;
const ll MAXK = 18;
ll n,m;
ll a[MAXN + 1];
namespace max_height{
ll sp[MAXK][MAXN+1];
ll best(ll x,ll y){
if (a[x]>a[y])return x;
return y;
}
void init(){
for (ll i = 1;i <= n; i++){
sp[0][i] = i;
}
for (ll j = 1;j < MAXK;j ++){
for (ll i = 1;i + MASK(j) - 1 <= n;i ++){
sp[j][i] = best(sp[j-1][i],sp[j-1][i+MASK(j-1)]);
}
}
}
ll query(ll l,ll r){
ll sz = __lg(r-l+1);
return best(sp[sz][l],sp[sz][r-MASK(sz)+1]);
}
}
struct star{
ll x,y,c;
};
star b[MAXN + 1];
vector <ll> g[MAXN + 1];
ll pre[MAXK][MAXN + 1];
void add_edge(ll p,ll u){
g[p].push_back(u);
pre[0][u] = p;
}
ll build_tree(ll l,ll r){
// cout<<l<<' '<<r<<'\n';
ll mid = max_height::query(l,r);
if (l <= mid - 1)add_edge(mid,build_tree(l,mid-1));
if (mid + 1 <= r)add_edge(mid,build_tree(mid+1,r));
return mid;
}
vector <pll> path[MAXN+1];
ll dp[MAXN+1];
ll in[MAXN+1],out[MAXN+1];
ll timeDFS;
ll BIT[MAXN+1];
void update(ll i,ll x){
for (;i <= n;i += i & -i)BIT[i]+=x;
}
void update(ll l,ll r,ll x){
update(l,x);
update(r+1,-x);
}
ll get(ll i){
ll res = 0;
for (;i > 0;i -= i & -i)res += BIT[i];
return res;
}
void dfs(ll u){
in[u] = ++timeDFS;
for (auto v:g[u]){
dfs(v);
}
out[u] = timeDFS;
}
void solve(ll u){
for (auto v:g[u]){solve(v);dp[u] += dp[v];update(in[u],in[u],dp[v]);}
if (sz(g[u])==2){
ll v1,v2;
v1 = g[u][0];
v2 = g[u][1];
update(in[v1],out[v1],dp[v2]);
update(in[v2],out[v2],dp[v1]);
}
for (auto x:path[u]){
dp[u] = max(dp[u],x.se+get(in[x.fi]));
}
// cout<<"DP "<<u<<' '<<dp[u]<<'\n';
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
cin>>n;
for (ll i = 1;i <= n;i ++)cin>>a[i];
cin>>m;
ll sum = 0;
for (ll i = 1;i <= m;i ++){
cin>>b[i].x>>b[i].y>>b[i].c;
sum += b[i].c;
}
max_height::init();
ll root = build_tree(1,n);
for (ll i = 1;i < MAXK;i ++){
for (ll u = 1;u <= n;u ++){
pre[i][u] = pre[i-1][pre[i-1][u]];
}
}
// for (ll i = 1;i <= n; i++){
// cout<<pre[0][i]<<' '<<pre[1][i]<<' '<<pre[2][i]<<'\n';
// }
// cout<<"END\n";
a[0] = n;
for (ll i = 1;i <= m;i ++){
ll p = b[i].x;
for (ll j = MAXK-1;j>=0;j--){
if (a[pre[j][p]] < b[i].y){
p = pre[j][p];
}
}
// cout<<p<<' '<<b[i].x<<' '<<b[i].c<<'\n';
path[p].push_back({b[i].x,b[i].c});
}
dfs(root);
solve(root);
cout<<sum-dp[root];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
59928 KB |
Output is correct |
2 |
Correct |
9 ms |
59996 KB |
Output is correct |
3 |
Correct |
8 ms |
59996 KB |
Output is correct |
4 |
Correct |
8 ms |
59992 KB |
Output is correct |
5 |
Correct |
9 ms |
59992 KB |
Output is correct |
6 |
Correct |
8 ms |
59996 KB |
Output is correct |
7 |
Correct |
8 ms |
59900 KB |
Output is correct |
8 |
Correct |
8 ms |
59996 KB |
Output is correct |
9 |
Correct |
8 ms |
59996 KB |
Output is correct |
10 |
Correct |
9 ms |
59956 KB |
Output is correct |
11 |
Correct |
8 ms |
59996 KB |
Output is correct |
12 |
Correct |
8 ms |
59996 KB |
Output is correct |
13 |
Correct |
8 ms |
59996 KB |
Output is correct |
14 |
Correct |
9 ms |
59996 KB |
Output is correct |
15 |
Correct |
8 ms |
59996 KB |
Output is correct |
16 |
Correct |
8 ms |
59996 KB |
Output is correct |
17 |
Correct |
8 ms |
59996 KB |
Output is correct |
18 |
Correct |
8 ms |
59832 KB |
Output is correct |
19 |
Correct |
9 ms |
59996 KB |
Output is correct |
20 |
Correct |
8 ms |
59996 KB |
Output is correct |
21 |
Correct |
9 ms |
59996 KB |
Output is correct |
22 |
Correct |
8 ms |
59996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
59928 KB |
Output is correct |
2 |
Correct |
9 ms |
59996 KB |
Output is correct |
3 |
Correct |
8 ms |
59996 KB |
Output is correct |
4 |
Correct |
8 ms |
59992 KB |
Output is correct |
5 |
Correct |
9 ms |
59992 KB |
Output is correct |
6 |
Correct |
8 ms |
59996 KB |
Output is correct |
7 |
Correct |
8 ms |
59900 KB |
Output is correct |
8 |
Correct |
8 ms |
59996 KB |
Output is correct |
9 |
Correct |
8 ms |
59996 KB |
Output is correct |
10 |
Correct |
9 ms |
59956 KB |
Output is correct |
11 |
Correct |
8 ms |
59996 KB |
Output is correct |
12 |
Correct |
8 ms |
59996 KB |
Output is correct |
13 |
Correct |
8 ms |
59996 KB |
Output is correct |
14 |
Correct |
9 ms |
59996 KB |
Output is correct |
15 |
Correct |
8 ms |
59996 KB |
Output is correct |
16 |
Correct |
8 ms |
59996 KB |
Output is correct |
17 |
Correct |
8 ms |
59996 KB |
Output is correct |
18 |
Correct |
8 ms |
59832 KB |
Output is correct |
19 |
Correct |
9 ms |
59996 KB |
Output is correct |
20 |
Correct |
8 ms |
59996 KB |
Output is correct |
21 |
Correct |
9 ms |
59996 KB |
Output is correct |
22 |
Correct |
8 ms |
59996 KB |
Output is correct |
23 |
Correct |
11 ms |
64092 KB |
Output is correct |
24 |
Correct |
11 ms |
64252 KB |
Output is correct |
25 |
Correct |
9 ms |
64092 KB |
Output is correct |
26 |
Correct |
10 ms |
64092 KB |
Output is correct |
27 |
Correct |
10 ms |
64128 KB |
Output is correct |
28 |
Correct |
11 ms |
64092 KB |
Output is correct |
29 |
Correct |
10 ms |
64092 KB |
Output is correct |
30 |
Correct |
10 ms |
64092 KB |
Output is correct |
31 |
Correct |
10 ms |
64092 KB |
Output is correct |
32 |
Correct |
10 ms |
64348 KB |
Output is correct |
33 |
Correct |
10 ms |
64092 KB |
Output is correct |
34 |
Correct |
10 ms |
64092 KB |
Output is correct |
35 |
Correct |
9 ms |
64092 KB |
Output is correct |
36 |
Correct |
10 ms |
64348 KB |
Output is correct |
37 |
Correct |
9 ms |
64300 KB |
Output is correct |
38 |
Correct |
10 ms |
64416 KB |
Output is correct |
39 |
Correct |
10 ms |
64092 KB |
Output is correct |
40 |
Correct |
10 ms |
64260 KB |
Output is correct |
41 |
Correct |
10 ms |
64084 KB |
Output is correct |
42 |
Correct |
10 ms |
64092 KB |
Output is correct |
43 |
Correct |
10 ms |
64188 KB |
Output is correct |
44 |
Correct |
11 ms |
64256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
59928 KB |
Output is correct |
2 |
Correct |
9 ms |
59996 KB |
Output is correct |
3 |
Correct |
8 ms |
59996 KB |
Output is correct |
4 |
Correct |
8 ms |
59992 KB |
Output is correct |
5 |
Correct |
9 ms |
59992 KB |
Output is correct |
6 |
Correct |
8 ms |
59996 KB |
Output is correct |
7 |
Correct |
8 ms |
59900 KB |
Output is correct |
8 |
Correct |
8 ms |
59996 KB |
Output is correct |
9 |
Correct |
8 ms |
59996 KB |
Output is correct |
10 |
Correct |
9 ms |
59956 KB |
Output is correct |
11 |
Correct |
8 ms |
59996 KB |
Output is correct |
12 |
Correct |
8 ms |
59996 KB |
Output is correct |
13 |
Correct |
8 ms |
59996 KB |
Output is correct |
14 |
Correct |
9 ms |
59996 KB |
Output is correct |
15 |
Correct |
8 ms |
59996 KB |
Output is correct |
16 |
Correct |
8 ms |
59996 KB |
Output is correct |
17 |
Correct |
8 ms |
59996 KB |
Output is correct |
18 |
Correct |
8 ms |
59832 KB |
Output is correct |
19 |
Correct |
9 ms |
59996 KB |
Output is correct |
20 |
Correct |
8 ms |
59996 KB |
Output is correct |
21 |
Correct |
9 ms |
59996 KB |
Output is correct |
22 |
Correct |
8 ms |
59996 KB |
Output is correct |
23 |
Correct |
11 ms |
64092 KB |
Output is correct |
24 |
Correct |
11 ms |
64252 KB |
Output is correct |
25 |
Correct |
9 ms |
64092 KB |
Output is correct |
26 |
Correct |
10 ms |
64092 KB |
Output is correct |
27 |
Correct |
10 ms |
64128 KB |
Output is correct |
28 |
Correct |
11 ms |
64092 KB |
Output is correct |
29 |
Correct |
10 ms |
64092 KB |
Output is correct |
30 |
Correct |
10 ms |
64092 KB |
Output is correct |
31 |
Correct |
10 ms |
64092 KB |
Output is correct |
32 |
Correct |
10 ms |
64348 KB |
Output is correct |
33 |
Correct |
10 ms |
64092 KB |
Output is correct |
34 |
Correct |
10 ms |
64092 KB |
Output is correct |
35 |
Correct |
9 ms |
64092 KB |
Output is correct |
36 |
Correct |
10 ms |
64348 KB |
Output is correct |
37 |
Correct |
9 ms |
64300 KB |
Output is correct |
38 |
Correct |
10 ms |
64416 KB |
Output is correct |
39 |
Correct |
10 ms |
64092 KB |
Output is correct |
40 |
Correct |
10 ms |
64260 KB |
Output is correct |
41 |
Correct |
10 ms |
64084 KB |
Output is correct |
42 |
Correct |
10 ms |
64092 KB |
Output is correct |
43 |
Correct |
10 ms |
64188 KB |
Output is correct |
44 |
Correct |
11 ms |
64256 KB |
Output is correct |
45 |
Correct |
493 ms |
93552 KB |
Output is correct |
46 |
Correct |
568 ms |
93028 KB |
Output is correct |
47 |
Correct |
510 ms |
93380 KB |
Output is correct |
48 |
Correct |
472 ms |
93164 KB |
Output is correct |
49 |
Correct |
520 ms |
93288 KB |
Output is correct |
50 |
Correct |
498 ms |
92988 KB |
Output is correct |
51 |
Correct |
501 ms |
93080 KB |
Output is correct |
52 |
Correct |
549 ms |
93612 KB |
Output is correct |
53 |
Correct |
527 ms |
93264 KB |
Output is correct |
54 |
Correct |
457 ms |
103756 KB |
Output is correct |
55 |
Correct |
409 ms |
100880 KB |
Output is correct |
56 |
Correct |
384 ms |
98900 KB |
Output is correct |
57 |
Correct |
414 ms |
97728 KB |
Output is correct |
58 |
Correct |
185 ms |
101600 KB |
Output is correct |
59 |
Correct |
183 ms |
101564 KB |
Output is correct |
60 |
Correct |
119 ms |
109668 KB |
Output is correct |
61 |
Correct |
337 ms |
92744 KB |
Output is correct |
62 |
Correct |
455 ms |
105824 KB |
Output is correct |
63 |
Correct |
322 ms |
92500 KB |
Output is correct |
64 |
Correct |
363 ms |
92260 KB |
Output is correct |
65 |
Correct |
410 ms |
105812 KB |
Output is correct |
66 |
Correct |
312 ms |
92380 KB |
Output is correct |