luogu#P8065. [BalkanOI 2012] The Best Teams

[BalkanOI 2012] The Best Teams

Background

You are the national team coach, and you need to choose the best team to take part in the BOI World Cup.

Problem Description

You have NN players, numbered 1N1 \dots N. Player ii has an age ageiage_i and a skill value skilliskill_i.

The quality of a team is the sum of the skill values. However, you believe it is not acceptable to put two players with similar skill values in the same team, because they will cause conflicts.

  • We say that two players x,yx,y have similar skill values if and only if there does not exist a third player zz such that skillx<skillz<skillyskill_x<skill_z<skill_y.

The team will play TT matches. Each match has an age limit AA, which means that for any player xx taking part in this match, agexAage_x \le A. In addition, each match has a team size limit KK, meaning the number of participating players K\le K.

A player may participate multiple times.

Input Format

The first line contains an integer NN, representing the number of players.

Each of the next NN lines contains two space-separated integers agei,skilliage_i, skill_i.

The next line contains an integer TT, representing the number of matches.

Each of the next TT lines contains two integers A,KA, K, representing the age limit and the team size limit for each match.

Output Format

For each match, output the quality of the team you choose.

7
17 21
24 36
14 19
27 20
21 50
18 5
33 7
4
20 3
50 2
99 5
10 2
45
71
95
0

Hint

Constraints

Subtask #0 is the sample.

1N,T3×1051 \le N, T \le 3 \times 10^5, 1agei,skilli1091 \le age_i, skill_i \le 10^9.

For all iji \ne j, we have skilliskilljskill_i \ne skill_j.

Sample Explanation

There are 66 pairs of players with similar skill values: (6,7)(6,7), (7,3)(7,3), (3,4)(3,4), (4,1)(4,1), (1,2)(1,2), (2,5)(2,5).

In the first match, players 1,3,61,3,6 are selected.

In the second match, players 1,51,5 are selected.

In the third match, players 1,3,5,61,3,5,6 are selected.

In the fourth match, no players can be selected, because there is no player with age 10\le 10.

Translated by ChatGPT 5