Microspikes

Time limit1sMemory limit128 MB

Summary
Given interleaved per-appliance power change records with relative times, reconstruct the total power timeline and count maximal above-threshold intervals of duration 1 to S that start and end.
Level

Medium6 of 10

Topics
Simulation, Sorting, Prefix sum, Implementation
Solved
No attempts yet

Problem

In a study of domestic power use, researchers built a simulator for residential homes. For each home the software simulates the power consumption of its appliances. The goal is to identify microspikes in power usage: short intervals during which the total power consumption rises above a specified limit.

Before a simulation starts, all appliances are off (drawing no power). At various times during the simulation an appliance increases or decreases its power draw. Every time this happens the simulator emits a record containing the appliance number, the time in seconds since that appliance's previous change (or since the start of the simulation for its first change), and the change in power level in watts.

For any single appliance the records appear in chronological order. However, records for different appliances are interleaved arbitrarily, so a record for appliance AA written before a record for appliance BB does not imply that AA's event occurred before BB's.

Given a power threshold MM and a time threshold SS, a microspike is a maximal interval during which the total power PP stays strictly above MM (P>MP > M) and whose duration DD in seconds satisfies 1≤D≤S1 \le D \le S. A microspike is counted only when both its start (total power rising strictly above MM) and its end (total power falling back to MM or below) are reported. Appliances that are still above the threshold when the simulation ends are never turned off, so an interval that never returns to MM or below is not counted.

Read the records for one or more simulations and count how many microspikes occur in each.

Input

The input describes one or more simulations. Each simulation begins with a line holding three integers TT, MM, and SS separated by single spaces, where TT is the total simulation time in seconds (0≤T≤1000000 \le T \le 100000), MM is the power threshold (0≤M≤1090 \le M \le 10^9), and SS is the time threshold (1≤S≤10001 \le S \le 1000).

The header line is followed by up to NN power-change lines (0≤N≤10000000 \le N \le 1000000). Each holds three integers aa, tt, and pp separated by single spaces: aa is the appliance number (1≤a≤1000001 \le a \le 100000); tt is the time in seconds since that appliance's previous change (0≤t≤T0 \le t \le T); and pp is the change in power level in watts (−10000≤p≤10000-10000 \le p \le 10000). A line 0 0 0 marks the end of the power-change lines for the current simulation.

A simulation header line equal to 0 0 0 ends the entire input. You may assume that the power level of any single appliance never becomes negative and that the total power consumption never exceeds 10000000001000000000. All power changes take effect instantaneously at the start of the indicated second.

Output

For each simulation, print a single line containing the number of microspikes observed.

Note

Total power consumption for the example input. The labels on the horizontal axis mark the start of each second. All power changes take effect instantaneously at the start of a second. The bar drawn at time 1010 is not part of the simulation; it is shown only to indicate the power level that continues after the simulation ends.

Examples1

  1. Example 1

    Input
    10 100 2
    8 4 20
    8 1 30
    1 1 60
    7 2 40
    7 3 -20
    1 5 -60
    1 3 60
    7 5 -20
    8 1 -50
    8 3 50
    0 0 0
    0 0 0
    
    Expected output
    1