-
Notifications
You must be signed in to change notification settings - Fork 0
/
10516_0516062.cpp
101 lines (81 loc) · 1.28 KB
/
10516_0516062.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
struct seg_node
{
seg_node(int l,int r);
void initial();
int nth(int k);
int l,r;
int rest;
seg_node *l_node, *r_node;
};
seg_node::seg_node(int l, int r)
{
this->l=l;
this->r=r;
this->rest=r-l+1;
this->l_node=NULL;
this->r_node=NULL;
initial();
}
void seg_node::initial()
{
int mid=(l+r)/2;
if(l!=r)
{
seg_node* new_l=new seg_node(l,mid);
seg_node* new_r=new seg_node(mid+1,r);
this->l_node=new_l;
this->r_node=new_r;
}
return;
}
int seg_node::nth(int k)
{
//find target
int output=0;
if(l==r)
{
this->rest=0;
return this->l;
}
if(this->l_node->rest >= k)
output=this->l_node->nth(k);
else
output=this->r_node->nth(k - this->l_node->rest);
if(output) this->rest--;
return output;
}
int store[100005];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,k;
int ans;
scanf("%d %d",&n,&k);
seg_node* start_node=new seg_node(1,n);
for(int t=1;t<=n;t++)
{
scanf("%d",&store[t]);
//printf("%d ",store[t]);
}
//printf("\n");
int step=k+1;
int next=1;
for(;n;n--)
{
step=(step+next-1)%n;
// step==0
if(!step) step=n;
ans=start_node->nth(step);
next=store[ans];
//printf("%d\n",next);
}
printf("%d\n",next);
}
return 0;
}