Torrent
Time limit2sMemory limit128 MB
Hwiwon collects all n pieces from seeds with fixed online windows at one piece per second and reports the earliest time the file is complete, or -1.
- Level
Medium7 of 10
- Topics
- Graph, Binary search, Intervals
- Solved
- No attempts yet
Problem
The torrent program Hwiwon uses splits a single file into several pieces and shares it piece by piece. While a seed that holds some of those pieces is online, Hwiwon receives pieces from that seed and puts the file together. A seed hands the pieces it holds to other users during the time it stays online.
Hwiwon wants one file that is split into pieces. Each seed comes with its online window and the pieces it holds, and the pieces held by everyone other than Hwiwon never change. Find the minimum time Hwiwon needs to receive every piece of the file.
Seeds hold different pieces and they come online at different times. Receiving one piece takes 1 second, so Hwiwon cannot receive pieces from two seeds during the same second. For example, if a seed arrives at second 0 and leaves at second 3, Hwiwon can receive at most 3 pieces from it. Hwiwon is online from second 0 onward.
All pieces are numbered from 1 to .
Input
The first line contains the number of test cases .
The first line of each test case contains the number of pieces the file is split into, (), and the number of seeds sharing the pieces, (). Each of the next lines describes one seed with the time it comes online (), the time it leaves (), the number of pieces it holds (), and then the piece numbers (, ).
Output
For each test case, print on one line the minimum time needed to receive the whole file. If no further seed comes online and some piece is still missing, print -1.