#P15452. [JOI 2026 SemiFinal] 宝石商 / Jeweler

[JOI 2026 SemiFinal] 宝石商 / Jeweler

Problem Description

JOI runs a jewelry shop. There are NN customers who want to buy jewels, numbered from 11 to NN. Customer ii (1≤i≤N1 \le i \le N) can visit the shop at any time between time LiL_i and time RiR_i, and plans to buy CiC_i jewels.

JOI is very busy and cannot keep the shop open all the time. Therefore, he considers MM possible opening-time plans. The plans are numbered from 11 to MM, and plan jj (1≤j≤M1 \le j \le M) means the shop is open from time Sj−0.1S_j - 0.1 to time Tj+0.1T_j + 0.1. For each plan, customer ii (1≤i≤N1 \le i \le N) will visit the shop and buy CiC_i jewels if there exists a time when the shop is open within the time interval in which they can visit. Otherwise, if no such time exists, customer ii will not visit and will not buy any jewels. However, JOI’s shop has enough jewels, so it will never run out.

Given the customer information and each opening-time plan, write a program to compute, for each plan, the total number of jewels that can be sold.

Input Format

The input is given from standard input in the following format:

NN
L1 R1 C1L_1\ R_1\ C_1
L2 R2 C2L_2\ R_2\ C_2
⋮\vdots
LN RN CNL_N\ R_N\ C_N
MM
S1 T1S_1\ T_1
S2 T2S_2\ T_2
⋮\vdots
SM TMS_M\ T_M

Output Format

Output MM lines to standard output. On the jj-th line (1≤j≤M1 \le j \le M), output the total number of jewels that can be sold under plan jj.

3
3 4 10
5 8 20
6 10 30
3
4 6
1 2
6 8
60
0
50
4
10 90 1
40 60 2
10 20 4
80 90 8
3
1 15
1 60
1 100

5
7
15
10
55 882 861052753
104 734 331227764
492 694 240198464
481 506 377367203
131 185 327968773
124 129 970226535
92 125 133053911
356 442 758055457
21 759 730522637
259 481 948997757
9
50 287
510 735
158 431
113 768
328 894
783 881
163 692
42 862
43 752
4303050130
2163001618
3957825141
5678671254
4247422035
861052753
4575390808
5678671254
5678671254

Hint

Sample Explanation 1

In plan 1, the shop is open from time 3.93.9 to time 6.16.1. Customer 1 can come at time 44, customer 2 can come at time 55, and customer 3 can come at time 66 to buy jewels. In total, 10+20+30=6010 + 20 + 30 = 60 jewels are sold.

In plan 2, the shop is open from time 0.90.9 to time 2.12.1. No customer can visit during the opening time, so a total of 00 jewels are sold.

In plan 3, the shop is open from time 5.95.9 to time 8.18.1. Customers 2 and 3 can both come at time 77 to buy jewels. In total, 20+30=5020 + 30 = 50 jewels are sold.

This sample input satisfies the constraints for subtasks 1 and 5.

Sample Explanation 2

In plan 1, customers 1 and 3 can visit the shop to buy jewels. In total, 1+4=51 + 4 = 5 jewels are sold.

In plan 2, customers 1, 2, and 3 can visit the shop to buy jewels. In total, 1+2+4=71 + 2 + 4 = 7 jewels are sold.

In plan 3, all customers can visit the shop to buy jewels. In total, 1+2+4+8=151 + 2 + 4 + 8 = 15 jewels are sold.

This sample input satisfies the constraints for subtasks 1, 3, 4, and 5.

Constraints

  • 1≤N≤300 0001 \le N \le 300\,000
  • 1≤Li<Ri≤1 000 0001 \le L_i < R_i \le 1\,000\,000 (1≤i≤N1 \le i \le N)
  • 1≤Ci≤1091 \le C_i \le 10^9 (1≤i≤N1 \le i \le N)
  • 1≤M≤300 0001 \le M \le 300\,000
  • 1≤Sj≤Tj≤1 000 0001 \le S_j \le T_j \le 1\,000\,000 (1≤j≤M1 \le j \le M)
  • All input values are integers.

Subtasks

  1. (12 points) N≤1000, M≤1000N \le 1000,\ M \le 1000
  2. (17 points) Sj=TjS_j = T_j (1≤j≤M1 \le j \le M)
  3. (21 points) Sj=1S_j = 1 (1≤j≤M1 \le j \le M)
  4. (23 points) Sj≤Sj+1, Tj≤Tj+1S_j \le S_{j+1},\ T_j \le T_{j+1} (1≤j<M1 \le j < M)
  5. (27 points) No additional constraints.

Translated by DeepSeek.

Translated by ChatGPT 5