LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition RuleMultiplication RuleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsCross and Dot ProductLinesPolygonsPoints and PolygonsConvex Hull
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Updating the Median

EasyProblem #38
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

You never need the whole sorted list - only what sits in the middle of it.

The statistics bureau wants to publish an honest number for the average salary. The arithmetic mean turns out to be a bad choice - a handful of people with enormous salaries pull it far above what an ordinary person earns. So they switched to the median: line all the salaries up in non-decreasing order and take the one in the middle. If the number of salaries is even, there is no single middle one, so the median is the arithmetic mean of the two middle values.

For example, the median of 1,2,4,7,9 is 4, and the median of 1,2,4,5,7,9 is 4.5.

Salaries keep arriving one by one, and at any moment the bureau may ask for the median of everything reported so far. Write a program that answers every such question.

Input

First line of input will be a single integer t - the number of testcases.
First line of each testcase contains an integer q - the number of operations.
Each of the next q lines contains one operation, in one of two forms:

  • d x - a new salary x is reported;
  • m - print the median of all salaries reported so far in this testcase.

The first operation of every testcase is guaranteed to be of the form d x.

Output

For every operation m, in the order the operations appear, print on its own line the median at that moment, rounded to one decimal.

Example

Input
1
6
d 5
d 7
d 6
m
d 8
m
Output
6.0
6.5

At the first question the reported salaries are 5,7,6, which sorted give 5,6,7 - the middle one is 6. At the second question they are 5,6,7,8, so the median is the mean of the two middle values, (6+7)/2=6.5.

Constraints

1≤t≤10
1≤q≤105
the sum of q over all testcases does not exceed 2⋅105
1≤x≤109


This problem was adapted, with permission, from Ažuriranje medijane, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit