题解:P10639 BZOJ4695 最佳女选手
xiao7_Mr_10_ · · 题解
好的如你所见,这道题一共有六种合情合理的操作其中有四个操作非常常见,想必不用多说,但是剩下两个就不太常见了。
需要维护这两个操作怎么整?直接分块。
当然我们对于这一类问题有专门的算法:吉司机线段树。
具体的洛谷好像目前有五道相关的题目,可以去做一下,这道题就是其中之一。
不会的移步到这里。
对于吉司机线段树我们通常维护最值和严格次最值以及最值个数,这道题最大值和最小值都要维护。
具体的,维护最大值、最小值、最大值出现个数、最小值出现个数、严格次大值、严格次小值等,然后再维护三个标记辅助修改。加法标记的优先级最高,剩下两种标记优先级可以认为是相同的。
然后注意一个很重要的问题,这样你可能交一发只有七十分。
至于为什么会这样呢,如果一个区间里只有两个完全不同的数那么最大值同时也是严格次小值,而全都是一个数就会导致最大值等于最小值。所以对于这种情况我们要特殊考虑,在修改某一个值的时候同时考虑修改相同的其他值,这是一个坑点。
所以我们需要特判一下这种情况,无论是下传最大标记或者最小标记。
具体实现细节很多,直接讲十分干涩,不如结合注释看看到底是个怎么个事。
#include <bits/stdc++.h>
using namespace std;
//啊!救命!有变态
#define int long long
const int N=5e5+5,inf=1e18;
int n,a[N];
struct tree{
int mx,mx2,mn,mn2,cmx,cmn,tmx,tmn,tag,sum;//最大值,严次大,最小值,严次小,最大值个数,最小值个数,最小值tag,最大值tag,加法tag,区间和
}c[N<<2];
void updata(int x){
c[x].sum=c[x<<1].sum+c[x<<1|1].sum;//这个简简单单好吧
if(c[x<<1].mx==c[x<<1|1].mx){
c[x].mx=c[x<<1].mx,c[x].cmx=c[x<<1].cmx+c[x<<1|1].cmx;//个数拼在一起
c[x].mx2=max(c[x<<1].mx2,c[x<<1|1].mx2);
}//最大值相等
else if(c[x<<1].mx>c[x<<1|1].mx){
c[x].mx=c[x<<1].mx,c[x].cmx=c[x<<1].cmx;
c[x].mx2=max(c[x<<1].mx2,c[x<<1|1].mx);
}//左儿子大
else{
c[x].mx=c[x<<1|1].mx,c[x].cmx=c[x<<1|1].cmx;
c[x].mx2=max(c[x<<1].mx,c[x<<1|1].mx2);
}//右儿子打
//下面同理,但是维护的是最小值
if(c[x<<1].mn==c[x<<1|1].mn){
c[x].mn=c[x<<1].mn,c[x].cmn=c[x<<1].cmn+c[x<<1|1].cmn;
c[x].mn2=min(c[x<<1].mn2,c[x<<1|1].mn2);
}
else if(c[x<<1].mn<c[x<<1|1].mn){
c[x].mn=c[x<<1].mn,c[x].cmn=c[x<<1].cmn;
c[x].mn2=min(c[x<<1].mn2,c[x<<1|1].mn);
}
else{
c[x].mn=c[x<<1|1].mn,c[x].cmn=c[x<<1|1].cmn;
c[x].mn2=min(c[x<<1].mn,c[x<<1|1].mn2);
}
}//分类讨论
void build(int x,int l,int r){
c[x].tmn=inf,c[x].tmx=-inf;//初始最值tag
if(l==r){
c[x].sum=c[x].mx=c[x].mn=a[l];
c[x].mx2=-inf,c[x].mn2=inf;
c[x].cmx=c[x].cmn=1;
return;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
updata(x);
}//建树
void pushsum(int x,int l,int r,int val){
c[x].sum+=(r-l+1ll)*val;
c[x].mx+=val,c[x].mn+=val;
if(c[x].mx2!=-inf)c[x].mx2+=val;//必须考虑次值/标记是否有的情况!
if(c[x].mn2!=inf)c[x].mn2+=val;
if(c[x].tmx!=-inf)c[x].tmx+=val;
if(c[x].tmn!=inf)c[x].tmn+=val;
c[x].tag+=val;
}//下传和
void pushmin(int x,int val){
if(c[x].mx<=val)return;//注意如果都小了没必要下传
c[x].sum+=(val-c[x].mx)*c[x].cmx;
if(c[x].mn2==c[x].mx)c[x].mn2=val;//更新次/最小,很重要
if(c[x].mn==c[x].mx)c[x].mn=val;
c[x].tmx=min(c[x].tmx,val),c[x].mx=val,c[x].tmn=val;
}//下传最小
void pushmax(int x,int val){
if(c[x].mn>val)return;
c[x].sum+=(val-c[x].mn)*c[x].cmn;
if(c[x].mx2==c[x].mn)c[x].mx2=val;//更新次/最大 ,很重要
if(c[x].mx==c[x].mn)c[x].mx=val;
c[x].tmn=max(c[x].tmn,val),c[x].mn=val,c[x].tmx=val;
}//下传最大
void pushdown(int x,int l,int r){
int mid=(l+r)>>1;
if(c[x].tag){
pushsum(x<<1,l,mid,c[x].tag);
pushsum(x<<1|1,mid+1,r,c[x].tag);
}//最高优先级
//剩下两个谁先谁后没必要
if(c[x].tmx!=-inf){
pushmax(x<<1,c[x].tmx);
pushmax(x<<1|1,c[x].tmx);
}
if(c[x].tmn!=inf){
pushmin(x<<1,c[x].tmn);
pushmin(x<<1|1,c[x].tmn);
}
c[x].tag=0,c[x].tmx=-inf,c[x].tmn=inf;
}//<--总体下传
//修改
void changesum(int x,int l,int r,int s,int t,int k){
if(l>=s&&r<=t){
pushsum(x,l,r,k);
return;
}
pushdown(x,l,r);
int mid=(l+r)>>1;
if(s<=mid)changesum(x<<1,l,mid,s,t,k);
if(t>mid)changesum(x<<1|1,mid+1,r,s,t,k);
updata(x);
}
void changemin(int x,int l,int r,int s,int t,int k){
if(c[x].mx<=k)return;
if(l>=s&&r<=t&&c[x].mx2<k){
pushmin(x,k);
return;
}
pushdown(x,l,r);
int mid=(l+r)>>1;
if(s<=mid)changemin(x<<1,l,mid,s,t,k);
if(t>mid)changemin(x<<1|1,mid+1,r,s,t,k);
updata(x);
}
void changemax(int x,int l,int r,int s,int t,int k){
if(c[x].mn>=k)return;
if(l>=s&&r<=t&&c[x].mn2>k){
pushmax(x,k);
return;
}
pushdown(x,l,r);
int mid=(l+r)>>1;
if(s<=mid)changemax(x<<1,l,mid,s,t,k);
if(t>mid)changemax(x<<1|1,mid+1,r,s,t,k);
updata(x);
}
//查答案
int querysum(int x,int l,int r,int s,int t){
if(l>=s&&r<=t)return c[x].sum;
int mid=(l+r)>>1;
pushdown(x,l,r);
if(s>mid)return querysum(x<<1|1,mid+1,r,s,t);
else{
if(t<=mid)return querysum(x<<1,l,mid,s,t);
return querysum(x<<1,l,mid,s,t)+querysum(x<<1|1,mid+1,r,s,t);
}
}
int querymax(int x,int l,int r,int s,int t){
if(l>=s&&r<=t)return c[x].mx;
int mid=(l+r)>>1;
pushdown(x,l,r);
if(s>mid)return querymax(x<<1|1,mid+1,r,s,t);
else{
if(t<=mid)return querymax(x<<1,l,mid,s,t);
return max(querymax(x<<1,l,mid,s,t),querymax(x<<1|1,mid+1,r,s,t));
}
}
int querymin(int x,int l,int r,int s,int t){
if(l>=s&&r<=t)return c[x].mn;
int mid=(l+r)>>1;
pushdown(x,l,r);
if(s>mid)return querymin(x<<1|1,mid+1,r,s,t);
else{
if(t<=mid)return querymin(x<<1,l,mid,s,t);
return min(querymin(x<<1,l,mid,s,t),querymin(x<<1|1,mid+1,r,s,t));
}
}
int m,op,l,r,x;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++)cin >> a[i];
build(1,1,n);
cin >> m;
for(int i = 1;i <= m;i++){
cin >> op >> l >> r;
if(op==1){
cin >> x;
changesum(1,1,n,l,r,x);
}
else if(op==2){
cin >> x;
changemax(1,1,n,l,r,x);
}
else if(op==3){
cin >> x;
changemin(1,1,n,l,r,x);
}
else if(op==4)cout << querysum(1,1,n,l,r) << "\n";
else if(op==5)cout << querymax(1,1,n,l,r) << "\n";
else cout << querymin(1,1,n,l,r) << "\n";
}
return 0;
}