Under construction

Still being built. Things may change

Job Scheduling

Time limit4sMemory limit1024 MB

Summary
Given N base jobs and Q queries that each add R jobs, find the maximum number of non-overlapping jobs after each query's additions.
Level

Hard8 of 10

Topics
Intervals, Dynamic programming, Binary search, Segment tree
Solved
No attempts yet

Problem

There are NN jobs. Each job is given by two integers s,ts, t (s<ts < t), meaning the job starts at time ss and ends at time tt. If the time intervals of two jobs overlap, the factory cannot perform both. (If the end time of one job equals the start time of another, the two jobs do not overlap.)

The factory wants to process as many jobs as possible. Because it can handle only one job at a time, the number of jobs it can process is limited. Instead of expanding the factory, the factory adds more jobs to the schedule.

There are QQ queries. In the ii-th query, RiR_i additional jobs are given. For each query, output the maximum number of jobs that can be processed among the N+RiN + R_i jobs.

For example, let N=4N=4 and Q=2Q=2, and let the base jobs be {(1,5),(5,10),(14,16),(2,9)}\{(1, 5), (5, 10), (14, 16), (2, 9)\}. The two queries are {(3,7),(11,14)}\{(3, 7), (11, 14)\} and {(4,8),(13,15)}\{(4, 8), (13, 15)\}. For the first query, the optimal choice is the 4 jobs {(1,5),(5,10),(11,14),(14,16)}\{(1, 5), (5, 10), (11, 14), (14, 16)\}. For the second query, the optimal choice is the 3 jobs {(1,5),(5,10),(14,16)}\{(1, 5), (5, 10), (14, 16)\}.

Input

The first line contains a natural number TT, the number of test cases (1≤T≤201 \le T \le 20). The TT test cases follow.

The first line of each test case contains the number of jobs NN (1≤N≤120 0001 \le N \le 120\,000). Each of the next NN lines contains the start time and end time s,ts, t of a job (0≤s<t≤1090 \le s < t \le 10^9).

The next line contains the number of queries QQ (1≤Q≤60 0001 \le Q \le 60\,000). Each query starts with a line containing the number of new jobs RiR_i (1≤Ri≤120 0001 \le R_i \le 120\,000, 1≤∑Ri≤120 0001 \le \sum R_i \le 120\,000). Each of the next RiR_i lines contains the start time and end time s,ts, t of a new job (0≤s<t≤1090 \le s < t \le 10^9).

The sum of NN over all test cases is at most 1 300 0001\,300\,000. The sum of ∑Ri\sum R_i over all test cases is at most 1 100 0001\,100\,000.

Output

For each test case, print a line in the form Case #CC, where CC is the test case number. Then print QQ lines, each containing the answer to one query.

Examples1

  1. Example 1

    Input
    1
    4
    1 5
    5 10
    14 16
    2 9
    2
    2
    3 7
    11 14
    2
    4 8
    13 15
    
    Expected output
    Case #1
    4
    3