PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000013. Cập nhật dãy số (Update the Array)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cập nhật dãy số (Update the Array)

Update the Array

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array aa of nn elements indexed 00 through n−1n-1, with all values initially zero. First perform uu range-add updates, and only afterward answer a set of queries.

Each update is specified by l r v and adds vv to every element from index ll through index rr, including both endpoints. Updates may overlap, in which case their contributions accumulate.

Once all uu updates have finished, there are qq queries. Each query supplies one index pp and asks for the final value of apa_p. No additional updates occur between these queries. Both update and query indices are zero-based.

Input

  • Line 1: n u gives array length and range-update count.
  • Next uu lines: each contains l r v; add vv to every element in the inclusive zero-based interval [l,r][l,r].
  • Next line: q, the number of point queries.
  • Last qq lines: each contains one zero-based position p to query after every update.

Output

Print exactly qq lines. Line jj gives the final value at the position requested in query jj, after all uu range additions.

Subtasks

Subtask 1 (20 points):

  • 1≤n≤241\le n\le 24.
  • 1≤u≤301\le u\le 30.
  • 1≤q≤100001\le q\le 10000.
  • 0≤l≤r<n0\le l\le r<n.
  • 0≤p<n0\le p<n.
  • 0≤v≤100000\le v\le 10000.

Subtask 2 (30 points):

  • 1≤n≤12001\le n\le 1200.
  • 1≤u≤16001\le u\le 1600.
  • 1≤q≤100001\le q\le 10000.
  • 0≤l≤r<n0\le l\le r<n.
  • 0≤p<n0\le p<n.
  • 0≤v≤100000\le v\le 10000.

Subtask 3 (50 points):

  • 1≤n≤100001\le n\le 10000.
  • 1≤u≤1000001\le u\le 100000.
  • 1≤q≤100001\le q\le 10000.
  • 0≤l≤r<n0\le l\le r<n.
  • 0≤p<n0\le p<n.
  • 0≤v≤100000\le v\le 10000.

Examples

Input

5 3
0 1 7
2 4 6
1 3 2
3
0
3
4

Output

7
8
6

Explanation

There are n=5n=5 elements. The initial values are [0, 0, 0, 0, 0].

  1. Command 1 — 0 1 7: Add 77 to [0,1][0,1], giving array [7, 7, 0, 0, 0].

  2. Command 2 — 2 4 6: Add 66 to [2,4][2,4], giving array [7, 7, 6, 6, 6].

  3. Command 3 — 1 3 2: Add 22 to [1,3][1,3], giving array [7, 9, 8, 8, 6].

  4. Command 4 — 0: After all additions, index 00 contains 77; print 7.

  5. Command 5 — 3: After all additions, index 33 contains 88; print 8.

  6. Command 6 — 4: After all additions, index 44 contains 66; print 6.

The final point-query answers are printed on separate lines in the order requested.