Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2)。其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y右边。 指令保证合法,即X不等于Y。 例如,初始状态1,2,3,4,5,6的小球执行1 1 4后,小球1被移动到小球4的左边,即2,3,1,4,5,6。如果再执行2 3 5,结点3将会移到5的右边,即2,1,4,5,3,6。
Input
第一行为一个整数t(0<t<10),表示测试用例个数。每个测试用例的第一行为两个整数n(1<n<=500000)和m(0<m<100000),n表示小球的个数,m为指令条数,以下m行每行为一条指令。
Output
为每个测试用例单独输出一行,从左到右输出最后序列,每个数字后面跟一个空格。
Sample Input
2 6 2 1 1 4 2 3 5 5 1 2 1 5
Sample Output
2 1 4 5 3 6 2 3 4 5 1// Problem#: 1934 // Submission#: 5041862 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include<iostream> #include<algorithm> using namespace std; struct Node{ int light; int right; }; Node n[500050]; int main() { int t, nn, m; int op, a, b; cin >> t; while(t--){ cin >> nn >> m; for(int i=0; i<=nn; i++){ n[i].light = i-1; n[i].right = i+1; } for(int i=0; i<m; i++){ cin >> op >> a >> b; n[n[a].light].right = n[a].right ;//a的左边的右边 = a的右边 n[n[a].right].light = n[a].light ; if(op==1){// a在b左边 a.r = b a.l = b. l n[a].right = b; n[a].light = n[b].light ; n[n[b].light].right = a; n[b].light = a; } else{ // a在b右边 n[a].light = b; n[a].right = n[b].right ; n[n[b].right ].light = a; n[b].right = a; } } int x = 0; for(int i=0; i<nn; i++){ printf("%d ",n[x].right ); x = n[x].right ; } printf("\n"); } }