-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBIT.cpp
More file actions
76 lines (73 loc) · 1.77 KB
/
BIT.cpp
File metadata and controls
76 lines (73 loc) · 1.77 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Author : Manish Sharma
//
// Binary Indexed Tree (BIT)
// SPOJ : MSE06H
// http://www.spoj.com/problems/MSE06H/
#include<bits/stdc++.h>
#define s(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
using namespace std;
typedef long long int ll;
pair<ll,ll> inp[1000006];
ll paths[1004];
ll tree[4000];
ll read(ll idx)
{
ll sum = 0;
while(idx > 0)
{
sum += tree[idx];
idx = idx - (idx&(-idx));
}
return sum;
}
ll update(ll idx,ll val,ll size)
{
while(idx<=size)
{
tree[idx] += val;
idx = idx + (idx&(-idx));
}
}
int main()
{
ll n,m,k,t,i,j,x,tcase;
s(t);
for(tcase=1;tcase<=t;tcase++)
{
s(n);s(m);s(k);
for(i=0;i<k;i++)
{
s(inp[i].first);
s(inp[i].second);
}
sort(inp,inp+k);
memset(paths,0,sizeof(paths));
memset(tree,0,sizeof(tree));
for(i=0;i<k;i++)paths[inp[i].first]++;
for(i=1;i<=n;i++)paths[i] += paths[i-1];
ll powof2 = 0;
while((1<<powof2)<m)
powof2 ++;
m = 1<<powof2;
ll ans = 0;
for(i=0;i<k;i++)
{
j=i;
while(j<k && inp[j].first==inp[i].first)
j++;
j--;
for(x=i;x<=j;x++)
{
n = paths[inp[x].first-1];
n -= read(inp[x].second);
ans += n;
}
for(x=i;x<=j;x++)
update(inp[x].second,1,m);
i=j;
}
printf("Test case %lld: %lld\n",tcase,ans);
}
return 0;
}