![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 using namespace std;
5 int a[10001],cnt;
6 bool cmp(int a,int b){
7 return a<b;
8 }
9 int sch(int u)
10 {
11 int low=0,high=cnt-1,mid;
12 if(u<=a[low]) return low;
13 else if(u>=a[high]) return high;
14 while(low<high)
15 {
16 mid=(low+high)>>1;
17 if(a[mid]>u) high=mid-1;
18 else if(a[mid]<u) low=mid+1;
19 else {high=low=mid;break;}
20 }
21 mid=(low+high)>>1;
22 if(a[mid]==u) return mid;
23 else if(a[mid]>u) return (a[mid]-u>=u-a[mid-1])?mid-1:mid;
24 else return (u-a[mid]>a[mid+1]-u)?mid+1:mid;
25 }
26 int main()
27 {
28 char t[10];int i,j,b,n,m,tem;
29 while(scanf("%d%d",&n,&m)!=EOF)
30 {
31 cnt=0;
32 for(i=0;i<n;i++)
33 scanf("%d",&a[cnt++]);
34 sort(a,a+n,cmp);
35 cnt=unique(a,a+cnt)-a;
36 for(i=0;i<m;i++)
37 {
38 scanf("%s%d",t,&b);
39 if(t[0]=='I')
40 {
41 j;
42 tem=sch(b); if(a[tem]==b) continue;
43 if(a[tem]<b) tem++;
44 for(j=cnt-1;j>=tem;j--) a[j+1]=a[j]; a[tem]=b;
45 cnt++;
46 }
47 else{
48 int tem=sch(b);printf("%d\n",a[tem]);
49 }
50 }
51 }
52 return 0;
53 }
二分