icpc2019 南京网络赛选拔赛 F 尺取 线段树维护区间最大值

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mp make_pair
#define pii pair<ll,ll>
#define all(x) x.begin(),x.end()
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
#define forn(i,x,g,e) for(int i=g[x];i;i=e[i].next)
#define show(x) cout<<#x<<"="<<x<<endl
#define showa(a,b) cout<<#a<<'['<<b<<"]="<<a[b]<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show4(w,x,y,z) cout<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show5(v,w,x,y,z) cout<<#v<<"="<<v<<" "<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define showmm(x,a,b) rep(i,0,a) rep(j,0,b) cout<<#x<<'['<<i<<']'<<'['<<j<<"]="<<x[i][j]<<(" 
"[j==b])
#define showm(x,a,b) rep(i,0,a) rep(j,0,b) cout<<x[i][j]<<(" 
"[j==b])
#define showa1(x,a,b) cout<<#x<<":
";rep(i,a,b) showa(x,i);cout<<endl
#define showa2(x,a,b) cout<<#x<<": ";rep(i,a,b) cout<<x[i]<<' ';cout<<endl
using namespace std;//head
const int maxn=1e5+10,maxm=2e6+10;
const ll INF=0x3f3f3f3f,mod=1e9+7;
int casn,n,m,k;
int a[maxn];
class segtree{public:
#define nd  node[now]
#define ndl node[now<<1]
#define ndr node[now<<1|1]
  struct segnode {
    int l,r,mx;
    inline int mid(){return (r+l)>>1;}
    inline int len(){return r-l+1;}
  }node[maxn<<2|3];
  void maketree(int s,int t,int now=1){
    nd={s,t,0};
    if(s==t) return ;
    maketree(s,nd.mid(),now<<1);
    maketree(nd.mid()+1,t,now<<1|1);
  }
  void update(int pos,int x,int now=1){
    if(nd.l==nd.r){
       nd.mx=x;
       return ;
    }
    if(pos<=ndl.r)update(pos,x,now<<1);
    else update(pos,x,now<<1|1);
    nd.mx=max(ndl.mx,ndr.mx);
  }
  int query(int s,int t,int now=1){
    if(s<=nd.l&&t>=nd.r) return nd.mx;
    int a=0,b=0;
    if(s<=ndl.r) a=query(s,t,now<<1);
    if(t>ndl.r) b=query(s,t,now<<1|1);
    return max(a,b);
  }
}tree;
int vis[maxn],dp[maxn];
int main() {IO;
    cin>>casn;
    while(casn--){
      cin>>n>>k;
      tree.maketree(1,n);
      rep(i,1,n) cin>>a[i];
      rep(i,1,n) vis[a[i]]=i;
      rep(i,1,n){
          int l=max(1,vis[i]-k);
          int r=min(n,vis[i]+k);
          int x=tree.query(l,r);
          dp[i]=dp[x]+1;    
          tree.update(vis[i],i);
      }
      cout<<dp[1];
      rep(i,2,n){
        cout<<' '<<dp[i];
        vis[i]=dp[i]=0;
      }
      if(casn) cout<<'
';
    }
    return 0;
}
原文地址:https://www.cnblogs.com/nervendnig/p/11453309.html