This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
if(b<0) return 0;
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
ll n, m, sum, ans, tim, dp[N], s[N], a[N], tin[N], tout[N], fen[N];
ll mx[lg][N], h[N];
vector<int> adj[N];
vector<pll> vec[N];
void update(ll i, ll val) {
while(i < N+10) {
fen[i] += val; i += (i&(-i));
}
}
ll query(ll i) {
ll res = 0;
while(i > 0) {
res += fen[i]; i -= (i&(-i));
}
return res;
}
int rot=0;
vector<int> st;
void init(int v) {
tin[v] = ++tim;
h[v] = h[mx[0][v]] + 1;
for(int i=1; i<lg; i++) mx[i][v] = mx[i-1][mx[i-1][v]];
for(auto u : adj[v]) {
init(u);
}
tout[v] = tim+1;
}
ll smc[N];
void dfs(int v) {
for(int u : adj[v]) {
dfs(u);
smc[v] += dp[u];
}
for(int i=0; size(adj[v])==2&&i<2; i++) {
update(tin[adj[v][i]], dp[adj[v][1-i]]);
update(tout[adj[v][i]], -dp[adj[v][1-i]]);
}
dp[v] = smc[v];
for(auto it : vec[v]) {
dp[v] = max(dp[v], smc[it.F] + query(tin[it.F]) + it.S);
}
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n;
for(int i=1; i<=n; i++) {
cin>>a[i];
int lst = 0;
while(size(st) > 0 && a[st.back()] < a[i]) {
lst = st.back();
st.pop_back();
}
if(size(st) > 0) mx[0][i] = st.back();
else mx[0][i] = i, rot = i;
if(lst > 0) mx[0][lst] = i;
st.pb(i);
}
for(int i=1; i<=n; i++) {
if(mx[0][i] != i) adj[mx[0][i]].pb(i);
}
init(rot);
cin>>m;
for(ll c,x,y,i=1; i<=m; i++) {
cin>>x>>y>>c; sum+=c;
int id = x;
for(int j=lg-1; j>=0; j--) {
if(a[mx[j][id]] < y) id = mx[j][id];
}
vec[id].pb({x, c});
}
dfs(rot);
cout<<sum-dp[rot]<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |