#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int n,q,x[10009];
map<int,int> pos;
map<int,int> lst;
map<int,int> nxt;
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++) scanf("%d",&x[i]);
x[0]=x[n+1]=-1;
for(int i=1;i<=n;i++){
pos[x[i]]=i;
lst[x[i]]=x[i-1];
nxt[x[i]]=x[i+1];
}
for(int i=1;i<=q;i++){
char op[10];
int num;
scanf("%s",op);
scanf("%d",&num);
if(op[0]=='l') printf("%d\n",lst[num]);
else if(op[0]=='n') printf("%d\n",nxt[num]);
else printf("%d\n",pos[num]);
}
return 0;
}
给定一个长度为 n 的数列 A,共 q 次询问,询问共有以下几种可能。
next x 询问 x 的下一个数,若不存在则输出 -1。find x 询问 x 的的位置,即下标。last x 询问 x 的上一个数,若不存在则输出 -1。请使用 scanf 和 printf。
第一行两个数 n,q。
接下来 n 行,每行一个数,表示数列 A。
接下来 q 行,每行一个询问。
共 q 行,每行一个回答。
10 6
15538464
13113120
85191840
33712000
78517152
3277868
13486336
42383105
75222000
8097610
next 15538464
next 42383105
next 75222000
next 33712000
last 15538464
last 13486336
13113120
75222000
8097610
78517152
-1
3277868
10 3
68295042
4493695
49152422
12199264
19215200
45180343
8882610
27564128
35466564
66708846
find 68295042
last 8882610
next 4493695
1
45180343
49152422
对于 100% 的数据,保证 1≤n≤10000,1≤q≤1000000,x∈A,0≤Ai≤100000000,且每个 Ai 的值唯一,不存在重复的情况。