#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define N 200005
#define INF 0x3f3f3f3f
#define MOD 1000000007LL
int a[N];
int l[N], r[N];
vector<pair<int, int> > st[N], tmp;
map<int, ll> ans[N];
ll c[N];
ll res = 0;
void insert(int v, int y, ll s){
if((--ans[v].upper_bound(y)) -> ss > s) return;
ans[v][y] = s;
auto it = ans[v].upper_bound(y);
while(it != ans[v].end() && it -> ss <= s){
it = ans[v].erase(it);
}
}
void dfs(int v){
int l = ::l[v];
int r = ::r[v];
if(l)
dfs(l);
if(r)
dfs(r);
ll L, R;
L = (--ans[l].upper_bound(a[v])) -> ss;
R = (--ans[r].upper_bound(a[v])) -> ss;
c[l] += R;
c[r] += L;
if(ans[l].size() > ans[r].size()){
swap(ans[l], ans[r]);
swap(c[l], c[r]);
}
swap(ans[r], ans[v]);
swap(c[r], c[v]);
for(auto& x : ans[l])
insert(v, x.ff, x.ss - c[v] + c[l]);
for(auto& x : st[v])
insert(v, x.ff, x.ss + L + R - c[v]);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
ans[0][1] = 0;
for(int i = 1; i <= n; ++i){
ans[i][1] = 0;
cin>>a[i];
while(!tmp.empty() && tmp.back().ff < a[i]){
l[i] = tmp.back().ss;
tmp.pop_back();
}
if(!tmp.empty())
r[tmp.back().ss] = i;
tmp.push_back({a[i], i});
}
int q;
cin>>q;
while(q--){
int x, y, s;
cin>>x>>y>>s;
res += s;
st[x].push_back({y, s});
}
dfs(tmp[0].ss);
res -= c[tmp[0].ss] + (--ans[tmp[0].ss].end()) -> ss;
cout<<res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
14464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
14464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
14464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |