Under construction

Still being built. Things may change

Meat Party

Time limit3sMemory limit1024 MB

Summary
Given meat pieces as intervals with tastiness and skewer pairs, output for each person the total tastiness of pieces both skewers pierce and no earlier person removed.
Level

Medium6 of 10

Topics
Segment tree, Intervals, Sorting
Solved
No attempts yet

Problem

Today is the day of a meat party. To suit the party, NN well-grilled pieces of meat are placed on a long grill.

Treat the grill as a line segment of length 10910^9, with the left end at coordinate 00 and the right end at coordinate 10910^9. Each piece of meat occupies a range on the grill and has a positive integer tastiness value. The ii-th piece occupies the coordinates in the interval [si,ei][s_i, e_i] and has tastiness tit_i. Several pieces may overlap.

MM people attend the party. Person 1 through person MM step up to the grill in order and each takes the meat they will eat. The process is as follows.

  • Person jj brings two long skewers and pushes them into the grill at coordinates aj+0.1a_j + 0.1 and bj+0.9b_j + 0.9 (aj≤bja_j \le b_j). A skewer pushed in at coordinate xx pierces every piece of meat that satisfies si≤x≤eis_i \le x \le e_i.
  • Then the person carries the skewers back to their seat. Every piece pierced by at least one skewer is carried away together and disappears from the grill.
  • A piece pierced by only one of the two skewers falls to the floor while being carried. Only pieces pierced by both skewers can be taken to the seat and eaten.

As the host, you want to know which meat each person ends up eating. Find the sum of tastiness values of the meat each person takes and eats. Exclude any meat that fell to the floor from the sum.

Input

The first line contains the number of meat pieces NN and the number of people MM.

The next NN lines each contain three integers sis_i, eie_i, tit_i, the interval and tastiness of the ii-th piece.

The next MM lines each contain two integers aja_j, bjb_j, the coordinates where person jj pushes the skewers.

Output

Print MM lines. On the jj-th line, print the sum of tastiness values of the meat that person jj takes and eats.

Constraints

  • All input values are integers.
  • 1≤N,M≤250 0001 \le N, M \le 250\,000
  • 0≤si<ei≤1090 \le s_i < e_i \le 10^9 (1≤i≤N1 \le i \le N)
  • 1≤ti≤1091 \le t_i \le 10^9 (1≤i≤N1 \le i \le N)
  • 0≤aj≤bj≤109−10 \le a_j \le b_j \le 10^9 - 1 (1≤j≤M1 \le j \le M)

Examples3

  1. Example 1

    Input
    5 3
    2 7 3
    5 6 9
    3 5 2
    1 3 6
    4 8 7
    3 6
    2 4
    5 5
    
    Expected output
    3
    0
    9
    
  2. Example 2

    Input
    6 3
    1 12 1
    2 11 10
    3 10 100
    4 9 1000
    5 8 10000
    6 7 100000
    1 11
    5 9
    6 8
    
    Expected output
    1
    110
    0
    
  3. Example 3

    Input
    5 2
    1 5 5
    2 6 2
    4 8 3
    5 9 4
    7 11 6
    4 5
    8 10
    
    Expected output
    5
    6