-
Notifications
You must be signed in to change notification settings - Fork 0
/
continuousmedian.cpp
47 lines (37 loc) · 926 Bytes
/
continuousmedian.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
**************************************************************
* Author : Bemen Girgis
* Contact : [email protected]
* Problem : continuousmedian Kattis
* Link : https://open.kattis.com/problems/continuousmedian
**************************************************************
* Approach:
**************************************************************
*/
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ost;
int main()
{
int tc;
scanf("%d", &tc);
while(tc--)
{
ost arr;
int n;
scanf("%d", &n);
long long unsigned sum = 0;
for(int i = 1; i <= n; ++i)
{
int val;
scanf("%d", &val);
arr.insert(val);
auto it = arr.find_by_order(arr.size() / 2);
sum += arr.size() & 1? *it : (*it + *--it) / 2;
}
printf("%llu\n", sum);
}
return 0;
}