#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define all(x) x.begin(),x.end()
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double db;
const ll maxn=2e5+69;
const ll offset=2e5;
const ll inf=1e18;
const int base=350;
const ll mod=1e9+7;
vector<int> L,adj[maxn];
int n,a[maxn],par[maxn],in[maxn],out[maxn],Time,up[maxn][20],dp[maxn];
vector<pii> qr[maxn];
struct fen
{
int st[maxn];
void Update1(int u,int w)
{
for(;u<=n+1;u+=u&-u) st[u]+=w;
}
void Update(int l,int r,int w)
{
Update1(r+1,-w);
Update1(l,w);
}
int Get(int u)
{
int r=0;
for(;u>0;u-=u&-u) r+=st[u];
return r;
}
}tree1,tree2;
void dfs1(int u)
{
in[u]=++Time;
for(int v:adj[u])
{
up[v][0]=u;
for1(i,1,18) up[v][i]=up[up[v][i-1]][i-1];
dfs1(v);
}
out[u]=Time;
}
void dfs(int u)
{
for(int v: adj[u])
{
dfs(v);
dp[u]+=dp[v];
}
tree1.Update(in[u],out[u],dp[u]);
for(auto v:qr[u])
{
// if (u==3) cerr<< v.fi<<" su "<<tree1.Get(in[u],out[u])<<'\n';
dp[u]=max(dp[u],v.se + tree1.Get(in[v.fi]) - tree2.Get(in[v.fi]));
}
// if (u==4) cerr<< dp[u]<<'\n';
tree2.Update(in[u],out[u],dp[u]);
}
void sol()
{
cin >> n;
for1(i,1,n) cin >> a[i];
a[0]=a[n+1]=n;
L.pb(0);
for1(i,1,n)
{
while (a[L.back()]<a[i])
{
if (a[par[L.back()]]>a[i]) par[L.back()]=i;
L.pop_back();
}
par[i]=L.back();
L.pb(i);
}
// for1(i,1,n) cerr<< par[i]<<'\n';
for1(i,1,n)
{
adj[par[i]].pb(i);
// cerr<<par[i]<<" -> "<<i<<'\n';
}
dfs1(0);
int q; cin >>q;
int rs=0;
for1(i,1,q)
{
int u,h,w;
cin >> u >>h>>w;
rs+=w;
int uu=u;
// cerr<< uu<<'\n';
for2(i,18,0) if (a[up[uu][i]] < h) uu=up[uu][i];
qr[uu].pb({u,w});
}
dfs(0);
cout << rs-dp[0];
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("990G.inp","r",stdin);
// freopen("990G.out","w",stdout);
int t=1;//cin >> t;
for1(i,1,t) {
// cout << "Case #"<<i<<": ";
sol();
}
}
/*
4 12
1 2 3
1 3
2 3
2 1
1 2
1 2
1 4
1 1
1 1
2 4
2 3
1 1
3 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21072 KB |
Output is correct |
2 |
Correct |
4 ms |
21072 KB |
Output is correct |
3 |
Correct |
4 ms |
21072 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21072 KB |
Output is correct |
6 |
Correct |
4 ms |
21072 KB |
Output is correct |
7 |
Correct |
4 ms |
21180 KB |
Output is correct |
8 |
Correct |
4 ms |
21244 KB |
Output is correct |
9 |
Correct |
4 ms |
21072 KB |
Output is correct |
10 |
Correct |
4 ms |
21208 KB |
Output is correct |
11 |
Correct |
4 ms |
21244 KB |
Output is correct |
12 |
Correct |
3 ms |
21180 KB |
Output is correct |
13 |
Correct |
4 ms |
21200 KB |
Output is correct |
14 |
Correct |
4 ms |
21072 KB |
Output is correct |
15 |
Correct |
4 ms |
21240 KB |
Output is correct |
16 |
Correct |
4 ms |
21072 KB |
Output is correct |
17 |
Correct |
4 ms |
21184 KB |
Output is correct |
18 |
Correct |
3 ms |
21072 KB |
Output is correct |
19 |
Correct |
4 ms |
21072 KB |
Output is correct |
20 |
Correct |
4 ms |
21072 KB |
Output is correct |
21 |
Correct |
4 ms |
21072 KB |
Output is correct |
22 |
Correct |
4 ms |
21072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21072 KB |
Output is correct |
2 |
Correct |
4 ms |
21072 KB |
Output is correct |
3 |
Correct |
4 ms |
21072 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21072 KB |
Output is correct |
6 |
Correct |
4 ms |
21072 KB |
Output is correct |
7 |
Correct |
4 ms |
21180 KB |
Output is correct |
8 |
Correct |
4 ms |
21244 KB |
Output is correct |
9 |
Correct |
4 ms |
21072 KB |
Output is correct |
10 |
Correct |
4 ms |
21208 KB |
Output is correct |
11 |
Correct |
4 ms |
21244 KB |
Output is correct |
12 |
Correct |
3 ms |
21180 KB |
Output is correct |
13 |
Correct |
4 ms |
21200 KB |
Output is correct |
14 |
Correct |
4 ms |
21072 KB |
Output is correct |
15 |
Correct |
4 ms |
21240 KB |
Output is correct |
16 |
Correct |
4 ms |
21072 KB |
Output is correct |
17 |
Correct |
4 ms |
21184 KB |
Output is correct |
18 |
Correct |
3 ms |
21072 KB |
Output is correct |
19 |
Correct |
4 ms |
21072 KB |
Output is correct |
20 |
Correct |
4 ms |
21072 KB |
Output is correct |
21 |
Correct |
4 ms |
21072 KB |
Output is correct |
22 |
Correct |
4 ms |
21072 KB |
Output is correct |
23 |
Correct |
6 ms |
21328 KB |
Output is correct |
24 |
Correct |
4 ms |
21168 KB |
Output is correct |
25 |
Correct |
5 ms |
21328 KB |
Output is correct |
26 |
Correct |
4 ms |
21156 KB |
Output is correct |
27 |
Correct |
4 ms |
21328 KB |
Output is correct |
28 |
Correct |
5 ms |
21316 KB |
Output is correct |
29 |
Correct |
5 ms |
21496 KB |
Output is correct |
30 |
Correct |
5 ms |
21328 KB |
Output is correct |
31 |
Correct |
4 ms |
21184 KB |
Output is correct |
32 |
Correct |
5 ms |
21328 KB |
Output is correct |
33 |
Correct |
4 ms |
21328 KB |
Output is correct |
34 |
Correct |
5 ms |
21184 KB |
Output is correct |
35 |
Correct |
5 ms |
21328 KB |
Output is correct |
36 |
Correct |
4 ms |
21444 KB |
Output is correct |
37 |
Correct |
4 ms |
21584 KB |
Output is correct |
38 |
Correct |
4 ms |
21328 KB |
Output is correct |
39 |
Correct |
4 ms |
21188 KB |
Output is correct |
40 |
Correct |
5 ms |
21328 KB |
Output is correct |
41 |
Correct |
5 ms |
21328 KB |
Output is correct |
42 |
Correct |
5 ms |
21340 KB |
Output is correct |
43 |
Correct |
4 ms |
21328 KB |
Output is correct |
44 |
Correct |
4 ms |
21328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21072 KB |
Output is correct |
2 |
Correct |
4 ms |
21072 KB |
Output is correct |
3 |
Correct |
4 ms |
21072 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21072 KB |
Output is correct |
6 |
Correct |
4 ms |
21072 KB |
Output is correct |
7 |
Correct |
4 ms |
21180 KB |
Output is correct |
8 |
Correct |
4 ms |
21244 KB |
Output is correct |
9 |
Correct |
4 ms |
21072 KB |
Output is correct |
10 |
Correct |
4 ms |
21208 KB |
Output is correct |
11 |
Correct |
4 ms |
21244 KB |
Output is correct |
12 |
Correct |
3 ms |
21180 KB |
Output is correct |
13 |
Correct |
4 ms |
21200 KB |
Output is correct |
14 |
Correct |
4 ms |
21072 KB |
Output is correct |
15 |
Correct |
4 ms |
21240 KB |
Output is correct |
16 |
Correct |
4 ms |
21072 KB |
Output is correct |
17 |
Correct |
4 ms |
21184 KB |
Output is correct |
18 |
Correct |
3 ms |
21072 KB |
Output is correct |
19 |
Correct |
4 ms |
21072 KB |
Output is correct |
20 |
Correct |
4 ms |
21072 KB |
Output is correct |
21 |
Correct |
4 ms |
21072 KB |
Output is correct |
22 |
Correct |
4 ms |
21072 KB |
Output is correct |
23 |
Correct |
6 ms |
21328 KB |
Output is correct |
24 |
Correct |
4 ms |
21168 KB |
Output is correct |
25 |
Correct |
5 ms |
21328 KB |
Output is correct |
26 |
Correct |
4 ms |
21156 KB |
Output is correct |
27 |
Correct |
4 ms |
21328 KB |
Output is correct |
28 |
Correct |
5 ms |
21316 KB |
Output is correct |
29 |
Correct |
5 ms |
21496 KB |
Output is correct |
30 |
Correct |
5 ms |
21328 KB |
Output is correct |
31 |
Correct |
4 ms |
21184 KB |
Output is correct |
32 |
Correct |
5 ms |
21328 KB |
Output is correct |
33 |
Correct |
4 ms |
21328 KB |
Output is correct |
34 |
Correct |
5 ms |
21184 KB |
Output is correct |
35 |
Correct |
5 ms |
21328 KB |
Output is correct |
36 |
Correct |
4 ms |
21444 KB |
Output is correct |
37 |
Correct |
4 ms |
21584 KB |
Output is correct |
38 |
Correct |
4 ms |
21328 KB |
Output is correct |
39 |
Correct |
4 ms |
21188 KB |
Output is correct |
40 |
Correct |
5 ms |
21328 KB |
Output is correct |
41 |
Correct |
5 ms |
21328 KB |
Output is correct |
42 |
Correct |
5 ms |
21340 KB |
Output is correct |
43 |
Correct |
4 ms |
21328 KB |
Output is correct |
44 |
Correct |
4 ms |
21328 KB |
Output is correct |
45 |
Correct |
164 ms |
66376 KB |
Output is correct |
46 |
Correct |
155 ms |
66120 KB |
Output is correct |
47 |
Correct |
172 ms |
65712 KB |
Output is correct |
48 |
Correct |
169 ms |
66376 KB |
Output is correct |
49 |
Correct |
157 ms |
65096 KB |
Output is correct |
50 |
Correct |
166 ms |
65492 KB |
Output is correct |
51 |
Correct |
173 ms |
65188 KB |
Output is correct |
52 |
Correct |
197 ms |
66376 KB |
Output is correct |
53 |
Correct |
174 ms |
66128 KB |
Output is correct |
54 |
Correct |
294 ms |
79556 KB |
Output is correct |
55 |
Correct |
276 ms |
75216 KB |
Output is correct |
56 |
Correct |
269 ms |
72648 KB |
Output is correct |
57 |
Correct |
253 ms |
71028 KB |
Output is correct |
58 |
Correct |
255 ms |
76228 KB |
Output is correct |
59 |
Correct |
247 ms |
75972 KB |
Output is correct |
60 |
Correct |
139 ms |
86176 KB |
Output is correct |
61 |
Correct |
166 ms |
65680 KB |
Output is correct |
62 |
Correct |
328 ms |
79716 KB |
Output is correct |
63 |
Correct |
185 ms |
64808 KB |
Output is correct |
64 |
Correct |
179 ms |
64196 KB |
Output is correct |
65 |
Correct |
327 ms |
80228 KB |
Output is correct |
66 |
Correct |
180 ms |
64320 KB |
Output is correct |