给你n个小球,从左到右编号依次为1,2,3,4,5,6.........n排成一行。现在有以下2种操作:A x y表示把编号为x小球移动到编号为y的小球的左边(和y相邻)。Q x为询问编号为x的小球左边的球号,如果x左边没有小球的话输出"cyk666"。
Input
第一行输入一个T,表示有T组测试数据。(1<=T<=100)
随后每一组测试数据第一行是两个整数N,M,其中N表示球的个数(1 随后有M行询问,第一个字符是操作类型s。
当s为'A'时,输入x,y表示把编号为x小球移动到编号为y的小球的左边。
当s为'Q'时,输入x表示询问小球x左边的球号。保证(1<=x<=N,1<=y<=N)
Output
输出每次询问的球号,如果这样的小球不存在,输出"cyk666"(不包括引号)。
Sample Input
1 6 5 A 1 2 A 1 4 A 3 5 Q 5 Q 2
Sample Output
3 cyk666
Hint
Source
#include <iostream> #include <cstdio> #include <bits/stdc++.h> using namespace std; struct node { int data; node *last,*next; }a[1000050]; int main() { int t,n,i,j,m,x,y; char c; cin>>t; while(t--) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { a[i].next = &a[i+1]; a[i].last = &a[i-1]; a[i].data = i; } while(m--) { getchar(); scanf("%c",&c); //cout<<c<<endl; if(c == 'A') { scanf("%d%d",&x,&y); a[x].next->last = a[x].last; a[x].last->next = a[x].next; a[x].last = a[y].last; a[y].last->next = &a[x]; a[x].next = &a[y]; a[y].last = &a[x]; } if(c == 'Q') { scanf("%d",&x); if(a[x].last->data == 0) printf("cyk666\n"); else printf("%d\n",a[x].last->data); } } } return 0; }