This page is still under construction.

Parts of this page are still being built. What you see may change.

Yosupo's Algorithm

Time limit4sMemory limit1024 MB

Summary
Given red and blue weighted points, answer each query by picking a red and a blue point that satisfy the y-order and L, R split conditions, maximizing their total weight.
Level

Hard8 of 10

Topics
Sorting, Segment tree
Solved
No attempts yet

Problem

...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 (rix,riy)(r^x_i, r^y_i), and its weight is riwr^w_i. The ii-th blue point's coordinate is (bix,biy)(b^x_i, b^y_i), and its weight is biwb^w_i.

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

  • rjy<bkyr^y_j < b^y_k
  • (rjx<Lir^x_j < L_i and Ri<bkxR_i < b^x_k) or (Li<rjxL_i < r^x_j and bkx<Rib^x_k < R_i)

Maximize the sum of weights of the two points, or report that it is impossible to select two points.

Input

Input is given from Standard Input in the following format:

NN

r1xr^x_1 r1yr^y_1 r1wr^w_1

⋮\vdots

rNxr^x_N rNyr^y_N rNwr^w_N

b1xb^x_1 b1yb^y_1 b1wb^w_1

⋮\vdots

bNxb^x_N bNyb^y_N bNwb^w_N

QQ

L1L_1 R1R_1

⋮\vdots

LQL_Q RQR_Q

Output

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

Constraints

  • 1≤N≤100,0001 \leq N \leq 100{,}000
  • 1≤Q≤500,0001 \leq Q \leq 500{,}000
  • −1,000,000,000≤rix,Li≤−1-1{,}000{,}000{,}000 \leq r^x_i, L_i \leq -1
  • 1≤bix,Ri≤1,000,000,0001 \leq b^x_i, R_i \leq 1{,}000{,}000{,}000
  • 1≤riy,biy≤1,000,000,0001 \leq r^y_i, b^y_i \leq 1{,}000{,}000{,}000
  • 1≤riw,biw≤1,000,000,0001 \leq r^w_i, b^w_i \leq 1{,}000{,}000{,}000
  • r1x,⋯ ,rNx,b1x,⋯ ,bNx,L1,⋯ ,LQ,R1,⋯ ,RQr^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
  • r1y,⋯ ,rNy,b1y,⋯ ,bNyr^y_1,\cdots,r^y_N,b^y_1,\cdots,b^y_N are all distinct

Examples2

  1. Example 1

    Input
    2
    -3 1 1
    -6 3 10
    3 4 100
    5 2 1000
    5
    -5 4
    -2 6
    -4 1
    -10 10
    -1 2
    
    Expected output
    101
    -1
    110
    1001
    1001
    
  2. Example 2

    Input
    10
    -389 786 414303478
    -159 301 976196121
    -268 599 754785437
    -605 652 597104844
    -199 841 214521748
    -192 8 581825989
    -515 898 509582353
    -297 36 854072992
    -489 616 41481895
    -665 876 378086770
    869 583 376652629
    509 222 380009514
    354 693 428231281
    519 738 608396032
    100 811 220629740
    960 708 928349711
    324 89 710139852
    716 897 771429659
    647 203 72269757
    368 699 540753047
    10
    -350 499
    -956 639
    -287 504
    -915 742
    -777 135
    -176 487
    -150 987
    -133 10
    -852 147
    -476 106
    
    Expected output
    1564212844
    1584592153
    1782422703
    1747625780
    1196825861
    1782422703
    -1
    1904545832
    1196825861
    1525454555