Yosupo's Algorithm

아직 제출이 없습니다시간 제한4초메모리 제한1024 MB

문제

...Can you replicate my master thesis in 5 hours?


Yosupo

You are given NN red points and NN blue points on a two dimensional plane. The ii-th red point's coordinate is (rx_i,ry_i)(r^x\_i, r^y\_i), and its weight is rw_ir^w\_i. The ii-th blue point's coordinate is (bx_i,by_i)(b^x\_i, b^y\_i), and its weight is bw_ib^w\_i.

Process QQ queries. In the ii-th query, you are given two integers L_iL\_i and R_iR\_i, and choose a red point jj and a blue point kk with following conditions:

  • ry_j<by_kr^y\_j < b^y\_k
  • (rx_j<L_ir^x\_j < L\_i and R_i<bx_kR\_i < b^x\_k) or (L_i<rx_jL\_i < r^x\_j and bx_k<R_ib^x\_k < R\_i)

Your task is to maximize the sum of weights of the two points or report that it is impossible to select two points.

입력

Input is given from Standard Input in the following format:

NN

rx_1r^x\_1 ry_1r^y\_1 rw_1r^w\_1

\vdots

rx_Nr^x\_N ry_Nr^y\_N rw_Nr^w\_N

bx_1b^x\_1 by_1b^y\_1 bw_1b^w\_1

\vdots

bx_Nb^x\_N by_Nb^y\_N bw_Nb^w\_N

QQ

L_1L\_1 R_2R\_2

\vdots

L_QL\_Q R_QR\_Q

출력

For each query, in a line, print the maximum sum of weights of the selected points, or 1-1 if it is impossible to choose two points.

제한

  • 1N100,0001 \leq N \leq 100{,}000
  • 1Q500,0001 \leq Q \leq 500{,}000
  • 1,000,000,000rx_i,L_i1-1{,}000{,}000{,}000 \leq r^x\_i, L\_i \leq -1
  • 1bx_i,R_i1,000,000,0001 \leq b^x\_i, R\_i \leq 1{,}000{,}000{,}000
  • 1ry_i,by_i1,000,000,0001 \leq r^y\_i, b^y\_i \leq 1{,}000{,}000{,}000
  • 1rw_i,bw_i1,000,000,0001 \leq r^w\_i, b^w\_i \leq 1{,}000{,}000{,}000
  • rx_1,,rx_N,bx_1,,bx_N,L_1,,L_Q,R_1,,R_Qr^x\_1,\cdots,r^x\_N,b^x\_1,\cdots,b^x\_N,L\_1,\cdots,L\_Q,R\_1,\cdots,R\_Q are all distinct
  • ry_1,,ry_N,by_1,,by_N,r^y\_1,\cdots,r^y\_N,b^y\_1,\cdots,b^y\_N, are all distinct