This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#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 (stderr)
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |