//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=2e5+5;
const int inf=1e9;
const ll inf2=1e18;
int a[mxn];
pair<int,int> table[mxn][18];
int up[mxn][18];
vector<pair<int,int>> st[mxn],en[mxn];
int l[mxn],r[mxn];
ll dp[mxn];
int ordl[mxn],ordr[mxn];
ll bit[mxn];
int cnt=0;
void update(int pos,ll val){
for(;pos<mxn;pos+=(pos&-pos)){
bit[pos]+=val;
}
}
ll query(int pos){
ll ans=0;
for(;pos>0;pos-=(pos&-pos)){
ans+=bit[pos];
}
return ans;
}
int get(int l,int r){
int b=__lg(r-l+1);
return max(table[l][b],table[r-(1<<b)+1][b]).s;
}
void go(int L,int R){
if(L==R) return;
int mid=get(L,R);
if(mid>L){
l[mid]=get(L,mid-1);
up[l[mid]][0]=mid;
go(L,mid-1);
}
if(mid<R){
r[mid]=get(mid+1,R);
up[r[mid]][0]=mid;
go(mid+1,R);
}
}
void dfs(int v){
//cout<<v<<' '<<l[v]<<' '<<r[v]<<'\n';
ordl[v]=++cnt;
if(l[v]!=0){
dfs(l[v]);
}
if(r[v]!=0){
dfs(r[v]);
}
ordr[v]=cnt;
if(l[v]!=0){
update(ordl[v],dp[l[v]]);
update(ordr[v]+1,-dp[l[v]]);
update(ordl[l[v]],-dp[l[v]]);
update(ordr[l[v]]+1,dp[l[v]]);
}
if(r[v]!=0){
update(ordl[v],dp[r[v]]);
update(ordr[v]+1,-dp[r[v]]);
update(ordl[r[v]],-dp[r[v]]);
update(ordr[r[v]]+1,dp[r[v]]);
}
dp[v]=dp[l[v]]+dp[r[v]];
for(auto u:en[v]){
int x=u.f;
dp[v]=max(dp[v],query(ordl[x])+u.s);
}
}
signed main() {_
int n;
cin>>n;
a[0]=inf;
//dp[0]=-inf2;
for(int i=1;i<=n;i++){
cin>>a[i];
table[i][0]={a[i],i};
}
for(int j=1;j<18;j++){
for(int i=1;i+(1<<j)-1<=n;i++){
table[i][j]=max(table[i][j-1],table[i+(1<<(j-1))][j-1]);
}
}
int root=get(1,n);
go(1,n);
for(int j=1;j<18;j++){
for(int i=1;i<=n;i++){
up[i][j]=up[up[i][j-1]][j-1];
}
}
int m;
cin>>m;
ll sum=0;
for(int i=1;i<=m;i++){
int x,y,c;
cin>>x>>y>>c;
sum+=c;
int node=x;
for(int j=17;j>=0;j--){
int v=up[node][j];
if(a[v]<y) node=v;
}
//cout<<st<<'\n';
en[node].push_back({x,c});
st[x].push_back({node,c});
}
dfs(root);
cout<<sum-dp[root]<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message
constellation3.cpp: In function 'void setIO(std::string)':
constellation3.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
4 ms |
18944 KB |
Output is correct |
5 |
Correct |
4 ms |
19044 KB |
Output is correct |
6 |
Correct |
3 ms |
18780 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18780 KB |
Output is correct |
11 |
Correct |
4 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18980 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18936 KB |
Output is correct |
15 |
Correct |
4 ms |
18776 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
4 ms |
18952 KB |
Output is correct |
19 |
Correct |
4 ms |
18780 KB |
Output is correct |
20 |
Correct |
4 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
18884 KB |
Output is correct |
22 |
Correct |
3 ms |
18780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
4 ms |
18944 KB |
Output is correct |
5 |
Correct |
4 ms |
19044 KB |
Output is correct |
6 |
Correct |
3 ms |
18780 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18780 KB |
Output is correct |
11 |
Correct |
4 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18980 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18936 KB |
Output is correct |
15 |
Correct |
4 ms |
18776 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
4 ms |
18952 KB |
Output is correct |
19 |
Correct |
4 ms |
18780 KB |
Output is correct |
20 |
Correct |
4 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
18884 KB |
Output is correct |
22 |
Correct |
3 ms |
18780 KB |
Output is correct |
23 |
Correct |
8 ms |
19036 KB |
Output is correct |
24 |
Correct |
5 ms |
19016 KB |
Output is correct |
25 |
Correct |
5 ms |
19036 KB |
Output is correct |
26 |
Correct |
5 ms |
19036 KB |
Output is correct |
27 |
Correct |
5 ms |
19036 KB |
Output is correct |
28 |
Correct |
5 ms |
19036 KB |
Output is correct |
29 |
Correct |
5 ms |
19036 KB |
Output is correct |
30 |
Correct |
5 ms |
19048 KB |
Output is correct |
31 |
Correct |
5 ms |
19036 KB |
Output is correct |
32 |
Correct |
5 ms |
19104 KB |
Output is correct |
33 |
Correct |
5 ms |
19036 KB |
Output is correct |
34 |
Correct |
6 ms |
19036 KB |
Output is correct |
35 |
Correct |
5 ms |
19032 KB |
Output is correct |
36 |
Correct |
4 ms |
19036 KB |
Output is correct |
37 |
Correct |
5 ms |
19036 KB |
Output is correct |
38 |
Correct |
4 ms |
19032 KB |
Output is correct |
39 |
Correct |
6 ms |
19036 KB |
Output is correct |
40 |
Correct |
5 ms |
19036 KB |
Output is correct |
41 |
Correct |
4 ms |
19036 KB |
Output is correct |
42 |
Correct |
5 ms |
19036 KB |
Output is correct |
43 |
Correct |
5 ms |
18932 KB |
Output is correct |
44 |
Correct |
5 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
4 ms |
18944 KB |
Output is correct |
5 |
Correct |
4 ms |
19044 KB |
Output is correct |
6 |
Correct |
3 ms |
18780 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18780 KB |
Output is correct |
11 |
Correct |
4 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18980 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18936 KB |
Output is correct |
15 |
Correct |
4 ms |
18776 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
4 ms |
18952 KB |
Output is correct |
19 |
Correct |
4 ms |
18780 KB |
Output is correct |
20 |
Correct |
4 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
18884 KB |
Output is correct |
22 |
Correct |
3 ms |
18780 KB |
Output is correct |
23 |
Correct |
8 ms |
19036 KB |
Output is correct |
24 |
Correct |
5 ms |
19016 KB |
Output is correct |
25 |
Correct |
5 ms |
19036 KB |
Output is correct |
26 |
Correct |
5 ms |
19036 KB |
Output is correct |
27 |
Correct |
5 ms |
19036 KB |
Output is correct |
28 |
Correct |
5 ms |
19036 KB |
Output is correct |
29 |
Correct |
5 ms |
19036 KB |
Output is correct |
30 |
Correct |
5 ms |
19048 KB |
Output is correct |
31 |
Correct |
5 ms |
19036 KB |
Output is correct |
32 |
Correct |
5 ms |
19104 KB |
Output is correct |
33 |
Correct |
5 ms |
19036 KB |
Output is correct |
34 |
Correct |
6 ms |
19036 KB |
Output is correct |
35 |
Correct |
5 ms |
19032 KB |
Output is correct |
36 |
Correct |
4 ms |
19036 KB |
Output is correct |
37 |
Correct |
5 ms |
19036 KB |
Output is correct |
38 |
Correct |
4 ms |
19032 KB |
Output is correct |
39 |
Correct |
6 ms |
19036 KB |
Output is correct |
40 |
Correct |
5 ms |
19036 KB |
Output is correct |
41 |
Correct |
4 ms |
19036 KB |
Output is correct |
42 |
Correct |
5 ms |
19036 KB |
Output is correct |
43 |
Correct |
5 ms |
18932 KB |
Output is correct |
44 |
Correct |
5 ms |
19036 KB |
Output is correct |
45 |
Correct |
267 ms |
66668 KB |
Output is correct |
46 |
Correct |
266 ms |
66280 KB |
Output is correct |
47 |
Correct |
228 ms |
66536 KB |
Output is correct |
48 |
Correct |
220 ms |
66596 KB |
Output is correct |
49 |
Correct |
253 ms |
66372 KB |
Output is correct |
50 |
Correct |
222 ms |
66328 KB |
Output is correct |
51 |
Correct |
241 ms |
66424 KB |
Output is correct |
52 |
Correct |
227 ms |
66388 KB |
Output is correct |
53 |
Correct |
228 ms |
66320 KB |
Output is correct |
54 |
Correct |
306 ms |
72608 KB |
Output is correct |
55 |
Correct |
301 ms |
70480 KB |
Output is correct |
56 |
Correct |
287 ms |
69412 KB |
Output is correct |
57 |
Correct |
274 ms |
68692 KB |
Output is correct |
58 |
Correct |
212 ms |
69288 KB |
Output is correct |
59 |
Correct |
222 ms |
69480 KB |
Output is correct |
60 |
Correct |
157 ms |
74756 KB |
Output is correct |
61 |
Correct |
204 ms |
65872 KB |
Output is correct |
62 |
Correct |
285 ms |
73208 KB |
Output is correct |
63 |
Correct |
202 ms |
65484 KB |
Output is correct |
64 |
Correct |
214 ms |
65536 KB |
Output is correct |
65 |
Correct |
282 ms |
73040 KB |
Output is correct |
66 |
Correct |
228 ms |
65416 KB |
Output is correct |