Under construction

Still being built. Things may change

Maximize The Value

Time limit2sMemory limit1024 MB

Summary
For each position K and operation range [S, T], find the largest sum of X over a contiguous block of operations l to r, counting only operations whose range covers K.
Level

Medium7 of 10

Topics
Segment tree, Prefix sum, Intervals
Solved
No attempts yet

Problem

You are given a one-based array of NN integers: A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N. Initially, the value of each element is 00.

There are MM operations, numbered from 11 to MM. Operation ii is ⟨Li,Ri,Xi⟩\langle L_i, R_i, X_i \rangle. When operation ii is executed, every AjA_j with Li≤j≤RiL_i \le j \le R_i increases by XiX_i.

You must answer QQ independent queries. Each query is ⟨K,S,T⟩\langle K, S, T \rangle, which asks you to choose a range [l,r][l, r] with S≤l≤r≤TS \le l \le r \le T, then execute operations l,l+1,…,rl, l+1, \dots, r in order. The answer is the maximum value of AKA_K after the operations are executed, taken over all possible choices of ll and rr.

Input

The first line contains two integers NN and MM (1≤N,M≤100 0001 \le N, M \le 100\,000).

Each of the next MM lines contains three integers LiL_i, RiR_i, and XiX_i (1≤Li≤Ri≤N1 \le L_i \le R_i \le N; −100 000≤Xi≤100 000-100\,000 \le X_i \le 100\,000).

The next line contains an integer QQ (1≤Q≤100 0001 \le Q \le 100\,000).

Each of the next QQ lines contains three integers KK, SS, and TT (1≤K≤N1 \le K \le N; 1≤S≤T≤M1 \le S \le T \le M).

Output

For each query, print the answer on a separate line.

Examples2

  1. Example 1

    Input
    2 6
    1 1 -50
    1 2 -20
    2 2 -30
    1 1 60
    1 2 40
    2 2 10
    5
    1 1 6
    2 1 6
    1 1 3
    2 1 3
    1 1 2
    
    Expected output
    100
    50
    0
    0
    -20
    
  2. Example 2

    Input
    5 3
    1 3 3
    2 4 -2
    3 5 3
    6
    1 1 3
    2 1 3
    3 1 3
    3 2 3
    2 2 3
    2 2 2
    
    Expected output
    3
    3
    4
    3
    0
    -2