#include <bits/stdc++.h>
#define MAXN 200007
using namespace std;
long long dp[MAXN],seg[4*MAXN];
int lc[40*MAXN],per[40*MAXN],mxb[MAXN],mnb[MAXN],rc[40*MAXN],dl[MAXN],dr[MAXN],h[MAXN],root[MAXN],cnt,n,m;
struct star{int x,y; long long c;};
bool cmp(star a,star b) {return a.y<b.y;}
star a[MAXN];
vector<int> zp[MAXN],zf[MAXN];
void updmxb(int x,int ind)
{
while(ind<MAXN)
{
mxb[ind]=max(mxb[ind],x);
ind+=(ind&-ind);
}
}
int nmax(int ind)
{
int val=0;
while(ind)
{
val=max(val,mxb[ind]);
ind-=(ind&-ind);
}
return val;
}
void updmnb(int x,int ind)
{
while(ind<MAXN)
{
mnb[ind]=min(mnb[ind],x);
ind+=ind&-ind;
}
}
int nmin(int ind)
{
int val=MAXN;
while(ind)
{
val=min(val,mnb[ind]);
ind-=(ind&-ind);
}
return val;
}
void updseg(int l,int r,int lt,int rt,long long v,int ind)
{
if(r<lt || l>rt) return;
if(l>=lt && r<=rt) {seg[ind]+=v; return;}
int s=(l+r)/2;
updseg(l,s,lt,rt,v,2*ind);
updseg(s+1,r,lt,rt,v,2*ind+1);
}
long long nsum(int l,int r,int t,int ind)
{
if(l==r) return seg[ind];
int s=(l+r)/2;
if(s>=t) return seg[ind]+nsum(l,s,t,2*ind);
return seg[ind]+nsum(s+1,r,t,2*ind+1);
}
void makeper(int l,int r,int ind)
{
per[ind]=m+1;
if(l==r) return;
int s=(l+r)/2;
lc[ind]=++cnt;
makeper(l,s,lc[ind]);
rc[ind]=++cnt;
makeper(s+1,r,rc[ind]);
}
void makenewper(int l,int r,int t,int val,int inh,int ind)
{
per[ind]=val;
if(l==r) return;
int s=(l+r)/2;
if(t<=s)
{
rc[ind]=rc[inh];
lc[ind]=++cnt;
makenewper(l,s,t,val,lc[inh],lc[ind]);
}
else
{
lc[ind]=lc[inh];
rc[ind]=++cnt;
makenewper(s+1,r,t,val,rc[inh],rc[ind]);
}
}
int fnd(int l,int r,int lt,int rt,int ind)
{
if(r<lt || l>rt) return MAXN;
if(l>=lt && r<=rt) return per[ind];
int s=(l+r)/2;
return min(fnd(l,s,lt,rt,lc[ind]),fnd(s+1,r,lt,rt,rc[ind]));
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&h[i]);
scanf("%d",&m);
for(int i=1;i<=m;i++) scanf("%d%d%lld",&a[i].x,&a[i].y,&a[i].c);
sort(a+1,a+m+1,cmp);
for(int i=1;i<=m;i++) zf[a[i].x].push_back(i);
fill(mnb,mnb+MAXN,n+1);
for(int i=1;i<=n;i++)
{
updmxb(i,n+1-h[i]);
for(int j=0;j<zf[i].size();j++) dl[zf[i][j]]=nmax(n+1-a[zf[i][j]].y);
}
for(int i=n;i>=1;i--)
{
updmnb(i,n+1-h[i]);
for(int j=0;j<zf[i].size();j++) dr[zf[i][j]]=nmin(n+1-a[zf[i][j]].y);
}
makeper(1,n,root[m+1]);
for(int i=m;i>=1;i--)
{
root[i]=++cnt;
makenewper(1,n,a[i].x,i,root[i+1],root[i]);
}
a[m+1].c=0;
for(int i=1;i<=m+1;i++)
{
long long nc=nsum(1,n,a[i].x,1),ac=a[i].c;
for(int j=0;j<zp[i].size();j++) {nc+=dp[zp[i][j]]; ac+=dp[zp[i][j]];}
dp[i]=min(ac,nc);
if(i==m+1) continue;
int nxt=fnd(1,n,dl[i]+1,dr[i]-1,root[i+1]);
zp[nxt].push_back(i);
updseg(1,n,dl[i]+1,dr[i]-1,ac-dp[i],1);
}
printf("%lld",dp[m+1]);
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:108:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<zf[i].size();j++) dl[zf[i][j]]=nmax(n+1-a[zf[i][j]].y);
~^~~~~~~~~~~~~
constellation3.cpp:113:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<zf[i].size();j++) dr[zf[i][j]]=nmin(n+1-a[zf[i][j]].y);
~^~~~~~~~~~~~~
constellation3.cpp:125:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<zp[i].size();j++) {nc+=dp[zp[i][j]]; ac+=dp[zp[i][j]];}
~^~~~~~~~~~~~~
constellation3.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
constellation3.cpp:99:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%d",&h[i]);
~~~~~^~~~~~~~~~~~
constellation3.cpp:100:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&m);
~~~~~^~~~~~~~~
constellation3.cpp:101:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=m;i++) scanf("%d%d%lld",&a[i].x,&a[i].y,&a[i].c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
10624 KB |
Output is correct |
2 |
Correct |
11 ms |
10752 KB |
Output is correct |
3 |
Correct |
10 ms |
10624 KB |
Output is correct |
4 |
Correct |
10 ms |
10752 KB |
Output is correct |
5 |
Correct |
10 ms |
10752 KB |
Output is correct |
6 |
Correct |
10 ms |
10752 KB |
Output is correct |
7 |
Correct |
11 ms |
10752 KB |
Output is correct |
8 |
Correct |
11 ms |
10752 KB |
Output is correct |
9 |
Correct |
11 ms |
10880 KB |
Output is correct |
10 |
Correct |
11 ms |
10880 KB |
Output is correct |
11 |
Correct |
10 ms |
10752 KB |
Output is correct |
12 |
Correct |
11 ms |
10752 KB |
Output is correct |
13 |
Correct |
12 ms |
10752 KB |
Output is correct |
14 |
Correct |
10 ms |
10752 KB |
Output is correct |
15 |
Correct |
10 ms |
10880 KB |
Output is correct |
16 |
Correct |
11 ms |
10752 KB |
Output is correct |
17 |
Correct |
11 ms |
10752 KB |
Output is correct |
18 |
Correct |
11 ms |
10752 KB |
Output is correct |
19 |
Correct |
10 ms |
10752 KB |
Output is correct |
20 |
Correct |
11 ms |
10752 KB |
Output is correct |
21 |
Correct |
11 ms |
10752 KB |
Output is correct |
22 |
Correct |
11 ms |
10752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
10624 KB |
Output is correct |
2 |
Correct |
11 ms |
10752 KB |
Output is correct |
3 |
Correct |
10 ms |
10624 KB |
Output is correct |
4 |
Correct |
10 ms |
10752 KB |
Output is correct |
5 |
Correct |
10 ms |
10752 KB |
Output is correct |
6 |
Correct |
10 ms |
10752 KB |
Output is correct |
7 |
Correct |
11 ms |
10752 KB |
Output is correct |
8 |
Correct |
11 ms |
10752 KB |
Output is correct |
9 |
Correct |
11 ms |
10880 KB |
Output is correct |
10 |
Correct |
11 ms |
10880 KB |
Output is correct |
11 |
Correct |
10 ms |
10752 KB |
Output is correct |
12 |
Correct |
11 ms |
10752 KB |
Output is correct |
13 |
Correct |
12 ms |
10752 KB |
Output is correct |
14 |
Correct |
10 ms |
10752 KB |
Output is correct |
15 |
Correct |
10 ms |
10880 KB |
Output is correct |
16 |
Correct |
11 ms |
10752 KB |
Output is correct |
17 |
Correct |
11 ms |
10752 KB |
Output is correct |
18 |
Correct |
11 ms |
10752 KB |
Output is correct |
19 |
Correct |
10 ms |
10752 KB |
Output is correct |
20 |
Correct |
11 ms |
10752 KB |
Output is correct |
21 |
Correct |
11 ms |
10752 KB |
Output is correct |
22 |
Correct |
11 ms |
10752 KB |
Output is correct |
23 |
Correct |
13 ms |
11136 KB |
Output is correct |
24 |
Correct |
13 ms |
11136 KB |
Output is correct |
25 |
Correct |
13 ms |
11136 KB |
Output is correct |
26 |
Correct |
14 ms |
11136 KB |
Output is correct |
27 |
Correct |
14 ms |
11136 KB |
Output is correct |
28 |
Correct |
14 ms |
11136 KB |
Output is correct |
29 |
Correct |
13 ms |
11136 KB |
Output is correct |
30 |
Correct |
14 ms |
11136 KB |
Output is correct |
31 |
Correct |
13 ms |
11136 KB |
Output is correct |
32 |
Correct |
13 ms |
11136 KB |
Output is correct |
33 |
Correct |
14 ms |
11264 KB |
Output is correct |
34 |
Correct |
13 ms |
11136 KB |
Output is correct |
35 |
Correct |
13 ms |
11264 KB |
Output is correct |
36 |
Correct |
14 ms |
11264 KB |
Output is correct |
37 |
Correct |
13 ms |
11264 KB |
Output is correct |
38 |
Correct |
13 ms |
11136 KB |
Output is correct |
39 |
Correct |
14 ms |
11136 KB |
Output is correct |
40 |
Correct |
14 ms |
11136 KB |
Output is correct |
41 |
Correct |
13 ms |
11136 KB |
Output is correct |
42 |
Correct |
13 ms |
11136 KB |
Output is correct |
43 |
Correct |
13 ms |
11136 KB |
Output is correct |
44 |
Correct |
14 ms |
11136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
10624 KB |
Output is correct |
2 |
Correct |
11 ms |
10752 KB |
Output is correct |
3 |
Correct |
10 ms |
10624 KB |
Output is correct |
4 |
Correct |
10 ms |
10752 KB |
Output is correct |
5 |
Correct |
10 ms |
10752 KB |
Output is correct |
6 |
Correct |
10 ms |
10752 KB |
Output is correct |
7 |
Correct |
11 ms |
10752 KB |
Output is correct |
8 |
Correct |
11 ms |
10752 KB |
Output is correct |
9 |
Correct |
11 ms |
10880 KB |
Output is correct |
10 |
Correct |
11 ms |
10880 KB |
Output is correct |
11 |
Correct |
10 ms |
10752 KB |
Output is correct |
12 |
Correct |
11 ms |
10752 KB |
Output is correct |
13 |
Correct |
12 ms |
10752 KB |
Output is correct |
14 |
Correct |
10 ms |
10752 KB |
Output is correct |
15 |
Correct |
10 ms |
10880 KB |
Output is correct |
16 |
Correct |
11 ms |
10752 KB |
Output is correct |
17 |
Correct |
11 ms |
10752 KB |
Output is correct |
18 |
Correct |
11 ms |
10752 KB |
Output is correct |
19 |
Correct |
10 ms |
10752 KB |
Output is correct |
20 |
Correct |
11 ms |
10752 KB |
Output is correct |
21 |
Correct |
11 ms |
10752 KB |
Output is correct |
22 |
Correct |
11 ms |
10752 KB |
Output is correct |
23 |
Correct |
13 ms |
11136 KB |
Output is correct |
24 |
Correct |
13 ms |
11136 KB |
Output is correct |
25 |
Correct |
13 ms |
11136 KB |
Output is correct |
26 |
Correct |
14 ms |
11136 KB |
Output is correct |
27 |
Correct |
14 ms |
11136 KB |
Output is correct |
28 |
Correct |
14 ms |
11136 KB |
Output is correct |
29 |
Correct |
13 ms |
11136 KB |
Output is correct |
30 |
Correct |
14 ms |
11136 KB |
Output is correct |
31 |
Correct |
13 ms |
11136 KB |
Output is correct |
32 |
Correct |
13 ms |
11136 KB |
Output is correct |
33 |
Correct |
14 ms |
11264 KB |
Output is correct |
34 |
Correct |
13 ms |
11136 KB |
Output is correct |
35 |
Correct |
13 ms |
11264 KB |
Output is correct |
36 |
Correct |
14 ms |
11264 KB |
Output is correct |
37 |
Correct |
13 ms |
11264 KB |
Output is correct |
38 |
Correct |
13 ms |
11136 KB |
Output is correct |
39 |
Correct |
14 ms |
11136 KB |
Output is correct |
40 |
Correct |
14 ms |
11136 KB |
Output is correct |
41 |
Correct |
13 ms |
11136 KB |
Output is correct |
42 |
Correct |
13 ms |
11136 KB |
Output is correct |
43 |
Correct |
13 ms |
11136 KB |
Output is correct |
44 |
Correct |
14 ms |
11136 KB |
Output is correct |
45 |
Correct |
556 ms |
84856 KB |
Output is correct |
46 |
Correct |
641 ms |
83064 KB |
Output is correct |
47 |
Correct |
673 ms |
85624 KB |
Output is correct |
48 |
Correct |
522 ms |
83064 KB |
Output is correct |
49 |
Correct |
523 ms |
83576 KB |
Output is correct |
50 |
Correct |
623 ms |
82680 KB |
Output is correct |
51 |
Correct |
626 ms |
84216 KB |
Output is correct |
52 |
Correct |
643 ms |
85240 KB |
Output is correct |
53 |
Correct |
542 ms |
83192 KB |
Output is correct |
54 |
Correct |
499 ms |
86776 KB |
Output is correct |
55 |
Correct |
449 ms |
86776 KB |
Output is correct |
56 |
Correct |
433 ms |
85752 KB |
Output is correct |
57 |
Correct |
497 ms |
86136 KB |
Output is correct |
58 |
Correct |
403 ms |
84216 KB |
Output is correct |
59 |
Correct |
456 ms |
84856 KB |
Output is correct |
60 |
Correct |
251 ms |
83576 KB |
Output is correct |
61 |
Correct |
481 ms |
84088 KB |
Output is correct |
62 |
Correct |
494 ms |
85752 KB |
Output is correct |
63 |
Correct |
536 ms |
84856 KB |
Output is correct |
64 |
Correct |
462 ms |
82552 KB |
Output is correct |
65 |
Correct |
443 ms |
84088 KB |
Output is correct |
66 |
Correct |
468 ms |
85752 KB |
Output is correct |