PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000054. Tính trung bình MK trên luồng số (Finding MK Average)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tính trung bình MK trên luồng số (Finding MK Average)

Finding MK Average

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A stream of positive integers starts empty. Fixed parameters mm and kk define MKAverage: if the stream has fewer than mm elements, it is -1. Otherwise, take the most recent mm elements, sort them, discard the smallest kk and largest kk elements (counting duplicates individually), and floor the arithmetic mean of the remaining m−2km-2k elements.

Process operation 1 x to append xx and operation 2 to print the current MKAverage. Only the most recent mm elements are used in any calculation.

Input

  • Line 1: integers m,k,qm,k,q for the window length, discarded count per end and number of operations.
  • Next qq lines: either 1 x to append xx or 2 to query MKAverage, with no second parameter for type 2.

Output

For every type 2 query, print one integer: -1 before the stream reaches length mm, otherwise its MKAverage.

Subtasks

Subtask 1 (20 points):

  • 3≤m≤203\le m\le 20; 1≤k1\le k and 2k<m2k<m; 1≤q≤201\le q\le 20; type 1 x has 1≤x≤1051\le x\le10^5; each operation is type 1 or 2.

Subtask 2 (30 points):

  • 3≤m≤20003\le m\le 2000; 1≤k1\le k and 2k<m2k<m; 1≤q≤20001\le q\le 2000; type 1 x has 1≤x≤1051\le x\le10^5; each operation is type 1 or 2.

Subtask 3 (50 points):

  • 3≤m≤1000003\le m\le 100000; 1≤k1\le k and 2k<m2k<m; 1≤q≤1000001\le q\le 100000; type 1 x has 1≤x≤1051\le x\le10^5; each operation is type 1 or 2.

Examples

Examples 1

Input

3 1 9
1 3
1 1
2
1 10
2
1 5
1 5
1 5
2

Output

-1
3
5

Explanation

With only 3 and 1 inserted, the first query outputs -1. Adding 10 yields the sorted window [1,3,10][1,3,10]; dropping 1 and 10 leaves 3. After appending three 5s, the last three elements are [5,5,5][5,5,5]; discarding one from either end leaves 5.

Examples 2

Input

5 1 7
1 7
1 2
1 9
1 3
1 4
2
2

Output

4
4

Explanation

Sort the five appended values to [2,3,4,7,9][2,3,4,7,9]. Remove 2 and 9, leaving 3+4+7=143+4+7=14; ⌊14/3⌋=4\lfloor 14/3 \rfloor=4. No append occurs between the last two queries, so both return 4.