#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const int maxn=(4e5)+10;
const int INF=(1e6);
int n,m,tot,a[maxn],rt,fa[maxn];
int L[maxn],R[maxn],p[maxn],back[maxn];
ll f[maxn],g[maxn],tmp;
vector<int> son[maxn];
struct star { int x,y,c; } d[maxn];
map<pair<int,int>,int> dy;
vector<int> G[maxn]; // stars in this region
ll sum[maxn];
int dfn[maxn],Son[maxn],sz[maxn],cnt,top[maxn];
namespace Seg1 {
int st[maxn][20],lg[maxn],pos[maxn][20],mn[maxn][20]; // mx -> first in the left & right >= me
int Querymx(int l,int r) {
int j=lg[r-l+1];
int x=st[l][j],y=st[r-(1<<j)+1][j];
return max(x,y);
}
int querymx(int l,int r) {
int j=lg[r-l+1];
int x=pos[l][j],y=pos[r-(1<<j)+1][j];
if (a[x]>=a[y]) return x;
return y;
}
int Querymn(int l,int r) {
int j=lg[r-l+1];
int x=mn[l][j],y=mn[r-(1<<j)+1][j];
return min(x,y);
}
void init() {
int x,y;
for (int i=0;i<=n+1;i++) st[i][0]=mn[i][0]=a[i],pos[i][0]=i;
for (int i=2;i<=n+2;i++) lg[i]=lg[i/2]+1;
for (int j=1;j<=19;j++)
for (int i=0;i+(1<<j)-1<=n+1;i++) {
x=st[i][j-1],y=st[i+(1<<(j-1))][j-1];
st[i][j]=max(x,y);
x=pos[i][j-1],y=pos[i+(1<<(j-1))][j-1];
if (a[x]>=a[y]) pos[i][j]=x;
else pos[i][j]=y;
x=mn[i][j-1],y=mn[i+(1<<(j-1))][j-1];
mn[i][j]=min(x,y);
}
}
void solve() {
int l,r,mid,res,x,y;
for (int i=1;i<=m;i++) {
l=0,r=d[i].x-1; res=0;
while (l<=r) {
mid=(l+r)/2;
if (Querymx(mid,d[i].x-1)>=d[i].y) l=mid+1,res=mid;
else r=mid-1;
}
x=res+1;
l=d[i].x+1,r=n+1; res=0;
while (l<=r) {
mid=(l+r)/2;
if (Querymx(d[i].x+1,mid)>=d[i].y) r=mid-1,res=mid;
else l=mid+1;
}
y=res-1;
//printf("find id=%d l=%d r=%d\n",i,x,y);
x=dy[make_pair(x,y)];
sum[x]+=d[i].c;
G[x].push_back(i);
}
}
};
namespace Seg2 {
int tr[maxn*4],lazy[maxn*4]; // mx -> belong to the deepest node
void pushdown(int root) {
if (!lazy[root]) return;
tr[root*2]=max(tr[root*2],lazy[root]);
tr[root*2+1]=max(tr[root*2+1],lazy[root]);
lazy[root*2]=max(lazy[root*2],lazy[root]);
lazy[root*2+1]=max(lazy[root*2+1],lazy[root]);
lazy[root]=0;
}
void add(int L,int R,int l,int r,int root,int delta) {
if (L<=l&&r<=R) {
tr[root]=max(tr[root],delta);
lazy[root]=max(lazy[root],delta);
return;
}
pushdown(root);
int mid=(l+r)/2;
if (L<=mid) add(L,R,l,mid,root*2,delta);
if (mid<R) add(L,R,mid+1,r,root*2+1,delta);
tr[root]=max(tr[root*2],tr[root*2+1]);
}
int query(int x,int l,int r,int root) {
if (l==r) return tr[root];
pushdown(root);
int mid=(l+r)/2;
if (x<=mid) return query(x,l,mid,root*2);
return query(x,mid+1,r,root*2+1);
}
};
int build(int l,int r) {
tot++;
dy[make_pair(l,r)]=tot;
L[tot]=l,R[tot]=r;
int u=tot,mx=Seg1::Querymx(l,r),mn=Seg1::Querymn(l,r);
if (mn==mx) return u;
int x=Seg1::querymx(l,r),y;
if (l<=x-1) son[u].push_back(build(l,x-1));
while (x<=r) {
y=Seg1::querymx(x+1,r);
if (a[y]==mx) {
if (x+1<=y-1) son[u].push_back(build(x+1,y-1));
x=y;
} else {
if (x+1<=r) son[u].push_back(build(x+1,r));
break;
}
}
return u;
}
void dfs(int u) {
sz[u]=1;
for (int i=0,v;i<son[u].size();i++) {
v=son[u][i];
dfs(v);
fa[v]=u;
sz[u]+=sz[v];
if (sz[v]>sz[Son[u]]) Son[u]=v;
}
}
void dfs(int u,int t) {
dfn[u]=++cnt; back[cnt]=u;
top[u]=t;
Seg2::add(L[u],R[u],1,n,1,dfn[u]);
if (Son[u]) dfs(Son[u],t);
for (int i=0,v;i<son[u].size();i++) {
v=son[u][i];
if (v==Son[u]) continue;
dfs(v,v);
}
}
namespace Seg3 {
ll tr[maxn]; // sum -> DP
void add(int x,ll delta) {
x=dfn[x];
for (;x<=tot;x+=x&(-x)) tr[x]+=delta;
}
ll query(int x) {
ll res=0;
for (;x;x-=x&(-x)) res+=tr[x];
return res;
}
ll Query(int l,int r) {
return query(r)-query(l-1);
}
ll query(int x,int y) {
if (x==y) return 0;
ll res=0;
while (top[x]!=top[y]) {
res+=Query(dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
if (x==y) return res;
return res+Query(dfn[y]+1,dfn[x]);
}
};
void solve(int u) {
f[u]=sum[u];
for (int i=0,v;i<son[u].size();i++) {
v=son[u][i];
solve(v);
f[u]+=f[v];
}
ll first=f[u];
for (int i=0;i<G[u].size();i++) {
tmp=first-d[G[u][i]].c+Seg3::query(p[d[G[u][i]].x],u);
f[u]=min(f[u],tmp);
}
g[u]=first-f[u];
// printf("dp[%d]: %lld %lld %lld\n",u,first,f[u],g[u]);
Seg3::add(u,g[u]);
}
int main() {
//freopen("1.txt","r",stdin);
int x,y;
read(n);
for (int i=1;i<=n;i++) read(a[i]);
read(m);
for (int i=1;i<=m;i++) read(d[i].x),read(d[i].y),read(d[i].c);
a[0]=a[n+1]=INF;
Seg1::init();
rt=build(1,n);
Seg1::solve();
dfs(rt);
dfs(rt,rt);
for (int i=1;i<=n;i++) {
p[i]=back[Seg2::query(i,1,n,1)];//,printf("p[%d]=%d\n",i,p[i]);
}
solve(rt);
printf("%lld\n",f[rt]);
return 0;
}
Compilation message
constellation3.cpp: In function 'void dfs(int)':
constellation3.cpp:129:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0,v;i<son[u].size();i++) {
~^~~~~~~~~~~~~~
constellation3.cpp: In function 'void dfs(int, int)':
constellation3.cpp:142:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0,v;i<son[u].size();i++) {
~^~~~~~~~~~~~~~
constellation3.cpp: In function 'void solve(int)':
constellation3.cpp:175:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0,v;i<son[u].size();i++) {
~^~~~~~~~~~~~~~
constellation3.cpp:181:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<G[u].size();i++) {
~^~~~~~~~~~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:191:6: warning: unused variable 'x' [-Wunused-variable]
int x,y;
^
constellation3.cpp:191:8: warning: unused variable 'y' [-Wunused-variable]
int x,y;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
19456 KB |
Output is correct |
2 |
Correct |
20 ms |
19456 KB |
Output is correct |
3 |
Correct |
19 ms |
19456 KB |
Output is correct |
4 |
Correct |
15 ms |
19456 KB |
Output is correct |
5 |
Correct |
16 ms |
19328 KB |
Output is correct |
6 |
Correct |
16 ms |
19328 KB |
Output is correct |
7 |
Correct |
18 ms |
19456 KB |
Output is correct |
8 |
Correct |
16 ms |
19456 KB |
Output is correct |
9 |
Correct |
16 ms |
19328 KB |
Output is correct |
10 |
Correct |
16 ms |
19456 KB |
Output is correct |
11 |
Correct |
16 ms |
19456 KB |
Output is correct |
12 |
Correct |
17 ms |
19456 KB |
Output is correct |
13 |
Correct |
16 ms |
19456 KB |
Output is correct |
14 |
Correct |
16 ms |
19304 KB |
Output is correct |
15 |
Correct |
16 ms |
19328 KB |
Output is correct |
16 |
Correct |
16 ms |
19456 KB |
Output is correct |
17 |
Correct |
20 ms |
19456 KB |
Output is correct |
18 |
Correct |
16 ms |
19456 KB |
Output is correct |
19 |
Correct |
17 ms |
19328 KB |
Output is correct |
20 |
Correct |
16 ms |
19456 KB |
Output is correct |
21 |
Correct |
16 ms |
19456 KB |
Output is correct |
22 |
Correct |
16 ms |
19328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
19456 KB |
Output is correct |
2 |
Correct |
20 ms |
19456 KB |
Output is correct |
3 |
Correct |
19 ms |
19456 KB |
Output is correct |
4 |
Correct |
15 ms |
19456 KB |
Output is correct |
5 |
Correct |
16 ms |
19328 KB |
Output is correct |
6 |
Correct |
16 ms |
19328 KB |
Output is correct |
7 |
Correct |
18 ms |
19456 KB |
Output is correct |
8 |
Correct |
16 ms |
19456 KB |
Output is correct |
9 |
Correct |
16 ms |
19328 KB |
Output is correct |
10 |
Correct |
16 ms |
19456 KB |
Output is correct |
11 |
Correct |
16 ms |
19456 KB |
Output is correct |
12 |
Correct |
17 ms |
19456 KB |
Output is correct |
13 |
Correct |
16 ms |
19456 KB |
Output is correct |
14 |
Correct |
16 ms |
19304 KB |
Output is correct |
15 |
Correct |
16 ms |
19328 KB |
Output is correct |
16 |
Correct |
16 ms |
19456 KB |
Output is correct |
17 |
Correct |
20 ms |
19456 KB |
Output is correct |
18 |
Correct |
16 ms |
19456 KB |
Output is correct |
19 |
Correct |
17 ms |
19328 KB |
Output is correct |
20 |
Correct |
16 ms |
19456 KB |
Output is correct |
21 |
Correct |
16 ms |
19456 KB |
Output is correct |
22 |
Correct |
16 ms |
19328 KB |
Output is correct |
23 |
Correct |
19 ms |
20096 KB |
Output is correct |
24 |
Correct |
18 ms |
20096 KB |
Output is correct |
25 |
Correct |
19 ms |
20224 KB |
Output is correct |
26 |
Correct |
19 ms |
20224 KB |
Output is correct |
27 |
Correct |
19 ms |
20096 KB |
Output is correct |
28 |
Correct |
19 ms |
20096 KB |
Output is correct |
29 |
Correct |
19 ms |
20096 KB |
Output is correct |
30 |
Correct |
19 ms |
20224 KB |
Output is correct |
31 |
Correct |
19 ms |
20096 KB |
Output is correct |
32 |
Correct |
19 ms |
20480 KB |
Output is correct |
33 |
Correct |
20 ms |
20224 KB |
Output is correct |
34 |
Correct |
19 ms |
20224 KB |
Output is correct |
35 |
Correct |
20 ms |
20224 KB |
Output is correct |
36 |
Correct |
17 ms |
19840 KB |
Output is correct |
37 |
Correct |
17 ms |
19840 KB |
Output is correct |
38 |
Correct |
18 ms |
20352 KB |
Output is correct |
39 |
Correct |
18 ms |
20096 KB |
Output is correct |
40 |
Correct |
19 ms |
20352 KB |
Output is correct |
41 |
Correct |
18 ms |
19968 KB |
Output is correct |
42 |
Correct |
19 ms |
20096 KB |
Output is correct |
43 |
Correct |
18 ms |
20224 KB |
Output is correct |
44 |
Correct |
18 ms |
19968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
19456 KB |
Output is correct |
2 |
Correct |
20 ms |
19456 KB |
Output is correct |
3 |
Correct |
19 ms |
19456 KB |
Output is correct |
4 |
Correct |
15 ms |
19456 KB |
Output is correct |
5 |
Correct |
16 ms |
19328 KB |
Output is correct |
6 |
Correct |
16 ms |
19328 KB |
Output is correct |
7 |
Correct |
18 ms |
19456 KB |
Output is correct |
8 |
Correct |
16 ms |
19456 KB |
Output is correct |
9 |
Correct |
16 ms |
19328 KB |
Output is correct |
10 |
Correct |
16 ms |
19456 KB |
Output is correct |
11 |
Correct |
16 ms |
19456 KB |
Output is correct |
12 |
Correct |
17 ms |
19456 KB |
Output is correct |
13 |
Correct |
16 ms |
19456 KB |
Output is correct |
14 |
Correct |
16 ms |
19304 KB |
Output is correct |
15 |
Correct |
16 ms |
19328 KB |
Output is correct |
16 |
Correct |
16 ms |
19456 KB |
Output is correct |
17 |
Correct |
20 ms |
19456 KB |
Output is correct |
18 |
Correct |
16 ms |
19456 KB |
Output is correct |
19 |
Correct |
17 ms |
19328 KB |
Output is correct |
20 |
Correct |
16 ms |
19456 KB |
Output is correct |
21 |
Correct |
16 ms |
19456 KB |
Output is correct |
22 |
Correct |
16 ms |
19328 KB |
Output is correct |
23 |
Correct |
19 ms |
20096 KB |
Output is correct |
24 |
Correct |
18 ms |
20096 KB |
Output is correct |
25 |
Correct |
19 ms |
20224 KB |
Output is correct |
26 |
Correct |
19 ms |
20224 KB |
Output is correct |
27 |
Correct |
19 ms |
20096 KB |
Output is correct |
28 |
Correct |
19 ms |
20096 KB |
Output is correct |
29 |
Correct |
19 ms |
20096 KB |
Output is correct |
30 |
Correct |
19 ms |
20224 KB |
Output is correct |
31 |
Correct |
19 ms |
20096 KB |
Output is correct |
32 |
Correct |
19 ms |
20480 KB |
Output is correct |
33 |
Correct |
20 ms |
20224 KB |
Output is correct |
34 |
Correct |
19 ms |
20224 KB |
Output is correct |
35 |
Correct |
20 ms |
20224 KB |
Output is correct |
36 |
Correct |
17 ms |
19840 KB |
Output is correct |
37 |
Correct |
17 ms |
19840 KB |
Output is correct |
38 |
Correct |
18 ms |
20352 KB |
Output is correct |
39 |
Correct |
18 ms |
20096 KB |
Output is correct |
40 |
Correct |
19 ms |
20352 KB |
Output is correct |
41 |
Correct |
18 ms |
19968 KB |
Output is correct |
42 |
Correct |
19 ms |
20096 KB |
Output is correct |
43 |
Correct |
18 ms |
20224 KB |
Output is correct |
44 |
Correct |
18 ms |
19968 KB |
Output is correct |
45 |
Correct |
790 ms |
112308 KB |
Output is correct |
46 |
Correct |
755 ms |
111240 KB |
Output is correct |
47 |
Correct |
774 ms |
110200 KB |
Output is correct |
48 |
Correct |
759 ms |
112504 KB |
Output is correct |
49 |
Correct |
739 ms |
109432 KB |
Output is correct |
50 |
Correct |
752 ms |
109432 KB |
Output is correct |
51 |
Correct |
785 ms |
109760 KB |
Output is correct |
52 |
Correct |
771 ms |
111864 KB |
Output is correct |
53 |
Correct |
755 ms |
111480 KB |
Output is correct |
54 |
Correct |
982 ms |
124092 KB |
Output is correct |
55 |
Correct |
972 ms |
118648 KB |
Output is correct |
56 |
Correct |
938 ms |
115704 KB |
Output is correct |
57 |
Correct |
973 ms |
113392 KB |
Output is correct |
58 |
Correct |
568 ms |
80120 KB |
Output is correct |
59 |
Correct |
580 ms |
79992 KB |
Output is correct |
60 |
Correct |
384 ms |
131960 KB |
Output is correct |
61 |
Correct |
724 ms |
101624 KB |
Output is correct |
62 |
Correct |
985 ms |
117016 KB |
Output is correct |
63 |
Correct |
730 ms |
96168 KB |
Output is correct |
64 |
Correct |
760 ms |
98908 KB |
Output is correct |
65 |
Correct |
966 ms |
117900 KB |
Output is correct |
66 |
Correct |
738 ms |
95204 KB |
Output is correct |