#BS0000478. Mưa thiên thạch (Meteors)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 circular sectors numbered through , with sector immediately following sector . Each sector contains exactly one sampling station owned by one of the member countries. Station belongs to country . A country may own multiple stations or none.
Initially no samples have been collected. Country aims to collect at least samples in total across its stations. There are forecast showers in chronological order. Shower is described by . Each station on the arc from sector to sector in increasing cyclic order receives additional samples:
- If , the affected sectors are .
- If , they are .
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 showers.
Input
The first line contains integers , the numbers of countries and sectors.
The second line contains integers , identifying station owners in sector order.
The third line contains integers , the targets in country order.
The fourth line contains integer , the number of forecast showers.
Each of the next lines, numbered , contains integers : the endpoints of the affected arc and the samples delivered to each station on that arc.
Output
Print lines in country order. Line contains the earliest shower number (from to ) at which country meets its target, or NIE if no such shower exists.
Subtasks
- Subtask 1 (20 points): ; (); (); , ().
- Subtask 2 (30 points): ; (); (); , ().
- Subtask 3 (50 points): ; (); (); , ().
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 . 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 , so country 3 first meets its target at shower 1.
Shower 2 adds 1 sample to sectors 1, 2, 3, giving totals . Countries 1 and 2 are still below their targets.
Shower 3 adds 2 samples to sectors 3, 4, 5, giving totals . Country 1 first meets its target at shower 3. Country 2 finishes with samples, so its answer is NIE.