Card Collection

시간 제한4초메모리 제한1024 MB

요약
각 카드가 (강도, 비용) 두 값을 가질 때 인접한 두 카드를 최댓값 또는 최솟값으로 합치는 연산을 N-1번 수행해, M개의 목표 카드 중 얻을 수 있는 것을 판별한다.
난이도

어려움10점 중 8점

유형
그리디, 분할 정복, 정렬, 구현
정답자
아직 제출이 없습니다

문제

JOI-kun is enthusiastic about collecting cards in a card game. Each card in the card game has two integers representing its strength and cost. To obtain a new card, JOI-kun brings NN cards to a card exchange. Each card is numbered from 11 to NN. The strength of card ii (1≤i≤N1 ≤ i ≤ N) is S_iS\_i and the cost of card ii is V_iV\_i.

There are two machines available in the card exchange. If you insert two cards, A and B, into one of the machines, you will be able to receive any card C satisfying the following conditions.

  • If you use the first machine, then the strength of C must be equal to the maximum of the strength of A and B, and the cost of C must be equal to the maximum of the cost of A and B.
  • If you use the second machine, then the strength of C must be equal to the minimum of the strength of A and B, and the cost of C must be equal to the minimum of the cost of A and B.

JOI-kun plans to use the machines exactly N−1N - 1 times to obtain a new card. To do this, he lines up the NN cards in a row from card 11 to card NN. He then repeats the following operation N−1N - 1 times.

Choose two adjacent cards, exchange them with a new card using one of the machines, and place the new card where the chosen two cards were in the row before the operation.

After performing N−1N-1 operations, JOI-kun will have only one card left. The strength and cost of this card will depend on the operations he performs. JOI-kun has a list of MM cards that he wants to obtain after performing N−1N - 1 operations. The jj-th card (1≤j≤M1 ≤ j ≤ M) is represented by a pair of integers (T_j,W_j)(T\_j , W\_j), where T_jT\_j is the strength and W_jW\_j is the cost of the jj-th card. Write a program that, given information about JOI-kun’s cards and the list of cards he wants to obtain, determines all the cards in the list that he can obtain after performing N−1N - 1 operations.

입력

Read the following data from the standard input.

NN MM

S_1S\_1 V_1V\_1

S_2S\_2 V_2V\_2

⋮\vdots

S_NS\_N V_NV\_N

T_1T\_1 W_1W\_1

T_2T\_2 W_2W\_2

⋮\vdots

T_MT\_M W_MW\_M

출력

Write one line to the standard output. The output should contain the indices of all the cards in the list that JOI-kun can obtain after performing N−1N - 1 operations in increasing order.

제한

  • 2≤N≤200,0002 ≤ N ≤ 200\\, 000.
  • 1≤M≤200,0001 ≤ M ≤ 200\\, 000.
  • 1≤S_i≤1091 ≤ S\_i ≤ 10^9 (1≤i≤N1 ≤ i ≤ N).
  • 1≤V_i≤1091 ≤ V\_i ≤ 10^9 (1≤i≤N1 ≤ i ≤ N).
  • 1≤T_j≤1091 ≤ T\_j ≤ 10^9 (1≤j≤M1 ≤ j ≤ M).
  • 1≤W_j≤1091 ≤ W\_j ≤ 10^9 (1≤j≤M1 ≤ j ≤ M).
  • Given values are all integers.

예제3

  1. 예제 1

    입력
    5 3
    1 3
    2 2
    4 4
    1 3
    1 1
    2 3
    2 1
    4 4
    
    예상 출력
    1 3
    
  2. 예제 2

    입력
    2 2
    1 1
    2 2
    1 2
    2 1
    
    예상 출력
  3. 예제 3

    입력
    8 8
    5 2
    4 4
    1 3
    7 8
    3 1
    8 7
    6 5
    2 6
    1 4
    7 2
    8 8
    3 1
    5 6
    2 7
    6 3
    2 5
    
    예상 출력
    3 4 5 8