-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathFAKEBS.cpp
84 lines (82 loc) · 2.03 KB
/
FAKEBS.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
#include <bits/stdc++.h>
using namespace std;
#define loop(i,x,n) for(int i=x;i<n;i++)
#define sf scanf
#define pf printf
int main()
{
int t,n,q;
sf("%d",&t);
while(t--)
{
sf("%d%d",&n,&q);
int arr[n+1],orig[n+1],s,cnt,flag,l,u,mid,small,big,left,right;
map<int,int> ans,sm,bi;
loop(i,1,n+1)
sf("%d",&arr[i]),orig[i]=arr[i];
sort(arr+1,arr+n+1);
loop(i,1,n+1)
{
sm[arr[i]]=i-1;
bi[arr[i]]=n-i;
}
loop(i,1,n+1)
{
s=orig[i];
left=0,right=0;
flag=0;
l=1;
u=n;
small=sm[s];
big=bi[s];
while(l<=u)
{
mid=(l+u)/2;
if(mid==i)
{
flag=1;
break;
}
else if((i<mid&&s<orig[mid]&&big>0))
u=mid-1,big--;
else if((i>mid&&s>orig[mid]&&small>0))
l=mid+1,small--;
else if(i<mid&&s>orig[mid])
{
//find an element strict greater than s
if(big>0)
{
big--;
left++;
u=mid-1;
}
else
break;
}
else if(i>mid&&s<orig[mid])
{
//find an element strict less than s
if(small>0)
{
small--;
right++;
l=mid+1;
}
else
break;
}
else
break;
}
if(flag)
ans[s]=max(left,right);
else
ans[s]=-1;
}
int temp;
while(q--)
sf("%d",&temp),
pf("%d\n",ans[temp]);
}
return 0;
}