Suspicious Samples

Time limit2sMemory limit512 MB

Summary
For each condition, count samples whose value is greater or less than the min, max, or average of samples in the preceding time window.
Level

Medium6 of 10

Topics
Sliding window, Queue, Array, Implementation
Solved
No attempts yet

Problem

Fatima is a researcher. She studies water circulation in the river basins of her country. She collects samples from meteorological stations that measure various climate values, then looks for interesting patterns in those samples. Her program reads incoming sample data in real time and prints the samples that are interesting or suspicious in some way. Whether a sample counts as interesting or suspicious is decided by a fixed set of conditions, such as "the value is greater than the average of the last two hours" or "the value is lower than anything else in the last five minutes". Conditions like these are easy to put into a program.

Today Fatima doubts yesterday's results, so she came to you, an experienced programmer. She thinks her program did not evaluate the data correctly and asks you to verify its results. She brings the complete sequence of samples and describes the set of conditions. Read the samples and produce the output the conditions call for. Fatima will compare your output with the output of her program and decide what to do next.

Input

The input holds several test cases. Process it until end of file.

The first line of each test case contains one integer NN (1≤N≤1051 \le N \le 10^5), the number of samples. Each of the following NN lines describes one sample with two integers TiT_i and ViV_i (1≤Ti≤1091 \le T_i \le 10^9, 1≤Vi≤1041 \le V_i \le 10^4), meaning that the sample value ViV_i was acquired at time TiT_i. Times are given in seconds elapsed since some fixed moment in the past and form a strictly increasing sequence (Ti<TkT_i < T_k for all ii and kk with 1≤i<k≤N1 \le i < k \le N).

The next line contains one integer CC (1≤C≤101 \le C \le 10), the number of conditions to evaluate. Each of the following CC lines specifies one condition CjC_j as three tokens separated by a space.

  • A relation operator RjR_j, either gt (greater than) or lt (less than).
  • An aggregate function FjF_j, one of min (minimum), max (maximum), avg (average).
  • An integer LjL_j (1≤Lj≤1091 \le L_j \le 10^9), the length of the time interval to consider, in seconds.

A condition applied to a sample value ViV_i checks how ViV_i relates to an aggregate of the samples acquired before ViV_i. The function FjF_j picks that aggregate.

Precisely, SijS_{ij} is the set of all samples acquired before ViV_i but no more than LjL_j seconds earlier, that is, every sample kk with Ti−Lj≤Tk<TiT_i - L_j \le T_k < T_i. The sample value ViV_i satisfies condition CjC_j if and only if the relation Vi  Rj  Fj(Sij)V_i \; R_j \; F_j(S_{ij}) holds. For example, the sample value 800 together with lt min 300 reads as "is 800 less than the minimum sample value acquired in the five minutes before this 800 was obtained?". The sample ViV_i itself is not an element of SijS_{ij}. The average is the exact value, with no rounding.

The sum of NN over all test cases does not exceed 10510^5.

Output

For each test case, take the conditions in the order they are given and print one integer per condition on its own line: the number of sample values that satisfy that condition. If the time interval a condition specifies holds no samples, that condition is never counted as satisfied.

Examples2

  1. Example 1

    Input
    10
    60 30
    120 28
    180 35
    240 34
    300 40
    360 31
    420 28
    480 2
    540 42
    600 30
    2
    gt avg 7200
    lt min 300
    
    Expected output
    4
    2
    
  2. Example 2

    Input
    1
    500 77
    3
    gt avg 1000
    lt min 1000
    gt max 1
    3
    10 5
    20 5
    30 5
    4
    gt max 100
    lt min 100
    gt avg 100
    lt avg 100
    
    Expected output
    0
    0
    0
    0
    0
    0
    0