[決策單調 分治] LOJ#535. 「LibreOJ Round #6」花火

如果 i<jai>aj 那麼 i 作爲左端點比 j 優,右端點同理

那麼搞出兩個上升序列,發現右端點遞增的時候左端點也是單調上升的,也就是gjghfdvector說的具有決策單調

分治就好了

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int N=300010;

int n,cnt,a[N],Q1[N],rt[N],tot[N*20],ls[N*20],rs[N*20],top1,Q2[N],top2;

void Add(int &g,int l,int r,int x){
  int k=g; g=++cnt; ls[g]=ls[k]; rs[g]=rs[k]; tot[g]=tot[k]+1;
  if(l==r) return ;
  int mid=l+r>>1;
  if(x<=mid) Add(ls[g],l,mid,x);
  else Add(rs[g],mid+1,r,x);
}

int Query(int g,int l,int r,int L,int R){
  if((l==L && r==R) || !tot[g]) return tot[g];
  int mid=L+R>>1;
  if(r<=mid) return Query(ls[g],l,r,L,mid);
  else if(l>mid) return Query(rs[g],l,r,mid+1,R);
  else return Query(ls[g],l,mid,L,mid)+Query(rs[g],mid+1,r,mid+1,R);
}

int t[N];

inline int Qu(int x){
  int ret=0;
  for(;x<=n;x+=x&-x) ret+=t[x];
  return ret;
}

inline void Ad(int x){ for(;x;x-=x&-x) t[x]++; }

int l,r,Max,res[N],pos[N];

inline int calc(int l,int r){
  if(a[l]<a[r] || l>=r) return 0;
  return Query(rt[r],a[r],a[l]-1,1,n)-1-Query(rt[l],a[r],a[l]-1,1,n);
}

void solve(int l,int r,int L,int R){
  if(L>R) return ;
  if(l==r){
    for(int i=L;i<=R;i++){
      int cur=calc(Q1[l],Q2[i]);
      if(cur>res[i]) res[i]=cur,pos[i]=l;
    }
    return ;
  }
  int mid=L+R>>1; res[mid]=0;
  for(int i=l;i<=r;i++){
    int cur=calc(Q1[i],Q2[mid]);
    if(cur>=res[mid]) res[mid]=cur,pos[mid]=i;
  }
  solve(l,pos[mid],L,mid-1);
  solve(pos[mid],r,mid+1,R);
}

int main(){
#ifdef ljn 
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
#endif
  scanf("%d",&n);
  for(int i=1;i<=n;i++){
    scanf("%d",&a[i]); rt[i]=rt[i-1];
    Add(rt[i],1,n,a[i]);
  }
  for(int i=1;i<=n;i++){
    if(a[i]>a[Q1[top1]]) Q1[++top1]=i;
    while(top2 && a[i]<a[Q2[top2]]) top2--;
    Q2[++top2]=i;
  }
  solve(1,top1,1,top2); l=r=0;
  for(int i=1;i<=top2;i++)
    if(res[i]>Max) Max=res[i],l=Q1[pos[i]],r=Q2[i];
  swap(a[l],a[r]);
  long long ans=l!=r;
  for(int i=1;i<=n;i++) ans+=Qu(a[i]),Ad(a[i]);
  printf("%lld\n",ans);
  return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章