1st Problem Description
Input Format
- Line 1:
M N T - Next N lines: Each line represents one event:
BOOK l r p(where1 <= l <= r <= M, and1 <= p <= 10,000)QUERY l r(where1 <= l <= r <= M)
Detailed Format:
- Line 1:
M N T - Lines 2 to N+1: Each line is one event.
Output Format
- For each
QUERYevent, output its answer on its own line. - After all events are processed, output one extra line: the earliest 1-based event index at which any segment’s cumulative cost first reached or exceeded
T. If the thresholdTis never reached, output-1.
Sample Input & Explanation
Sample Input:
Plaintext
6 5 15
BOOK 1 3 5
QUERY 1 6
BOOK 4 6 10
QUERY 2 5
BOOK 3 4 6
Output:
Plaintext
0
5
5
Input Explanation:
- There are 6 segments, 5 events, and a threshold of 15.
- After Event 1: segments 1 to 3 cost 5; the rest are 0.
- After Event 2 (query): The minimum value over the range [1 to 6] is 0 (since segments 4 to 6 are still 0). Output: 0
- After Event 3: segments 4 to 6 cost 10; segments 1 to 3 cost 5.
- After Event 4 (query): The minimum value over the range [2 to 5] is the minimum of (5, 5, 10, 10), which is 5. Output: 5
- After Event 5: Add 6 to segments 3 to 4. The cost vector becomes:
[5, 5, 11, 16, 10, 10]. - Threshold 15 is first reached or exceeded by segment 4 at Event 5 (after adding 6, segment 4 becomes 16, which is >= 15). Output: 5
Examples
Example 1: Threshold Never Reached
Input:
Plaintext
4 3 100
BOOK 1 4 1
QUERY 1 4
BOOK 2 3 2
Output:
Plaintext
1
-1
Explanation: The single query returns 1 (every segment costs 1 after the first event). The maximum cost across the entire day is 3 (which is less than 100), so the threshold-event output is -1.
Example 2: All-Updates, No Queries
Input:
Plaintext
3 4 5
BOOK 1 1 3
BOOK 2 2 4
BOOK 3 3 6
BOOK 1 3 1
Output:
Plaintext
3
Explanation: Costs after each event:
- Event 1:
[3, 0, 0] - Event 2:
[3, 4, 0] - Event 3:
[3, 4, 6] - Event 4:
[4, 5, 7]
The threshold of 5 is first reached or exceeded at Event 3 (where segment 3 hits 6, which is >= 5).
Constraints
1 <= M <= 1,000,000(Number of segments)1 <= N <= 200,000(Number of events)1 <= T <= 1,000,000,000(Threshold)1 <= l <= r <= M1 <= p <= 10,000(for BOOK events)- The cumulative cost for any segment is guaranteed to fit within a 64-bit signed integer.

