#include <bits/stdc++.h>
#define ld long double
#define ll long long
#define pb push_back
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
using namespace std;
ll n,m;
vector<ll> bX;
vector<vector<ll>>bY;
vector<vector<pair<ll,ll>>> sX,sY;
vector<pair<ll,pair<ll,ll>>> dsu;
vector<ll> sgt,lazy;
ll getp(ll x)
{
if(x==dsu[x].x) return x;
return dsu[x].x=getp(dsu[x].x);
}
void connect(ll u, ll v)
{
u=getp(u), v=getp(v);
if(dsu[u].y.y-dsu[u].y.x<dsu[v].y.y-dsu[v].y.x) swap(u,v);
dsu[v].x=u;
dsu[u].y.x=min(dsu[u].y.x,dsu[v].y.x);
dsu[u].y.y=max(dsu[u].y.y,dsu[v].y.y);
}
void upd(ll a, ll b, ll val, ll p=1, ll l=0, ll r=n-1)
{
if(lazy[p])
{
sgt[p]+=lazy[p];
if(l!=r)
{
lazy[p*2]+=lazy[p];
lazy[p*2+1]+=lazy[p];
}
lazy[p]=0;
}
if(l>=a && r<=b)
{
sgt[p]+=val;
if(l!=r)
{
lazy[p*2]+=val;
lazy[p*2+1]+=val;
}
return;
}
ll md=(l+r)/2;
if(l<=b && md>=a) upd(a,b,val,p*2,l,md);
if(md+1<=b && r>=a) upd(a,b,val,p*2+1,md+1,r);
sgt[p]=max(sgt[p*2],sgt[p*2+1]);
}
ll getmx(ll a, ll b, ll p=1, ll l=0, ll r=n-1)
{
if(lazy[p])
{
sgt[p]+=lazy[p];
if(l!=r)
{
lazy[p*2]+=lazy[p];
lazy[p*2+1]+=lazy[p];
}
lazy[p]=0;
}
if(l>=a && r<=b) return sgt[p];
ll md=(l+r)/2;
ll ret=0;
if(l<=b && md>=a) ret=getmx(a,b,p*2,l,md);
if(md+1<=b && r>=a) ret=max(ret,getmx(a,b,p*2+1,md+1,r));
return ret;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
cin>>n;
bY.resize(n+1), sX.resize(n), sY.resize(n+1), sgt.resize(4*n), lazy.resize(4*n);
for(ll i=0;i<n;i++) dsu.pb({i,{i,i}});
for(ll i=0;i<n;i++)
{
ll Ai; cin>>Ai;
Ai--;
bX.pb(Ai);
bY[Ai].pb(i);
}
ll ans=0;
cin>>m;
for(ll i=0;i<m;i++)
{
ll x,y; ll c; cin>>x>>y>>c;
x--,y--;
sX[x].pb({y,c});
ans+=c;
}
for(ll i=0;i<n;i++)
{
sort(all(sX[i]));
ll prev=0;
for(auto p:sX[i])
{
if(p.y<=prev) continue;
sY[p.x].pb({i,p.y-prev});
prev=p.y;
}
}
vector<bool> started(n,0);
for(ll i=0;i<=n;i++)
{
for(auto p:sY[i])
upd(p.x,p.x,p.y);
for(ll bld:bY[i])
{
ll lV=0, rV=0;
if(bld>0) if(started[bld-1])
{
ll par=getp(bld-1);
ll L=dsu[par].y.x, R=dsu[par].y.y;
lV=getmx(L,R);
}
if(bld<n-1) if(started[bld+1])
{
ll par=getp(bld+1);
ll L=dsu[par].y.x, R=dsu[par].y.y;
rV=getmx(L,R);
upd(L,R,lV);
}
if(bld>0) if(started[bld-1])
{
ll par=getp(bld-1);
ll L=dsu[par].y.x, R=dsu[par].y.y;
upd(L,R,rV);
}
if(bld>0) if(started[bld-1]) connect(bld,bld-1);
if(bld<n-1) if(started[bld+1]) connect(bld,bld+1);
started[bld]=1;
upd(bld,bld,lV+rV);
}
}
cout<<ans-sgt[1]<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
7 ms |
768 KB |
Output is correct |
24 |
Correct |
7 ms |
768 KB |
Output is correct |
25 |
Correct |
7 ms |
768 KB |
Output is correct |
26 |
Correct |
7 ms |
768 KB |
Output is correct |
27 |
Correct |
7 ms |
768 KB |
Output is correct |
28 |
Correct |
7 ms |
816 KB |
Output is correct |
29 |
Correct |
7 ms |
768 KB |
Output is correct |
30 |
Correct |
7 ms |
768 KB |
Output is correct |
31 |
Correct |
7 ms |
768 KB |
Output is correct |
32 |
Correct |
7 ms |
768 KB |
Output is correct |
33 |
Correct |
7 ms |
896 KB |
Output is correct |
34 |
Correct |
7 ms |
768 KB |
Output is correct |
35 |
Correct |
7 ms |
768 KB |
Output is correct |
36 |
Correct |
7 ms |
768 KB |
Output is correct |
37 |
Correct |
7 ms |
768 KB |
Output is correct |
38 |
Correct |
7 ms |
896 KB |
Output is correct |
39 |
Correct |
7 ms |
768 KB |
Output is correct |
40 |
Correct |
9 ms |
768 KB |
Output is correct |
41 |
Correct |
7 ms |
768 KB |
Output is correct |
42 |
Correct |
7 ms |
768 KB |
Output is correct |
43 |
Correct |
8 ms |
768 KB |
Output is correct |
44 |
Correct |
8 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
7 ms |
768 KB |
Output is correct |
24 |
Correct |
7 ms |
768 KB |
Output is correct |
25 |
Correct |
7 ms |
768 KB |
Output is correct |
26 |
Correct |
7 ms |
768 KB |
Output is correct |
27 |
Correct |
7 ms |
768 KB |
Output is correct |
28 |
Correct |
7 ms |
816 KB |
Output is correct |
29 |
Correct |
7 ms |
768 KB |
Output is correct |
30 |
Correct |
7 ms |
768 KB |
Output is correct |
31 |
Correct |
7 ms |
768 KB |
Output is correct |
32 |
Correct |
7 ms |
768 KB |
Output is correct |
33 |
Correct |
7 ms |
896 KB |
Output is correct |
34 |
Correct |
7 ms |
768 KB |
Output is correct |
35 |
Correct |
7 ms |
768 KB |
Output is correct |
36 |
Correct |
7 ms |
768 KB |
Output is correct |
37 |
Correct |
7 ms |
768 KB |
Output is correct |
38 |
Correct |
7 ms |
896 KB |
Output is correct |
39 |
Correct |
7 ms |
768 KB |
Output is correct |
40 |
Correct |
9 ms |
768 KB |
Output is correct |
41 |
Correct |
7 ms |
768 KB |
Output is correct |
42 |
Correct |
7 ms |
768 KB |
Output is correct |
43 |
Correct |
8 ms |
768 KB |
Output is correct |
44 |
Correct |
8 ms |
768 KB |
Output is correct |
45 |
Correct |
520 ms |
46652 KB |
Output is correct |
46 |
Correct |
515 ms |
46048 KB |
Output is correct |
47 |
Correct |
498 ms |
45532 KB |
Output is correct |
48 |
Correct |
509 ms |
46532 KB |
Output is correct |
49 |
Correct |
503 ms |
45148 KB |
Output is correct |
50 |
Correct |
484 ms |
45152 KB |
Output is correct |
51 |
Correct |
531 ms |
45532 KB |
Output is correct |
52 |
Correct |
523 ms |
46304 KB |
Output is correct |
53 |
Correct |
514 ms |
46044 KB |
Output is correct |
54 |
Correct |
458 ms |
48900 KB |
Output is correct |
55 |
Correct |
420 ms |
47964 KB |
Output is correct |
56 |
Correct |
424 ms |
47452 KB |
Output is correct |
57 |
Correct |
404 ms |
46940 KB |
Output is correct |
58 |
Correct |
399 ms |
43100 KB |
Output is correct |
59 |
Correct |
382 ms |
42972 KB |
Output is correct |
60 |
Correct |
301 ms |
49116 KB |
Output is correct |
61 |
Correct |
346 ms |
44892 KB |
Output is correct |
62 |
Correct |
450 ms |
46432 KB |
Output is correct |
63 |
Correct |
331 ms |
45148 KB |
Output is correct |
64 |
Correct |
363 ms |
43868 KB |
Output is correct |
65 |
Correct |
443 ms |
46556 KB |
Output is correct |
66 |
Correct |
344 ms |
44124 KB |
Output is correct |