Cheese

시간 제한2초메모리 제한2048 MB

요약
기록된 각 거래가 이전에 받아들인 기록과 모순되지 않는지 판정한다. 치즈 가격 차이가 지불 금액과 가장 작은 지폐로 정해지는 조건을 만족해야 한다.
난이도

어려움10점 중 8점

유형
유니온 파인드, 수학, 비트 연산
정답자
아직 제출이 없습니다

문제

Recently, a group of local farmers have begun trading their cheese products in EJOI-land. Each farmer has their own cheese worth some certain fixed cost.

In EJOI-land, exchanges are made with the help of banknotes that have face value powers of two (1,2,4,8,… )(1, 2, 4, 8, \dots).

One day, a market opens where each farmer brings some samples of the cheese they made, intending to trade it with one another. In an exchange, two farmers can trade one sample of their cheeses. Since the price of the samples from different farmers may differ, both farmers may use banknotes to balance the exchange, so that the combined value of each farmer's cheese and the money they add equals the other's.

For example, consider the following exchange between two farmers: Victor and Sanda. If Sanda's cheese is priced 22 units less than Victor's, they may have the following exchange: Sanda gives Victor an 88-unit banknote, Victor gives Sanda a 22-unit banknote and a 44-unit banknote. This exchange ensures that the exchange is balanced.

The market organizer observes all the exchanges and writes them down in her notebook. Since there are a lot of them, she struggles to remember each one completely. Sometimes, she remembers the exact amount of the exchange; other times, she only remembers a part of what the first farmer has given and the smallest banknote used to complete the rest of the exchange.

More formally, for each exchange, she wrote in her notebook ii and jj representing the indices of the farmers that were part of the exchange, AA representing the amount of money that farmer ii paid initially, and BB where:

  • B=−1B = -1 she remembers the exact amount of the exchange, meaning that after the initial payment the exchange is finalized
  • otherwise when she doesn't remember the exact amount of the trade, BB represents the value of the smallest banknote used to cover the rest of the exchange

As the organizer's friend, you're asked to review each observation in turn. If any observation clearly contradicts the existing exchange records, it should be ignored. Otherwise, regard it as valid and add it to the exchange records.

입력

The first line of input contains two integers NN and MM, representing the number of farmers and the number of exchanges at the market.

The following MM lines contain the entries in the notebook, each line containing ii, jj, AA, BB, where ii and jj represent the indices of the farmers, AA represents the amount of money that farmer ii paid initially, and BB represents the value of the smallest banknote used to balance the exchange, or B=−1B = -1, if the farmers didn't use any additional money aside from the initial amount paid.

출력

Output MM lines each corresponding to an exchange from the input. Each line has to contain 11 if the exchange is valid or 00 if it invalid.

제한

  • 2≤N,M≤5⋅1052 ≤ N,M ≤ 5 \cdot 10^5
  • 1≤i,j≤N1 ≤ i, j ≤ N, i≠ji \ne j
  • 0≤A≤2150 ≤ A ≤ 2^{15}
  • B=−1B = -1 or B=1,2,4,8,…,214,215B = 1, 2, 4, 8, \dots , 2^{14} , 2^{15}

예제1

  1. 예제 1

    입력
    4 10
    1 2 5 -1
    1 2 5 16
    2 3 0 4
    2 1 1 2
    1 3 0 8
    1 3 1 8
    2 3 16 8
    3 2 12 -1
    1 4 2 8
    4 3 1 4
    
    예상 출력
    1
    1
    1
    1
    0
    1
    0
    1
    1
    0