1934. Move the ball Constraints
Time limit:1 secs, Memory limit:32 MB
Description
You have some small balls, numbered from left to right in three-way,..., N. You can execute two commands (1 or 2). Where 1 x y means to move the ball X to the left of the small ball y, 2 x y means to move the ball X to the right of the ball Y. The directive guarantees legality, i.e. x is not equal to Y. For example, when the initial state of the 1,2,3,4,5,6 ball executes 1 1 4, the ball 1 is moved to the left of the ball 4, the 2,3,1,4,5,6. If you do 2 3 5 again, Node 3 will move to the right of 5, which is 2,1,4,5,3,6.
Input
The first behavior is an integer t (0<t<10), which indicates the number of test cases. The first behavior of each test case is two integers n (1<n<=500000) and M (0<m<100000), n represents the number of balls, M is the number of instructions, and the following M-line each behaves as an instruction.
Output
Output a single line for each test case, outputting the last sequence from left to right, followed by a space after each number.
Sample Input
Sample Output
2 1 4 5 3 6 2 3 4 5 1
0.26s:
#include <stdio.h>int l[500005], r[500005];void do_order (int order_num) {int kind, x, y; while (order_num--) {scanf ("%d%d%d", &kind, &x, &y); R[L[X]] = r[x];//first x ball position on both sides to handle l[r[x]] = l[x]; if (kind & 1) {R[l[y]] = x;//then the new insertion position of two balls and it itself to process l[x] = l[y]; L[y] = x; R[x] = y; } else {L[r[y]] = x;//ibid. r[x] = R[y]; R[y] = x; L[x] = y; }}}void print (int n) {int temp = 0; while (n--) {temp = r[temp];//the ball to the right of each output ball is the last order of printf ("%d", temp); } printf ("\ n");} int main () {int case_num, n, Order_num; scanf ("%d", &case_num); while (case_num--) {scanf ("%d%d", &n, &order_num); R[0] = 1; for (int i = 1; I <= n; i++) {l[i] = i-1; R[i] = i + 1; } do_order (Order_num); print (n); } return 0;}
Sicily 1934. Moving the ball