Can of Worms
Time limit3sMemory limit128 MB
For each can, count how many cans explode when it is shot, following the chain reaction where each blast hits cans within its radius.
- Level
Hard8 of 10
- Topics
- Sorting, Binary search, Graph, DFS
- Solved
- No attempts yet
Problem
There is an old saying about "opening a can of worms." A lesser-known one is about shooting a can of exploding worms with a BB gun.
Imagine we line up several cans of exploding worms along a long, straight fence. When a can is shot, every worm inside it explodes. Different kinds of worms have different blast radii, and each can holds only one kind of worm.
The -th can sits at position on the fence and has blast radius . When a can at position with radius explodes, every other can whose position lies within distance of it, that is, every can located in the interval , also explodes. This can set off a chain reaction. Each can explodes at most once, and the process continues until no more cans explode.
Suppose exactly one can is shot and it is the only can shot. Determine how many cans explode in total.
Input
The input contains several test cases. Each test case begins with a line containing a single integer (), the number of cans on the fence. Each of the next lines contains two integers () and (): the position of the can on the fence and its blast radius. No two cans share the same position.
The input ends with a line containing a single .
Output
For each test case, print integers on a single line, separated by single spaces. The -th integer is the number of cans that explode when the -th can (in input order) is the one that is shot. Do not print any extra spaces, and do not print blank lines between test cases.