/
MSE06H.cpp
64 lines (54 loc) · 959 Bytes
/
MSE06H.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
AUTHOR: Akhilesh Anandh
Solution for "Japan" (www.spoj.com/problems/MSE06H)
Algorithm: Binary Indexed Trees
*/
#include<cstdio>
#include<algorithm>
using namespace std;
struct e{
int st;
int en;
};
typedef struct e Edge;
typedef long long L;
Edge list[1000000];
bool comp(Edge e1,Edge e2){
if(e1.st==e2.st) return e1.en<e2.en;
return e1.st<e2.st;
}
L bit[1001];
L get(int idx){
L ans = 0;
while(idx>0){
ans += bit[idx];
idx -= (idx&-idx);
}
return ans;
}
void update(int idx,int n){
while(idx<=n){
bit[idx]++;
idx += (idx & -idx);
}
}
int main(){
int t,n,m,k,cas,i;
L ans;
scanf("%d",&t);
for(cas=1;cas<=t;cas++){
scanf("%d %d %d",&n,&m,&k);
for(i=1;i<=m;i++) bit[i] = 0;
for(i=0;i<k;i++){
scanf("%d %d",&(list[i].st),&(list[i].en));
}
sort(list,list+k,comp);
ans = 0;
for(i=0;i<k;i++){
ans += get(m-list[i].en);
update(m-list[i].en+1,m);
}
printf("Test case %d: %lld\n",cas,ans);
}
return 0;
}