//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#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][20];
int up[mxn][20];
vector<pair<int,int>> st[mxn],en[mxn];
int l[mxn],r[mxn];
ll dp[mxn];
int ordl[mxn],ordr[mxn];
ll dp2[mxn];
ll bit[mxn];
int cnt=0;
void update(int pos,int val){
for(;pos<mxn;pos+=(pos&-pos)){
bit[pos]+=val;
}
}
ll query(int pos){
int 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<20;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<20;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=19;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: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 + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
25076 KB |
Output is correct |
2 |
Correct |
4 ms |
24924 KB |
Output is correct |
3 |
Correct |
4 ms |
24920 KB |
Output is correct |
4 |
Correct |
4 ms |
24924 KB |
Output is correct |
5 |
Correct |
4 ms |
24924 KB |
Output is correct |
6 |
Correct |
4 ms |
24924 KB |
Output is correct |
7 |
Correct |
4 ms |
24924 KB |
Output is correct |
8 |
Correct |
4 ms |
24920 KB |
Output is correct |
9 |
Correct |
4 ms |
24924 KB |
Output is correct |
10 |
Correct |
5 ms |
24924 KB |
Output is correct |
11 |
Correct |
4 ms |
24924 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
4 ms |
24920 KB |
Output is correct |
14 |
Correct |
4 ms |
24924 KB |
Output is correct |
15 |
Correct |
4 ms |
25100 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
24920 KB |
Output is correct |
18 |
Correct |
5 ms |
24920 KB |
Output is correct |
19 |
Correct |
4 ms |
24924 KB |
Output is correct |
20 |
Correct |
5 ms |
24924 KB |
Output is correct |
21 |
Correct |
4 ms |
24924 KB |
Output is correct |
22 |
Correct |
4 ms |
24924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
25076 KB |
Output is correct |
2 |
Correct |
4 ms |
24924 KB |
Output is correct |
3 |
Correct |
4 ms |
24920 KB |
Output is correct |
4 |
Correct |
4 ms |
24924 KB |
Output is correct |
5 |
Correct |
4 ms |
24924 KB |
Output is correct |
6 |
Correct |
4 ms |
24924 KB |
Output is correct |
7 |
Correct |
4 ms |
24924 KB |
Output is correct |
8 |
Correct |
4 ms |
24920 KB |
Output is correct |
9 |
Correct |
4 ms |
24924 KB |
Output is correct |
10 |
Correct |
5 ms |
24924 KB |
Output is correct |
11 |
Correct |
4 ms |
24924 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
4 ms |
24920 KB |
Output is correct |
14 |
Correct |
4 ms |
24924 KB |
Output is correct |
15 |
Correct |
4 ms |
25100 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
24920 KB |
Output is correct |
18 |
Correct |
5 ms |
24920 KB |
Output is correct |
19 |
Correct |
4 ms |
24924 KB |
Output is correct |
20 |
Correct |
5 ms |
24924 KB |
Output is correct |
21 |
Correct |
4 ms |
24924 KB |
Output is correct |
22 |
Correct |
4 ms |
24924 KB |
Output is correct |
23 |
Correct |
5 ms |
25180 KB |
Output is correct |
24 |
Correct |
6 ms |
25072 KB |
Output is correct |
25 |
Correct |
6 ms |
25120 KB |
Output is correct |
26 |
Correct |
5 ms |
25140 KB |
Output is correct |
27 |
Correct |
6 ms |
25180 KB |
Output is correct |
28 |
Correct |
5 ms |
25180 KB |
Output is correct |
29 |
Correct |
6 ms |
25224 KB |
Output is correct |
30 |
Correct |
6 ms |
25180 KB |
Output is correct |
31 |
Correct |
6 ms |
25180 KB |
Output is correct |
32 |
Correct |
6 ms |
25180 KB |
Output is correct |
33 |
Correct |
5 ms |
25180 KB |
Output is correct |
34 |
Correct |
5 ms |
25236 KB |
Output is correct |
35 |
Correct |
5 ms |
25180 KB |
Output is correct |
36 |
Correct |
6 ms |
25180 KB |
Output is correct |
37 |
Correct |
5 ms |
25180 KB |
Output is correct |
38 |
Correct |
5 ms |
23132 KB |
Output is correct |
39 |
Correct |
6 ms |
25180 KB |
Output is correct |
40 |
Correct |
7 ms |
25068 KB |
Output is correct |
41 |
Correct |
5 ms |
25180 KB |
Output is correct |
42 |
Correct |
5 ms |
25180 KB |
Output is correct |
43 |
Correct |
5 ms |
25180 KB |
Output is correct |
44 |
Correct |
6 ms |
25072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
25076 KB |
Output is correct |
2 |
Correct |
4 ms |
24924 KB |
Output is correct |
3 |
Correct |
4 ms |
24920 KB |
Output is correct |
4 |
Correct |
4 ms |
24924 KB |
Output is correct |
5 |
Correct |
4 ms |
24924 KB |
Output is correct |
6 |
Correct |
4 ms |
24924 KB |
Output is correct |
7 |
Correct |
4 ms |
24924 KB |
Output is correct |
8 |
Correct |
4 ms |
24920 KB |
Output is correct |
9 |
Correct |
4 ms |
24924 KB |
Output is correct |
10 |
Correct |
5 ms |
24924 KB |
Output is correct |
11 |
Correct |
4 ms |
24924 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
4 ms |
24920 KB |
Output is correct |
14 |
Correct |
4 ms |
24924 KB |
Output is correct |
15 |
Correct |
4 ms |
25100 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
24920 KB |
Output is correct |
18 |
Correct |
5 ms |
24920 KB |
Output is correct |
19 |
Correct |
4 ms |
24924 KB |
Output is correct |
20 |
Correct |
5 ms |
24924 KB |
Output is correct |
21 |
Correct |
4 ms |
24924 KB |
Output is correct |
22 |
Correct |
4 ms |
24924 KB |
Output is correct |
23 |
Correct |
5 ms |
25180 KB |
Output is correct |
24 |
Correct |
6 ms |
25072 KB |
Output is correct |
25 |
Correct |
6 ms |
25120 KB |
Output is correct |
26 |
Correct |
5 ms |
25140 KB |
Output is correct |
27 |
Correct |
6 ms |
25180 KB |
Output is correct |
28 |
Correct |
5 ms |
25180 KB |
Output is correct |
29 |
Correct |
6 ms |
25224 KB |
Output is correct |
30 |
Correct |
6 ms |
25180 KB |
Output is correct |
31 |
Correct |
6 ms |
25180 KB |
Output is correct |
32 |
Correct |
6 ms |
25180 KB |
Output is correct |
33 |
Correct |
5 ms |
25180 KB |
Output is correct |
34 |
Correct |
5 ms |
25236 KB |
Output is correct |
35 |
Correct |
5 ms |
25180 KB |
Output is correct |
36 |
Correct |
6 ms |
25180 KB |
Output is correct |
37 |
Correct |
5 ms |
25180 KB |
Output is correct |
38 |
Correct |
5 ms |
23132 KB |
Output is correct |
39 |
Correct |
6 ms |
25180 KB |
Output is correct |
40 |
Correct |
7 ms |
25068 KB |
Output is correct |
41 |
Correct |
5 ms |
25180 KB |
Output is correct |
42 |
Correct |
5 ms |
25180 KB |
Output is correct |
43 |
Correct |
5 ms |
25180 KB |
Output is correct |
44 |
Correct |
6 ms |
25072 KB |
Output is correct |
45 |
Correct |
307 ms |
132056 KB |
Output is correct |
46 |
Correct |
285 ms |
131592 KB |
Output is correct |
47 |
Correct |
283 ms |
132176 KB |
Output is correct |
48 |
Correct |
274 ms |
131576 KB |
Output is correct |
49 |
Correct |
264 ms |
131668 KB |
Output is correct |
50 |
Correct |
291 ms |
131668 KB |
Output is correct |
51 |
Correct |
259 ms |
131824 KB |
Output is correct |
52 |
Correct |
303 ms |
132516 KB |
Output is correct |
53 |
Correct |
261 ms |
131664 KB |
Output is correct |
54 |
Correct |
465 ms |
138560 KB |
Output is correct |
55 |
Correct |
454 ms |
136396 KB |
Output is correct |
56 |
Correct |
466 ms |
135248 KB |
Output is correct |
57 |
Correct |
407 ms |
134780 KB |
Output is correct |
58 |
Correct |
319 ms |
135276 KB |
Output is correct |
59 |
Correct |
366 ms |
135456 KB |
Output is correct |
60 |
Correct |
197 ms |
138236 KB |
Output is correct |
61 |
Correct |
261 ms |
131032 KB |
Output is correct |
62 |
Correct |
396 ms |
139088 KB |
Output is correct |
63 |
Correct |
266 ms |
131012 KB |
Output is correct |
64 |
Correct |
279 ms |
130480 KB |
Output is correct |
65 |
Correct |
433 ms |
138512 KB |
Output is correct |
66 |
Correct |
243 ms |
130820 KB |
Output is correct |