#P17468. [ICPC 2018 Jiaozuo R] Supreme Command

[ICPC 2018 Jiaozuo R] Supreme Command

Problem Description

Lewis likes playing chess. Now he has nn rooks on the chessboard with nn rows and nn columns. All rows of the chessboard are labelled with 11 through nn from top to bottom. All columns of the chessboard are labelled with 11 through nn from left to right. All rooks are labelled with 11 through nn as well. At the very beginning, each row or column contains exactly one rook. However, Lewis allows a square with two or more rooks during the game.

Now he starts to play a game named Supreme Command. He will provide several supreme commands to all rooks. All possible commands are in the following four different formats.

  • L k: Every rook moves kk squares to the left;
  • R k: Every rook moves kk squares to the right;
  • U k: Every rook moves kk squares upward;
  • D k: Every rook moves kk squares downward.

For a Supreme Command with given number kk, if a rook, after moving less than kk squares, had arrived at a boundary (which locates in the left-most columns, right-most column, top row or bottom row) such that the rook cannot move further, it would stay there and not move outside the chessboard.

He will also have several queries about rooks. The only two possible formats about queries are listed as follows.

  • ? k: Ask the current position of the kk-th rook;
  • !: Ask how many pairs of rooks there are currently located in the same square.

Your task in this problem is to answer these queries correctly.

Input Format

The input contains several test cases, and the first line contains a positive integer TT indicating the number of test cases which is up to 10001000.

For each test case, the first line contains two integers nn which is described as above, and mm indicating the total number of supreme commands and queries, where 1≤n,m≤3×1051 \leq n, m \leq 3 \times 10^5.

Each of the following nn lines contains two integers xx and yy, describing a rook located at the intersection of the xx-th row and the yy-th column, where 1≤x,y≤n1 \leq x, y \leq n.

Then the following mm lines describe all Supreme Commands and queries in chronological order, where all given parameters kk are integers ranged from 11 to nn.

We guarantee that the sum of nn and the sum of mm in all test cases are up to 10610^6 respectively.

Output Format

For each test case, output several lines to answer all queries.

For each query of the first type ("? kk"), output a line containing two integers xx and yy, which indicate the current position of the kk-th rook is the intersection of the xx-th row and the yy-th column.You should output exactly one whitespace between these two numbers.

For each query of the second type ("!"), output a line containing an integer which indicates the number of pairs of rocks that are currently located in the same square.

1
4 9
3 4
2 1
4 2
1 3
L 2
? 1
? 2
R 1
? 1
? 3
!
U 3
!
3 2
2 1
3 3
4 2
0
3

Hint

The following figures illustrate the chessboard at the beginning and after each Supreme Commands in the sample case.

:::align{center} :::