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

#BS0000478. Mưa thiên thạch (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 (Meteors)

Meteors

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

The Byteotian Interstellar Union (BIU) has discovered a planet subject to frequent meteor showers. For research, its orbit is partitioned into mm circular sectors numbered 11 through mm, with sector 11 immediately following sector mm. Each sector contains exactly one sampling station owned by one of the nn member countries. Station jj belongs to country ojo_j. A country may own multiple stations or none.

Initially no samples have been collected. Country ii aims to collect at least pip_i samples in total across its stations. There are kk forecast showers in chronological order. Shower tt is described by lt,rt,atl_t,r_t,a_t. Each station on the arc from sector ltl_t to sector rtr_t in increasing cyclic order receives ata_t additional samples:

  • If lt≤rtl_t\le r_t, the affected sectors are lt,lt+1,…,rtl_t,l_t+1,\ldots,r_t.
  • If lt>rtl_t>r_t, they are lt,lt+1,…,m,1,…,rtl_t,l_t+1,\ldots,m,1,\ldots,r_t.

Both endpoints are included. Samples accumulate over successive showers. For every country, determine the earliest shower by the end of which its combined samples meet its target, or report that its target cannot be reached after all kk showers.

Input

The first line contains integers n,mn,m, the numbers of countries and sectors.

The second line contains mm integers o1,…,omo_1,\ldots,o_m, identifying station owners in sector order.

The third line contains nn integers p1,…,pnp_1,\ldots,p_n, the targets in country order.

The fourth line contains integer kk, the number of forecast showers.

Each of the next kk lines, numbered t=1,…,kt=1,\ldots,k, contains integers lt,rt,atl_t,r_t,a_t: the endpoints of the affected arc and the samples delivered to each station on that arc.

Output

Print nn lines in country order. Line ii contains the earliest shower number (from 11 to kk) at which country ii meets its target, or NIE if no such shower exists.

Subtasks

  • Subtask 1 (20 points): 1≤n,m,k≤301\le n,m,k\le 30; 1≤oj≤n1\le o_j\le n (1≤j≤m1\le j\le m); 1≤pi≤1091\le p_i\le 10^9 (1≤i≤n1\le i\le n); 1≤lt,rt≤m1\le l_t,r_t\le m, 1≤at≤1091\le a_t\le 10^9 (1≤t≤k1\le t\le k).
  • Subtask 2 (30 points): 1≤n,m,k≤20001\le n,m,k\le 2000; 1≤oj≤n1\le o_j\le n (1≤j≤m1\le j\le m); 1≤pi≤1091\le p_i\le 10^9 (1≤i≤n1\le i\le n); 1≤lt,rt≤m1\le l_t,r_t\le m, 1≤at≤1091\le a_t\le 10^9 (1≤t≤k1\le t\le k).
  • Subtask 3 (50 points): 1≤n,m,k≤3000001\le n,m,k\le 300000; 1≤oj≤n1\le o_j\le n (1≤j≤m1\le j\le m); 1≤pi≤1091\le p_i\le 10^9 (1≤i≤n1\le i\le n); 1≤lt,rt≤m1\le l_t,r_t\le m, 1≤at≤1091\le a_t\le 10^9 (1≤t≤k1\le t\le k).

Examples

Input

3 5
1 3 2 1 3
10 5 7
3
4 2 4
1 3 1
3 5 2

Output

3
NIE
1

Explanation

The three targets are 10,5,710,5,7. Country 1 owns sectors 1 and 4; country 2 owns sector 3; country 3 owns sectors 2 and 5.

Shower 1 covers sectors 4, 5, 1, 2 and adds 4 samples per station. Country totals become (8,0,8)(8,0,8), so country 3 first meets its target at shower 1.

Shower 2 adds 1 sample to sectors 1, 2, 3, giving totals (9,1,9)(9,1,9). Countries 1 and 2 are still below their targets.

Shower 3 adds 2 samples to sectors 3, 4, 5, giving totals (11,3,11)(11,3,11). Country 1 first meets its target at shower 3. Country 2 finishes with 3<53<5 samples, so its answer is NIE.