题意:
查找区间k的后继。
思路:
直接主席树。
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\Users\13606\Desktop\草稿.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 23 #define fo(a,b,c) for(register int a=b;a<=c;++a) 24 #define fr(a,b,c) for(register int a=b;a>=c;--a) 25 #define mem(a,b) memset(a,b,sizeof(a)) 26 #define pr printf 27 #define sc scanf 28 #define ls rt<<1 29 #define rs rt<<1|1 30 typedef long long ll; 31 void swapp(int &a,int &b); 32 double fabss(double a); 33 int maxx(int a,int b); 34 int minn(int a,int b); 35 int Del_bit_1(int n); 36 int lowbit(int n); 37 int abss(int a); 38 //const long long INF=(1LL<<60); 39 const double E=2.718281828; 40 const double PI=acos(-1.0); 41 const int inf=(1<<30); 42 const double ESP=1e-9; 43 const int mod=(int)1e9+7; 44 const int N=(int)1e5+10; 45 46 int tot; 47 int root[N];//forest_root; 48 struct node 49 { 50 int l,r,sz; 51 }tr[N*32];//Log(n) 52 53 //========================================= 54 void Init() 55 { 56 tot=0; 57 } 58 int Build(int l,int r) 59 { 60 int now=++tot; 61 tr[now].sz=0; 62 int mid=(l+r)>>1; 63 if(l<r) 64 { 65 tr[now].l=Build(l,mid); 66 tr[now].r=Build(mid+1,r); 67 } 68 return now; 69 } 70 int update(int pre,int l,int r,int x) 71 { 72 int now=++tot; 73 tr[now].sz=tr[pre].sz+1; 74 tr[now].l=tr[pre].l; 75 tr[now].r=tr[pre].r; 76 if(l<r) 77 { 78 int mid=(l+r)>>1; 79 if(x>mid) 80 tr[now].r=update(tr[pre].r,mid+1,r,x); 81 else 82 tr[now].l=update(tr[pre].l,l,mid,x); 83 } 84 return now; 85 } 86 int Query_min(int u,int v,int l,int r,int k) 87 { 88 if(tr[v].sz-tr[u].sz==0)return 0; 89 if(l==r)return l<k?l:0; 90 int mid=(l+r)>>1; 91 if(k<=mid+1||tr[tr[v].r].sz-tr[tr[u].r].sz==0) 92 return Query_min(tr[u].l,tr[v].l,l,mid,k); 93 int t=Query_min(tr[u].r,tr[v].r,mid+1,r,k); 94 if(t)return t; 95 return Query_min(tr[u].l,tr[v].l,l,mid,k); 96 } 97 int Q(int u,int v,int l,int r,int k) 98 { 99 if(l==r)return tr[v].sz-tr[u].sz; 100 int mid=(l+r)>>1; 101 if(k<=mid) 102 return Q(tr[u].l,tr[v].l,l,mid,k); 103 else 104 return Q(tr[u].r,tr[v].r,mid+1,r,k); 105 } 106 void check(int u,int v,int n) 107 { 108 for(int i=1;i<=n;++i) 109 pr("%d ",Q(u,v,1,n,i)); 110 pr(" "); 111 } 112 113 int a[N]; 114 int pos[N]; 115 int ans[N]; 116 int main() 117 { 118 int T; 119 sc("%d",&T); 120 while(T--) 121 { 122 int n,k; 123 sc("%d%d",&n,&k); 124 Init(); 125 root[0]=Build(1,n); 126 for(int i=1;i<=n;++i) 127 { 128 sc("%d",&a[i]);ans[i]=0;pos[a[i]]=i; 129 root[i]=update(root[i-1],1,n,a[i]); 130 } 131 for(int i=1;i<=n;++i) 132 { 133 int t1=-1; 134 if(pos[i]!=1) 135 { 136 // pr("%d %d ",max(pos[i]-k-1,0),pos[i]-1); 137 // check(root[max(pos[i]-k-1,0)],root[pos[i]-1],n); 138 int tt=Query_min(root[max(pos[i]-k-1,0)],root[pos[i]-1],1,n,i); 139 ans[i]=ans[tt]+1; 140 t1=tt; 141 } 142 if(pos[i]!=n) 143 { 144 // pr("%d %d ",pos[i],min(pos[i]+k,n)); 145 // check(root[pos[i]],root[min(pos[i]+k,n)],n); 146 int tt=Query_min(root[pos[i]],root[min(pos[i]+k,n)],1,n,i); 147 if(tt>t1) 148 ans[i]=ans[tt]+1; 149 } 150 pr("%d%c",ans[i],i==n?' ':' '); 151 } 152 } 153 return 0; 154 } 155 156 /**************************************************************************************/ 157 158 int maxx(int a,int b) 159 { 160 return a>b?a:b; 161 } 162 163 void swapp(int &a,int &b) 164 { 165 a^=b^=a^=b; 166 } 167 168 int lowbit(int n) 169 { 170 return n&(-n); 171 } 172 173 int Del_bit_1(int n) 174 { 175 return n&(n-1); 176 } 177 178 int abss(int a) 179 { 180 return a>0?a:-a; 181 } 182 183 double fabss(double a) 184 { 185 return a>0?a:-a; 186 } 187 188 int minn(int a,int b) 189 { 190 return a<b?a:b; 191 }