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

#FT0000221. Mưa thiên thạch (MET-Meteors)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Mưa thiên thạch (MET-Meteors)

MET-Meteors

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

An alliance has nn member countries and stations around a planet. Its orbit is divided into mm sectors numbered cyclically from 11 to mm, so sector mm is adjacent to sector 11. Sector jj contains exactly one station, belonging to country ojo_j; a country may own several stations or none. Initially, no samples have been collected.

Country ii wants to collect at least pip_i meteorite samples in total from all stations that it owns.

There are kk predicted showers in chronological order. Shower tt affects all sectors from ltl_t to rtr_t in increasing cyclic order, wrapping from mm to 11 if lt>rtl_t>r_t. Every station in the affected sectors receives ata_t additional samples.

For each country, determine the earliest shower after which the total collected by its stations reaches the target. If the target is never met after all kk showers, output NIE.

Input

  • The first line contains n,mn,m, the number of countries and circular sectors.
  • The second line contains mm integers o1,…,omo_1,\ldots,o_m, giving the owner of the station in each sector.
  • The third line contains nn integers p1,…,pnp_1,\ldots,p_n, the sampling targets.
  • The fourth line contains kk, the number of predicted showers.
  • Each of the next kk lines contains lt,rt,atl_t,r_t,a_t, specifying the affected circular range and the number of samples each station in that range receives.

Output

Print nn lines. Line ii contains the earliest shower number by which country ii reaches at least pip_i samples, or NIE if it never reaches the target.

Subtasks

  • Subtask 1 (20 points): 1≤n,m,k≤101\le n,m,k\le10; 1≤oj≤n1\le o_j\le n; 1≤pi≤1091\le p_i\le10^9; 1≤lt,rt≤m1\le l_t,r_t\le m; 1≤at≤1091\le a_t\le10^9 for all valid indices.

  • Subtask 2 (30 points): 1≤n,m,k≤30001\le n,m,k\le3000; 1≤oj≤n1\le o_j\le n; 1≤pi≤1091\le p_i\le10^9; 1≤lt,rt≤m1\le l_t,r_t\le m; 1≤at≤1091\le a_t\le10^9 for all valid indices.

  • Subtask 3 (50 points): 1≤n,m,k≤3⋅1051\le n,m,k\le3\cdot10^5; 1≤oj≤n1\le o_j\le n; 1≤pi≤1091\le p_i\le10^9; 1≤lt,rt≤m1\le l_t,r_t\le m; 1≤at≤1091\le a_t\le10^9 for all valid indices.

Examples

Input

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

Output

3
2
3

Explanation

Sector ownership is [1,2,1,3,2][1,2,1,3,2], and the targets are 6,7,36,7,3. Shower 1 covers sectors 4,5,1,24,5,1,2, giving country totals (2,4,2)(2,4,2). Shower 2 covers sectors 2,32,3, changing totals to (5,7,2)(5,7,2); country 2 reaches its target here. Shower 3 covers all five sectors, yielding (7,9,3)(7,9,3); countries 1 and 3 meet their targets for the first time. Thus the three answers are 3, 2, 3.