#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXN=2e5+6;
ll n,m,sum;
struct star
{
ll x,y,c;
bool operator<(star const& other)const
{
return make_pair(y,c)>make_pair(other.y,other.c);
}
};
struct DSU
{
ll par[MAXN],sz[MAXN];
void init()
{
for(ll i=1;i<MAXN;i++)
{
par[i]=i;sz[i]=1;
}
}
ll getRoot(ll u)
{
if(par[u]==u)return u;
return par[u]=getRoot(par[u]);
}
void join(ll p,ll q)
{
if(sz[p]>=sz[q])
{
par[q]=p;
sz[p]+=sz[q];
}
else
{
par[p]=q;
sz[q]+=sz[p];
}
}
}dsu;
struct segment_tree
{
ll t[4*MAXN];
ll lazy[4*MAXN];
void init()
{
memset(t,0,sizeof(t));
memset(lazy,0,sizeof(lazy));
}
void push(ll l,ll r,ll idx)
{
t[idx]+=lazy[idx];
if(l!=r)
{
lazy[idx*2]+=lazy[idx];
lazy[idx*2+1]+=lazy[idx];
}
lazy[idx]=0;
}
void update(ll l,ll r,ll idx,ll ql,ll qr,ll val)
{
push(l,r,idx);
if(l>qr||r<ql)return;
if(l>=ql&&r<=qr)
{
lazy[idx]+=val;
push(l,r,idx);
return;
}
ll mid=(l+r)/2;
update(l,mid,idx*2,ql,qr,val);
update(mid+1,r,idx*2+1,ql,qr,val);
t[idx]=max(t[idx*2],t[idx*2+1]);
}
ll query(ll l,ll r,ll idx,ll ql,ll qr)
{
push(l,r,idx);
if(l>qr||r<ql)return 0;
if(l>=ql&&r<=qr)
{
return t[idx];
}
ll mid=(l+r)/2;
return max(query(l,mid,idx*2,ql,qr),query(mid+1,r,idx*2+1,ql,qr));
}
void update(ll l,ll r,ll val)
{
update(1,n,1,l,r,val);
}
ll query(ll l,ll r)
{
return query(1,n,1,l,r);
}
}tree;
ll a[MAXN];
vector<star>starsx[MAXN];
vector<star>starsy[MAXN];
vector<ll>jp[MAXN];
ll pr[MAXN];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
dsu.init();tree.init();
cin>>n;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
if(i>1)jp[max(a[i],a[i-1])].push_back(i);
}
cin>>m;
for(ll i=1;i<=m;i++)
{
ll x,y,c;
cin>>x>>y>>c;sum+=c;
starsx[x].push_back({x,y,c});
}
for(ll i=1;i<=n;i++)
{
vector<star>v1;
sort(starsx[i].begin(),starsx[i].end());
for(ll j=0;j<starsx[i].size();j++)
{
while(!v1.size()==0&&v1.back().c<starsx[i][j].c)
{
v1.pop_back();
}
v1.push_back(starsx[i][j]);
}
for(auto xd:v1)
{
starsy[xd.y].push_back(xd);
}
}
for(ll i=1;i<=n;i++)
{
for(auto xd:starsy[i])
{
tree.update(xd.x,xd.x,xd.c-pr[xd.x]);
pr[xd.x]=xd.c;
}
for(auto xd:jp[i])
{
ll e1=xd,e2=xd-1;
ll r1=dsu.getRoot(e1);
ll r2=dsu.getRoot(e2);
ll mxl=tree.query(e2-dsu.sz[r2]+1,e2);
ll mxr=tree.query(e1,e1+dsu.sz[r1]-1);
tree.update(e2-dsu.sz[r2]+1,e2,mxr);
tree.update(e1,e1+dsu.sz[r1]-1,mxl);
dsu.join(r1,r2);
}
}
cout<<sum-tree.query(1,n)<<endl;
return 0;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:125:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<star>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for(ll j=0;j<starsx[i].size();j++)
| ~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
30080 KB |
Output is correct |
2 |
Correct |
15 ms |
30040 KB |
Output is correct |
3 |
Correct |
15 ms |
30080 KB |
Output is correct |
4 |
Correct |
15 ms |
30076 KB |
Output is correct |
5 |
Correct |
15 ms |
30108 KB |
Output is correct |
6 |
Correct |
16 ms |
30084 KB |
Output is correct |
7 |
Correct |
15 ms |
30028 KB |
Output is correct |
8 |
Correct |
16 ms |
30028 KB |
Output is correct |
9 |
Correct |
16 ms |
30028 KB |
Output is correct |
10 |
Correct |
16 ms |
30044 KB |
Output is correct |
11 |
Correct |
15 ms |
30032 KB |
Output is correct |
12 |
Correct |
15 ms |
30028 KB |
Output is correct |
13 |
Correct |
17 ms |
30088 KB |
Output is correct |
14 |
Correct |
16 ms |
30016 KB |
Output is correct |
15 |
Correct |
16 ms |
30064 KB |
Output is correct |
16 |
Correct |
15 ms |
30032 KB |
Output is correct |
17 |
Correct |
15 ms |
30028 KB |
Output is correct |
18 |
Correct |
16 ms |
30004 KB |
Output is correct |
19 |
Correct |
15 ms |
30028 KB |
Output is correct |
20 |
Correct |
15 ms |
30016 KB |
Output is correct |
21 |
Correct |
16 ms |
30116 KB |
Output is correct |
22 |
Correct |
16 ms |
30028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
30080 KB |
Output is correct |
2 |
Correct |
15 ms |
30040 KB |
Output is correct |
3 |
Correct |
15 ms |
30080 KB |
Output is correct |
4 |
Correct |
15 ms |
30076 KB |
Output is correct |
5 |
Correct |
15 ms |
30108 KB |
Output is correct |
6 |
Correct |
16 ms |
30084 KB |
Output is correct |
7 |
Correct |
15 ms |
30028 KB |
Output is correct |
8 |
Correct |
16 ms |
30028 KB |
Output is correct |
9 |
Correct |
16 ms |
30028 KB |
Output is correct |
10 |
Correct |
16 ms |
30044 KB |
Output is correct |
11 |
Correct |
15 ms |
30032 KB |
Output is correct |
12 |
Correct |
15 ms |
30028 KB |
Output is correct |
13 |
Correct |
17 ms |
30088 KB |
Output is correct |
14 |
Correct |
16 ms |
30016 KB |
Output is correct |
15 |
Correct |
16 ms |
30064 KB |
Output is correct |
16 |
Correct |
15 ms |
30032 KB |
Output is correct |
17 |
Correct |
15 ms |
30028 KB |
Output is correct |
18 |
Correct |
16 ms |
30004 KB |
Output is correct |
19 |
Correct |
15 ms |
30028 KB |
Output is correct |
20 |
Correct |
15 ms |
30016 KB |
Output is correct |
21 |
Correct |
16 ms |
30116 KB |
Output is correct |
22 |
Correct |
16 ms |
30028 KB |
Output is correct |
23 |
Correct |
18 ms |
30208 KB |
Output is correct |
24 |
Correct |
18 ms |
30192 KB |
Output is correct |
25 |
Correct |
19 ms |
30188 KB |
Output is correct |
26 |
Correct |
19 ms |
30180 KB |
Output is correct |
27 |
Correct |
19 ms |
30288 KB |
Output is correct |
28 |
Correct |
19 ms |
30216 KB |
Output is correct |
29 |
Correct |
19 ms |
30284 KB |
Output is correct |
30 |
Correct |
19 ms |
30200 KB |
Output is correct |
31 |
Correct |
19 ms |
30284 KB |
Output is correct |
32 |
Correct |
19 ms |
30208 KB |
Output is correct |
33 |
Correct |
19 ms |
30284 KB |
Output is correct |
34 |
Correct |
19 ms |
30284 KB |
Output is correct |
35 |
Correct |
18 ms |
30284 KB |
Output is correct |
36 |
Correct |
18 ms |
30284 KB |
Output is correct |
37 |
Correct |
19 ms |
30284 KB |
Output is correct |
38 |
Correct |
18 ms |
30264 KB |
Output is correct |
39 |
Correct |
19 ms |
30248 KB |
Output is correct |
40 |
Correct |
19 ms |
30284 KB |
Output is correct |
41 |
Correct |
18 ms |
30344 KB |
Output is correct |
42 |
Correct |
18 ms |
30344 KB |
Output is correct |
43 |
Correct |
19 ms |
30248 KB |
Output is correct |
44 |
Correct |
19 ms |
30248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
30080 KB |
Output is correct |
2 |
Correct |
15 ms |
30040 KB |
Output is correct |
3 |
Correct |
15 ms |
30080 KB |
Output is correct |
4 |
Correct |
15 ms |
30076 KB |
Output is correct |
5 |
Correct |
15 ms |
30108 KB |
Output is correct |
6 |
Correct |
16 ms |
30084 KB |
Output is correct |
7 |
Correct |
15 ms |
30028 KB |
Output is correct |
8 |
Correct |
16 ms |
30028 KB |
Output is correct |
9 |
Correct |
16 ms |
30028 KB |
Output is correct |
10 |
Correct |
16 ms |
30044 KB |
Output is correct |
11 |
Correct |
15 ms |
30032 KB |
Output is correct |
12 |
Correct |
15 ms |
30028 KB |
Output is correct |
13 |
Correct |
17 ms |
30088 KB |
Output is correct |
14 |
Correct |
16 ms |
30016 KB |
Output is correct |
15 |
Correct |
16 ms |
30064 KB |
Output is correct |
16 |
Correct |
15 ms |
30032 KB |
Output is correct |
17 |
Correct |
15 ms |
30028 KB |
Output is correct |
18 |
Correct |
16 ms |
30004 KB |
Output is correct |
19 |
Correct |
15 ms |
30028 KB |
Output is correct |
20 |
Correct |
15 ms |
30016 KB |
Output is correct |
21 |
Correct |
16 ms |
30116 KB |
Output is correct |
22 |
Correct |
16 ms |
30028 KB |
Output is correct |
23 |
Correct |
18 ms |
30208 KB |
Output is correct |
24 |
Correct |
18 ms |
30192 KB |
Output is correct |
25 |
Correct |
19 ms |
30188 KB |
Output is correct |
26 |
Correct |
19 ms |
30180 KB |
Output is correct |
27 |
Correct |
19 ms |
30288 KB |
Output is correct |
28 |
Correct |
19 ms |
30216 KB |
Output is correct |
29 |
Correct |
19 ms |
30284 KB |
Output is correct |
30 |
Correct |
19 ms |
30200 KB |
Output is correct |
31 |
Correct |
19 ms |
30284 KB |
Output is correct |
32 |
Correct |
19 ms |
30208 KB |
Output is correct |
33 |
Correct |
19 ms |
30284 KB |
Output is correct |
34 |
Correct |
19 ms |
30284 KB |
Output is correct |
35 |
Correct |
18 ms |
30284 KB |
Output is correct |
36 |
Correct |
18 ms |
30284 KB |
Output is correct |
37 |
Correct |
19 ms |
30284 KB |
Output is correct |
38 |
Correct |
18 ms |
30264 KB |
Output is correct |
39 |
Correct |
19 ms |
30248 KB |
Output is correct |
40 |
Correct |
19 ms |
30284 KB |
Output is correct |
41 |
Correct |
18 ms |
30344 KB |
Output is correct |
42 |
Correct |
18 ms |
30344 KB |
Output is correct |
43 |
Correct |
19 ms |
30248 KB |
Output is correct |
44 |
Correct |
19 ms |
30248 KB |
Output is correct |
45 |
Correct |
698 ms |
53476 KB |
Output is correct |
46 |
Correct |
656 ms |
53312 KB |
Output is correct |
47 |
Correct |
673 ms |
53704 KB |
Output is correct |
48 |
Correct |
673 ms |
53144 KB |
Output is correct |
49 |
Correct |
689 ms |
53284 KB |
Output is correct |
50 |
Correct |
640 ms |
52944 KB |
Output is correct |
51 |
Correct |
656 ms |
53212 KB |
Output is correct |
52 |
Correct |
663 ms |
53728 KB |
Output is correct |
53 |
Correct |
683 ms |
53236 KB |
Output is correct |
54 |
Correct |
621 ms |
54468 KB |
Output is correct |
55 |
Correct |
592 ms |
54272 KB |
Output is correct |
56 |
Correct |
600 ms |
54012 KB |
Output is correct |
57 |
Correct |
573 ms |
54204 KB |
Output is correct |
58 |
Correct |
551 ms |
51640 KB |
Output is correct |
59 |
Correct |
557 ms |
51860 KB |
Output is correct |
60 |
Correct |
416 ms |
55620 KB |
Output is correct |
61 |
Correct |
485 ms |
52248 KB |
Output is correct |
62 |
Correct |
629 ms |
55824 KB |
Output is correct |
63 |
Correct |
481 ms |
51336 KB |
Output is correct |
64 |
Correct |
478 ms |
51480 KB |
Output is correct |
65 |
Correct |
653 ms |
55416 KB |
Output is correct |
66 |
Correct |
475 ms |
51964 KB |
Output is correct |