Repository navigation
Expand file tree
/
Copy pathANDROUND.cpp
More file actions
135 lines (118 loc) · 3.01 KB
/
Copy pathANDROUND.cpp
File metadata and controls
135 lines (118 loc) · 3.01 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<set>
#include<vector>
#include<algorithm>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<utility>
#include<climits>
#include<queue>
#include<deque>
#define mp make_pair
#define pb push_back
#define REP(i,a,b) for(i=a;i<=b;i++)
#define FOR(i,a,b) for(i=a;i<b; i++)
#define sci(x) scanf("%d",&x)
#define sc2i(x,y) scanf("%d%d",&x,&y)
#define scli(x) scanf("%I64d", &x)
#define scl2i(x,y) scanf("%I64d%I64d",&x,&y)
#define debug(n) cout<<__LINE__<<" #n "<<n<<endl
#define debug2(a,b) cout<<__LINE__<<" #a "<<a<<" #b "<<b<<endl
#define debug3(a,b,c) cout<<__LINE__<<" #a "<<a<<" #b "<<b<<" #c "<<c<<endl
#define fill(a,x) memset(a,x,sizeof(a))
#define trace(x) cout<<__FUNCTION__<<" : "<<__LINE__<<" #x "<<x<<endl
#define trace2(x,y) cout<<__FUNCTION__<<" : "<<__LINE__<<" #x "<<x<<" #y "<<y<<endl
#define trace3(x,y,z) cout<<__FUNCTION__<<" : "<<__LINE__<<" #x "<<x<<" #y "<<y<<" #z "<<z<<endl
#define tracea(x,a,b) cout<<__FUNCTION__<<" : "<<__LINE__<<" #x "; FOR(i,a,b) cout<<x[i]<<" , " ; cout<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned int ui;
typedef long double ld;
typedef vector<int> VI;
typedef vector<long> VL;
typedef vector<long long> VLL;
inline int scan()
{
register int n=0;
register int sign=1;
register char c=getchar_unlocked();
while( (c<'0' || c>'9') && c!='-' )
c=getchar_unlocked();
if( c=='-' )
{
sign=-1;
c=getchar_unlocked();
}
while( c>='0' && c<='9' )
{
n=n*10+c-'0';
c=getchar_unlocked();
}
return n;
}
int arr[20009];
int farr[20009];
int T[100000]; // the segment tree
int k,n,t;
// the 1-ary array but the bounds are inclusive
void init(int n,int l,int r)
{
if( l==r )
{
T[n]=arr[l];
return;
}
if( l>r ) return ;
int mid=l+(r-l)/2;
init(n*2,l,mid);
init(n*2+1,mid+1,r);
T[n]= T[n*2] & T[n*2+1];
}
// [x,y] specifies the range in which we want to query
int query(int n,int l,int r,int x,int y)
{
if( l==x && r==y ) return T[n]; // the current node is within the range
if( y<l || x>r ) return -1; // out of range
int mid=l+(r-l)/2;
// trace3(n,l,r); cin>>t;
if( y<= mid ) return query(n*2,l,mid,x,y);
if( x> mid ) return query(n*2+1,mid+1,r,x,y);
else return query(n*2,l,mid,x,mid) & query(n*2+1,mid+1,r,mid+1,y);
}
int main()
{
register int i,j;
sci(t);
while(t--)
{
sc2i(n,k);
FOR(i,0,n)
sci(arr[i]);
k=min(k,n-1);
// apply the segment tree
init(1,0,n-1);
int tmp1,tmp2,tmp3;
int res=-1;
int s,e;
FOR(i,0,n) // apply this technique segment wise
{
s=(i-k+n)%n;
e=(i+k)%n;
res=-1;
if( s<i ) res&= query(1,0,n-1,s,i);
else res&= query(1,0,n-1,0,i) & query(1,0,n-1,s,n-1);
if( e>i ) res&= query(1,0,n-1,i,e);
else res&= query(1,0,n-1,i,n-1) & query(1,0,n-1,0,e);
farr[i]=res;
}
FOR(i,0,n)
cout<<farr[i]<<" ";
cout<<endl;
}
return 0;
}