#include<bits/stdc++.h>
using namespace std;
const long long maxn=200000+10;
struct node{
long long x,y,w,l,r;
}allq[maxn];
vector<int>allqi[maxn];
long long all[maxn],q,n;
vector<long long>sn,si;
pair<long long,long long>val[maxn],fakeval[maxn],fa[maxn],mx[maxn],val2[maxn];
pair<vector<long long>,vector<long long>>fmx[maxn];
int kaf=(1<<18);
struct segment{
long long seg[(1<<19)];
void upd(int i,long long w){
while(i!=0){
seg[i]+=w;
i>>=1;
}
}
long long pors(int i,int l,int r,int tl,int tr){
if(l>r||l>tr||r<tl||tl>tr){
return 0ll;
}
if(l>=tl&&r<=tr){
return seg[i];
}
int m=(l+r)>>1;
long long ret=pors((i<<1),l,m,tl,tr);
ret+=pors((i<<1)^1,m+1,r,tl,tr);
return ret;
}
}val2f,val2s;
bool cmpn(long long a,long long b){
return allq[a].y<allq[b].y;
}
bool cmpi(long long a,long long b){
if(all[a]==all[b]){
return a<b;
}
return all[a]<all[b];
}
void callr()
{
deque<pair<long long,long long>>allind;
allind.push_back(make_pair(n+2,0));
for(int i=1;i<=n;i++){
for(auto x:allqi[i]){
int p=lower_bound(allind.begin(),allind.end(),make_pair(allq[x].y,0ll))-allind.begin();
allq[x].l=allind[p].second;
}
while((int)allind.front().first<all[i]){
if(mx[i].first==allind.front().first){
fmx[i].first.push_back(allind.front().second);
}
if(mx[i].first<allind.front().first){
mx[i].first=allind.front().first;
fmx[i].first.clear();
fmx[i].first.push_back(allind.front().second);
}
allind.pop_front();
}
fa[i].first=allind.front().second;
allind.push_front(make_pair(all[i],i));
}
allind.clear();
allind.push_back(make_pair(n+1,n+1));
for(int i=n;i>=1;i--){
for(auto x:allqi[i]){
int p=lower_bound(allind.begin(),allind.end(),make_pair(allq[x].y,0ll))-allind.begin();
allq[x].r=allind[p].second;
}
while((int)allind.front().first<all[i]){
if(mx[i].second==allind.front().first){
fmx[i].second.push_back(allind.front().second);
}
if(mx[i].second<allind.front().first){
mx[i].second=allind.front().first;
fmx[i].second.clear();
fmx[i].second.push_back(allind.front().second);
}
allind.pop_front();
}
fa[i].second=allind.front().second;
allind.push_front(make_pair(all[i],i));
}
}
long long mainres=0;
void updn(long long ind){
long long res=0,resa=0;
long long maxa=-1;
res+=val2s.pors(1,0,kaf-1,allq[ind].x,allq[ind].r-1);
// for(long long i=allq[ind].x;i<allq[ind].r;i++){
// res+=val2[i].second;
// }
maxa=-1;
res+=val2f.pors(1,0,kaf-1,allq[ind].l+1,allq[ind].x);
// for(long long i=allq[ind].x;i>allq[ind].l;i--){
// res+=val2[i].first;
// }
res+=resa+allq[ind].w;
fakeval[allq[ind].r].first=max(fakeval[allq[ind].r].first,res);
fakeval[allq[ind].l].second=max(fakeval[allq[ind].l].second,res);
mainres=max(mainres,res);
}
void upd(long long ind){
val[ind]=fakeval[ind];
long long res=0,maxa=-1;
vector<long long>v;
if(fa[ind].first!=ind-1){
v=fmx[ind].first;
if((long long)v.size()>0){
for(auto x:v){
res+=val[x].second;
}
res+=val[v.back()].first;
val[ind].first=max(val[ind].first,res);
}
}
res=0,maxa=-1;
v.clear();
if(fa[ind].second!=ind+1){
v=fmx[ind].second;
if((long long)v.size()>0){
for(auto x:v){
res+=val[x].first;
}
res+=val[v.back()].second;
val[ind].second=max(val[ind].second,res);
}
}
mainres=max(mainres,max(val[ind].first,val[ind].second));
val2[ind].first=-val2f.pors(1,0,kaf-1,fa[ind].first+1,ind-1);
val2[ind].first+=val[ind].first;
val2f.upd(ind+kaf,val2[ind].first);
val2[ind].second=-val2s.pors(1,0,kaf-1,ind+1,fa[ind].second-1);
val2[ind].second+=val[ind].second;
val2s.upd(ind+kaf,val2[ind].second);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(long long i=1;i<=n;i++){
cin>>all[i];
si.push_back(i);
}
cin>>q;
long long suma=0;
for(long long i=1;i<=q;i++){
sn.push_back(i);
cin>>allq[i].x>>allq[i].y>>allq[i].w;
allqi[allq[i].x].push_back(i);
suma+=allq[i].w;
}
callr();
sort(sn.begin(),sn.end(),cmpn);
sort(si.begin(),si.end(),cmpi);
long long i=0,j=0;
while(i<(long long)sn.size()&&j<(long long)si.size()){
if(allq[sn[i]].y<=all[si[j]]){
updn(sn[i]);
i++;
}
else{
upd(si[j]);
j++;
}
}
while(i<(long long)sn.size()){
updn(sn[i]);
i++;
}
while(j<(long long)si.size()){
upd(si[j]);
j++;
}
long long now=(long long)si.size()-1;
long long res=val[si[now]].second;
while(now!=0&&all[si[now-1]]==all[si[now]]){
now--;
res+=val[si[now]].second;
}
res+=val[si[now]].first;
mainres=max(mainres,res);
mainres=suma-mainres;
cout<<mainres<<"\n";
}
Compilation message
constellation3.cpp: In function 'void updn(long long int)':
constellation3.cpp:96:12: warning: variable 'maxa' set but not used [-Wunused-but-set-variable]
96 | long long maxa=-1;
| ^~~~
constellation3.cpp: In function 'void upd(long long int)':
constellation3.cpp:114:18: warning: variable 'maxa' set but not used [-Wunused-but-set-variable]
114 | long long res=0,maxa=-1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33116 KB |
Output is correct |
2 |
Correct |
7 ms |
33116 KB |
Output is correct |
3 |
Correct |
6 ms |
33116 KB |
Output is correct |
4 |
Correct |
5 ms |
33220 KB |
Output is correct |
5 |
Correct |
6 ms |
33200 KB |
Output is correct |
6 |
Correct |
5 ms |
33368 KB |
Output is correct |
7 |
Correct |
7 ms |
33476 KB |
Output is correct |
8 |
Correct |
6 ms |
33116 KB |
Output is correct |
9 |
Correct |
6 ms |
33116 KB |
Output is correct |
10 |
Correct |
5 ms |
33116 KB |
Output is correct |
11 |
Correct |
5 ms |
33216 KB |
Output is correct |
12 |
Correct |
5 ms |
33308 KB |
Output is correct |
13 |
Correct |
5 ms |
33188 KB |
Output is correct |
14 |
Correct |
6 ms |
33116 KB |
Output is correct |
15 |
Correct |
5 ms |
33196 KB |
Output is correct |
16 |
Correct |
5 ms |
33344 KB |
Output is correct |
17 |
Correct |
5 ms |
33116 KB |
Output is correct |
18 |
Correct |
5 ms |
33116 KB |
Output is correct |
19 |
Correct |
5 ms |
33116 KB |
Output is correct |
20 |
Correct |
6 ms |
33116 KB |
Output is correct |
21 |
Correct |
6 ms |
33116 KB |
Output is correct |
22 |
Correct |
6 ms |
33116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33116 KB |
Output is correct |
2 |
Correct |
7 ms |
33116 KB |
Output is correct |
3 |
Correct |
6 ms |
33116 KB |
Output is correct |
4 |
Correct |
5 ms |
33220 KB |
Output is correct |
5 |
Correct |
6 ms |
33200 KB |
Output is correct |
6 |
Correct |
5 ms |
33368 KB |
Output is correct |
7 |
Correct |
7 ms |
33476 KB |
Output is correct |
8 |
Correct |
6 ms |
33116 KB |
Output is correct |
9 |
Correct |
6 ms |
33116 KB |
Output is correct |
10 |
Correct |
5 ms |
33116 KB |
Output is correct |
11 |
Correct |
5 ms |
33216 KB |
Output is correct |
12 |
Correct |
5 ms |
33308 KB |
Output is correct |
13 |
Correct |
5 ms |
33188 KB |
Output is correct |
14 |
Correct |
6 ms |
33116 KB |
Output is correct |
15 |
Correct |
5 ms |
33196 KB |
Output is correct |
16 |
Correct |
5 ms |
33344 KB |
Output is correct |
17 |
Correct |
5 ms |
33116 KB |
Output is correct |
18 |
Correct |
5 ms |
33116 KB |
Output is correct |
19 |
Correct |
5 ms |
33116 KB |
Output is correct |
20 |
Correct |
6 ms |
33116 KB |
Output is correct |
21 |
Correct |
6 ms |
33116 KB |
Output is correct |
22 |
Correct |
6 ms |
33116 KB |
Output is correct |
23 |
Correct |
7 ms |
33372 KB |
Output is correct |
24 |
Correct |
7 ms |
33368 KB |
Output is correct |
25 |
Correct |
7 ms |
33880 KB |
Output is correct |
26 |
Correct |
7 ms |
33372 KB |
Output is correct |
27 |
Correct |
7 ms |
33472 KB |
Output is correct |
28 |
Correct |
9 ms |
33368 KB |
Output is correct |
29 |
Correct |
7 ms |
33372 KB |
Output is correct |
30 |
Correct |
7 ms |
33428 KB |
Output is correct |
31 |
Correct |
8 ms |
33372 KB |
Output is correct |
32 |
Correct |
8 ms |
33408 KB |
Output is correct |
33 |
Correct |
8 ms |
33368 KB |
Output is correct |
34 |
Correct |
8 ms |
33400 KB |
Output is correct |
35 |
Correct |
7 ms |
33372 KB |
Output is correct |
36 |
Correct |
7 ms |
33504 KB |
Output is correct |
37 |
Correct |
8 ms |
33372 KB |
Output is correct |
38 |
Correct |
7 ms |
33372 KB |
Output is correct |
39 |
Correct |
7 ms |
33372 KB |
Output is correct |
40 |
Correct |
8 ms |
33372 KB |
Output is correct |
41 |
Correct |
7 ms |
33624 KB |
Output is correct |
42 |
Correct |
8 ms |
33372 KB |
Output is correct |
43 |
Correct |
7 ms |
33568 KB |
Output is correct |
44 |
Correct |
8 ms |
33372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33116 KB |
Output is correct |
2 |
Correct |
7 ms |
33116 KB |
Output is correct |
3 |
Correct |
6 ms |
33116 KB |
Output is correct |
4 |
Correct |
5 ms |
33220 KB |
Output is correct |
5 |
Correct |
6 ms |
33200 KB |
Output is correct |
6 |
Correct |
5 ms |
33368 KB |
Output is correct |
7 |
Correct |
7 ms |
33476 KB |
Output is correct |
8 |
Correct |
6 ms |
33116 KB |
Output is correct |
9 |
Correct |
6 ms |
33116 KB |
Output is correct |
10 |
Correct |
5 ms |
33116 KB |
Output is correct |
11 |
Correct |
5 ms |
33216 KB |
Output is correct |
12 |
Correct |
5 ms |
33308 KB |
Output is correct |
13 |
Correct |
5 ms |
33188 KB |
Output is correct |
14 |
Correct |
6 ms |
33116 KB |
Output is correct |
15 |
Correct |
5 ms |
33196 KB |
Output is correct |
16 |
Correct |
5 ms |
33344 KB |
Output is correct |
17 |
Correct |
5 ms |
33116 KB |
Output is correct |
18 |
Correct |
5 ms |
33116 KB |
Output is correct |
19 |
Correct |
5 ms |
33116 KB |
Output is correct |
20 |
Correct |
6 ms |
33116 KB |
Output is correct |
21 |
Correct |
6 ms |
33116 KB |
Output is correct |
22 |
Correct |
6 ms |
33116 KB |
Output is correct |
23 |
Correct |
7 ms |
33372 KB |
Output is correct |
24 |
Correct |
7 ms |
33368 KB |
Output is correct |
25 |
Correct |
7 ms |
33880 KB |
Output is correct |
26 |
Correct |
7 ms |
33372 KB |
Output is correct |
27 |
Correct |
7 ms |
33472 KB |
Output is correct |
28 |
Correct |
9 ms |
33368 KB |
Output is correct |
29 |
Correct |
7 ms |
33372 KB |
Output is correct |
30 |
Correct |
7 ms |
33428 KB |
Output is correct |
31 |
Correct |
8 ms |
33372 KB |
Output is correct |
32 |
Correct |
8 ms |
33408 KB |
Output is correct |
33 |
Correct |
8 ms |
33368 KB |
Output is correct |
34 |
Correct |
8 ms |
33400 KB |
Output is correct |
35 |
Correct |
7 ms |
33372 KB |
Output is correct |
36 |
Correct |
7 ms |
33504 KB |
Output is correct |
37 |
Correct |
8 ms |
33372 KB |
Output is correct |
38 |
Correct |
7 ms |
33372 KB |
Output is correct |
39 |
Correct |
7 ms |
33372 KB |
Output is correct |
40 |
Correct |
8 ms |
33372 KB |
Output is correct |
41 |
Correct |
7 ms |
33624 KB |
Output is correct |
42 |
Correct |
8 ms |
33372 KB |
Output is correct |
43 |
Correct |
7 ms |
33568 KB |
Output is correct |
44 |
Correct |
8 ms |
33372 KB |
Output is correct |
45 |
Correct |
398 ms |
60992 KB |
Output is correct |
46 |
Correct |
375 ms |
60616 KB |
Output is correct |
47 |
Correct |
383 ms |
60612 KB |
Output is correct |
48 |
Correct |
365 ms |
60872 KB |
Output is correct |
49 |
Correct |
418 ms |
60304 KB |
Output is correct |
50 |
Correct |
374 ms |
60356 KB |
Output is correct |
51 |
Correct |
382 ms |
60616 KB |
Output is correct |
52 |
Correct |
437 ms |
60896 KB |
Output is correct |
53 |
Correct |
368 ms |
60792 KB |
Output is correct |
54 |
Correct |
409 ms |
60928 KB |
Output is correct |
55 |
Correct |
479 ms |
66776 KB |
Output is correct |
56 |
Correct |
387 ms |
65832 KB |
Output is correct |
57 |
Correct |
422 ms |
65904 KB |
Output is correct |
58 |
Correct |
352 ms |
64704 KB |
Output is correct |
59 |
Correct |
350 ms |
64824 KB |
Output is correct |
60 |
Correct |
205 ms |
68036 KB |
Output is correct |
61 |
Correct |
365 ms |
65576 KB |
Output is correct |
62 |
Correct |
439 ms |
65648 KB |
Output is correct |
63 |
Correct |
339 ms |
65852 KB |
Output is correct |
64 |
Correct |
311 ms |
64968 KB |
Output is correct |
65 |
Correct |
384 ms |
65476 KB |
Output is correct |
66 |
Correct |
333 ms |
65736 KB |
Output is correct |