#include<bits/stdc++.h>
using namespace std;
const int nmax=2e5+42;
vector< pair<int/*x*/,int/*gain*/> > stars[nmax];
vector< pair<int/*y*/,int/*gain*/> > seen[nmax];
bool cmp(pair<int/*y*/,int/*gain*/> a,pair<int/*y*/,int/*gain*/> b)
{
if(a.first!=b.first)return a.first>b.first;
return a.second<b.second;
}
int n;
int inp[nmax];
int q;
vector< int/*x*/ > white[nmax];
int prv_seen[nmax];
struct info
{
long long lazy,mx;
};
info tree[4*nmax];
info actual(info a)
{
a.mx+=a.lazy;
a.lazy=0;
return a;
}
info my_merge(info a,info b)
{
a=actual(a);
b=actual(b);
a.mx=max(a.mx,b.mx);
return a;
}
void push(int node)
{
tree[node*2].lazy+=tree[node].lazy;
tree[node*2+1].lazy+=tree[node].lazy;
tree[node].lazy=0;
}
void update(int node,int l,int r,int lq,int rq,long long val)
{
if(l==lq&&r==rq)
{
tree[node].lazy+=val;
return;
}
push(node);
int av=(l+r)/2;
if(lq<=av)update(node*2,l,av,lq,min(av,rq),val);
if(av<rq)update(node*2+1,av+1,r,max(av+1,lq),rq,val);
tree[node]=my_merge(tree[node*2],tree[node*2+1]);
}
info query(int node,int l,int r,int lq,int rq)
{
if(l==lq&&r==rq)return actual(tree[node]);
int av=(l+r)/2;
info ret;ret.mx=0;ret.lazy=0;
if(lq<=av)ret=my_merge(ret,query(node*2,l,av,lq,min(av,rq)));
if(av<rq)ret=my_merge(ret,query(node*2+1,av+1,r,max(av+1,lq),rq));
return ret;
}
int le[nmax],ri[nmax],parent[nmax];
int root(int node)
{
if(parent[node]==node)return node;
parent[node]=root(parent[node]);
return parent[node];
}
int main()
{
scanf("%i",&n);
for(int i=1;i<=n;i++)scanf("%i",&inp[i]);
for(int i=1;i<n;i++)
white[max(inp[i],inp[i+1])].push_back(i);
long long outp=0;
scanf("%i",&q);
for(int i=1;i<=q;i++)
{
int x,y,c;
scanf("%i%i%i",&x,&y,&c);
outp+=c;
seen[x].push_back({y,c});
//stars[y].push_back({x,c});
}
for(int i=1;i<=n;i++)
if(seen[i].size())
{
sort(seen[i].begin(),seen[i].end(),cmp);
vector< pair<int/*y*/,int/*gain*/> > help={};
for(auto k:seen[i])
{
while(help.size()&&help.back().second<=k.second)
{
//outp-=help.back().second;
help.pop_back();
}
help.push_back(k);
}
for(auto k:help)
{
stars[k.first].push_back({i,k.second});
}
/*
cout<<i<<endl;
for(auto k:seen[i])
{
cout<<"y= "<<k.first<<" cost= "<<k.second<<endl;
}
cout<<"->"<<endl;
for(auto k:help)
{
cout<<"y= "<<k.first<<" cost= "<<k.second<<endl;
}
*/
}
for(int i=1;i<=n;i++)
{
le[i]=i;
ri[i]=i;
parent[i]=i;
}
for(int i=1;i<=n;i++)
{
for(auto k:stars[i])
{
update(1,1,n,k.first,k.first,k.second-prv_seen[k.first]);
prv_seen[k.first]=k.second;
}
for(auto k:white[i])
{
int l1=le[root(k)],r1=k;
int l2=k+1,r2=ri[root(k+1)];
long long mx_1=query(1,1,n,l1,r1).mx,mx_2=query(1,1,n,l2,r2).mx;
parent[root(k+1)]=root(k);
le[root(k)]=l1;
ri[root(k)]=r2;
update(1,1,n,l1,r1,mx_2);
update(1,1,n,l2,r2,mx_1);
}
}
printf("%lld\n",outp-actual(tree[1]).mx);
return 0;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
96 | scanf("%i",&n);
| ~~~~~^~~~~~~~~
constellation3.cpp:97:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
97 | for(int i=1;i<=n;i++)scanf("%i",&inp[i]);
| ~~~~~^~~~~~~~~~~~~~
constellation3.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
104 | scanf("%i",&q);
| ~~~~~^~~~~~~~~
constellation3.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
109 | scanf("%i%i%i",&x,&y,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
9 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
11 ms |
14592 KB |
Output is correct |
10 |
Correct |
10 ms |
14592 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
9 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14464 KB |
Output is correct |
14 |
Correct |
12 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14464 KB |
Output is correct |
17 |
Correct |
10 ms |
14464 KB |
Output is correct |
18 |
Correct |
9 ms |
14464 KB |
Output is correct |
19 |
Correct |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
10 ms |
14464 KB |
Output is correct |
21 |
Correct |
9 ms |
14464 KB |
Output is correct |
22 |
Correct |
10 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
9 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
11 ms |
14592 KB |
Output is correct |
10 |
Correct |
10 ms |
14592 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
9 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14464 KB |
Output is correct |
14 |
Correct |
12 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14464 KB |
Output is correct |
17 |
Correct |
10 ms |
14464 KB |
Output is correct |
18 |
Correct |
9 ms |
14464 KB |
Output is correct |
19 |
Correct |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
10 ms |
14464 KB |
Output is correct |
21 |
Correct |
9 ms |
14464 KB |
Output is correct |
22 |
Correct |
10 ms |
14464 KB |
Output is correct |
23 |
Correct |
12 ms |
14720 KB |
Output is correct |
24 |
Correct |
13 ms |
14720 KB |
Output is correct |
25 |
Correct |
13 ms |
14720 KB |
Output is correct |
26 |
Correct |
12 ms |
14720 KB |
Output is correct |
27 |
Correct |
14 ms |
14720 KB |
Output is correct |
28 |
Correct |
12 ms |
14720 KB |
Output is correct |
29 |
Correct |
12 ms |
14720 KB |
Output is correct |
30 |
Correct |
13 ms |
14720 KB |
Output is correct |
31 |
Correct |
13 ms |
14848 KB |
Output is correct |
32 |
Correct |
13 ms |
14720 KB |
Output is correct |
33 |
Correct |
12 ms |
14720 KB |
Output is correct |
34 |
Correct |
12 ms |
14720 KB |
Output is correct |
35 |
Correct |
13 ms |
14720 KB |
Output is correct |
36 |
Correct |
12 ms |
14592 KB |
Output is correct |
37 |
Correct |
12 ms |
14720 KB |
Output is correct |
38 |
Correct |
12 ms |
14720 KB |
Output is correct |
39 |
Correct |
12 ms |
14720 KB |
Output is correct |
40 |
Correct |
12 ms |
14848 KB |
Output is correct |
41 |
Correct |
12 ms |
14720 KB |
Output is correct |
42 |
Correct |
12 ms |
14720 KB |
Output is correct |
43 |
Correct |
13 ms |
14720 KB |
Output is correct |
44 |
Correct |
12 ms |
14720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
9 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
11 ms |
14592 KB |
Output is correct |
10 |
Correct |
10 ms |
14592 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
9 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14464 KB |
Output is correct |
14 |
Correct |
12 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14464 KB |
Output is correct |
17 |
Correct |
10 ms |
14464 KB |
Output is correct |
18 |
Correct |
9 ms |
14464 KB |
Output is correct |
19 |
Correct |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
10 ms |
14464 KB |
Output is correct |
21 |
Correct |
9 ms |
14464 KB |
Output is correct |
22 |
Correct |
10 ms |
14464 KB |
Output is correct |
23 |
Correct |
12 ms |
14720 KB |
Output is correct |
24 |
Correct |
13 ms |
14720 KB |
Output is correct |
25 |
Correct |
13 ms |
14720 KB |
Output is correct |
26 |
Correct |
12 ms |
14720 KB |
Output is correct |
27 |
Correct |
14 ms |
14720 KB |
Output is correct |
28 |
Correct |
12 ms |
14720 KB |
Output is correct |
29 |
Correct |
12 ms |
14720 KB |
Output is correct |
30 |
Correct |
13 ms |
14720 KB |
Output is correct |
31 |
Correct |
13 ms |
14848 KB |
Output is correct |
32 |
Correct |
13 ms |
14720 KB |
Output is correct |
33 |
Correct |
12 ms |
14720 KB |
Output is correct |
34 |
Correct |
12 ms |
14720 KB |
Output is correct |
35 |
Correct |
13 ms |
14720 KB |
Output is correct |
36 |
Correct |
12 ms |
14592 KB |
Output is correct |
37 |
Correct |
12 ms |
14720 KB |
Output is correct |
38 |
Correct |
12 ms |
14720 KB |
Output is correct |
39 |
Correct |
12 ms |
14720 KB |
Output is correct |
40 |
Correct |
12 ms |
14848 KB |
Output is correct |
41 |
Correct |
12 ms |
14720 KB |
Output is correct |
42 |
Correct |
12 ms |
14720 KB |
Output is correct |
43 |
Correct |
13 ms |
14720 KB |
Output is correct |
44 |
Correct |
12 ms |
14720 KB |
Output is correct |
45 |
Correct |
598 ms |
42360 KB |
Output is correct |
46 |
Correct |
608 ms |
42232 KB |
Output is correct |
47 |
Correct |
640 ms |
42616 KB |
Output is correct |
48 |
Correct |
689 ms |
42232 KB |
Output is correct |
49 |
Correct |
665 ms |
42012 KB |
Output is correct |
50 |
Correct |
658 ms |
41848 KB |
Output is correct |
51 |
Correct |
586 ms |
42004 KB |
Output is correct |
52 |
Correct |
606 ms |
42616 KB |
Output is correct |
53 |
Correct |
600 ms |
42360 KB |
Output is correct |
54 |
Correct |
526 ms |
43640 KB |
Output is correct |
55 |
Correct |
515 ms |
43640 KB |
Output is correct |
56 |
Correct |
498 ms |
43128 KB |
Output is correct |
57 |
Correct |
496 ms |
43384 KB |
Output is correct |
58 |
Correct |
482 ms |
39924 KB |
Output is correct |
59 |
Correct |
478 ms |
40172 KB |
Output is correct |
60 |
Correct |
364 ms |
43312 KB |
Output is correct |
61 |
Correct |
433 ms |
39192 KB |
Output is correct |
62 |
Correct |
599 ms |
43476 KB |
Output is correct |
63 |
Correct |
426 ms |
38860 KB |
Output is correct |
64 |
Correct |
420 ms |
38520 KB |
Output is correct |
65 |
Correct |
572 ms |
43316 KB |
Output is correct |
66 |
Correct |
411 ms |
39028 KB |
Output is correct |