题意:
查找区间k的后继。
思路:
直接主席树。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e5+; int tot;
int root[N];//forest_root;
struct node
{
int l,r,sz;
}tr[N*];//Log(n) //=========================================
void Init()
{
tot=;
}
int Build(int l,int r)
{
int now=++tot;
tr[now].sz=;
int mid=(l+r)>>;
if(l<r)
{
tr[now].l=Build(l,mid);
tr[now].r=Build(mid+,r);
}
return now;
}
int update(int pre,int l,int r,int x)
{
int now=++tot;
tr[now].sz=tr[pre].sz+;
tr[now].l=tr[pre].l;
tr[now].r=tr[pre].r;
if(l<r)
{
int mid=(l+r)>>;
if(x>mid)
tr[now].r=update(tr[pre].r,mid+,r,x);
else
tr[now].l=update(tr[pre].l,l,mid,x);
}
return now;
}
int Query_min(int u,int v,int l,int r,int k)
{
if(tr[v].sz-tr[u].sz==)return ;
if(l==r)return l<k?l:;
int mid=(l+r)>>;
if(k<=mid+||tr[tr[v].r].sz-tr[tr[u].r].sz==)
return Query_min(tr[u].l,tr[v].l,l,mid,k);
int t=Query_min(tr[u].r,tr[v].r,mid+,r,k);
if(t)return t;
return Query_min(tr[u].l,tr[v].l,l,mid,k);
}
int Q(int u,int v,int l,int r,int k)
{
if(l==r)return tr[v].sz-tr[u].sz;
int mid=(l+r)>>;
if(k<=mid)
return Q(tr[u].l,tr[v].l,l,mid,k);
else
return Q(tr[u].r,tr[v].r,mid+,r,k);
}
void check(int u,int v,int n)
{
for(int i=;i<=n;++i)
pr("%d ",Q(u,v,,n,i));
pr("\n");
} int a[N];
int pos[N];
int ans[N];
int main()
{
int T;
sc("%d",&T);
while(T--)
{
int n,k;
sc("%d%d",&n,&k);
Init();
root[]=Build(,n);
for(int i=;i<=n;++i)
{
sc("%d",&a[i]);ans[i]=;pos[a[i]]=i;
root[i]=update(root[i-],,n,a[i]);
}
for(int i=;i<=n;++i)
{
int t1=-;
if(pos[i]!=)
{
// pr("%d %d\n",max(pos[i]-k-1,0),pos[i]-1);
// check(root[max(pos[i]-k-1,0)],root[pos[i]-1],n);
int tt=Query_min(root[max(pos[i]-k-,)],root[pos[i]-],,n,i);
ans[i]=ans[tt]+;
t1=tt;
}
if(pos[i]!=n)
{
// pr("%d %d\n",pos[i],min(pos[i]+k,n));
// check(root[pos[i]],root[min(pos[i]+k,n)],n);
int tt=Query_min(root[pos[i]],root[min(pos[i]+k,n)],,n,i);
if(tt>t1)
ans[i]=ans[tt]+;
}
pr("%d%c",ans[i],i==n?'\n':' ');
}
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}