forked from Anmol2307/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRATING.cpp
84 lines (70 loc) · 1.51 KB
/
RATING.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
const int maxN = 300007;
int n;
int tree[maxN];
struct Triple {
int a;
int h;
int idx;
Triple () {}
};
void update (int pos, int r) {
for( ; pos < maxN; pos |= pos + 1)
tree[pos] += r;
}
int internal_sum (int pos) {
if(pos < 0)
return 0;
int res = 0;
for( ; pos >= 0; pos = (pos & (pos + 1)) - 1)
res += tree[pos];
return res;
}
int sum (int l, int r) {
return internal_sum(r) - internal_sum(l - 1);
}
Triple a[maxN], b[maxN];
int cnt1[maxN], cnt2[maxN];
bool by_first (Triple A, Triple B) {
return A.a < B.a;
}
bool by_second (Triple A, Triple B) {
return A.h < B.h;
}
int main () {
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d%d", &a[i].a, &a[i].h); a[i].idx = i;
--a[i].a;
--a[i].h;
b[i].a = a[i].a;
b[i].h = a[i].h;
b[i].idx = i;
}
std::sort(a, a + n, by_first);
std::memset(tree, 0, sizeof(tree));
for(int i = 0; i < n; ++i) {
cnt1[a[i].idx] += sum(0, a[i].h - 1);
update(a[i].h, 1);
}
std::sort(b, b + n, by_second);
std::memset(tree, 0, sizeof(tree));
for(int i = 0; i < n; ++i) {
cnt2[b[i].idx] += sum(0, b[i].a - 1);
update(b[i].a, 1);
}
std::memset(tree, 0, sizeof(tree));
for(int i = 0; i < n; ) {
int val = a[i].a;
int k = i;
while(i < n && a[i].a == val) ++i;
cnt3[a[k].idx] += sum(0, a[k].h - 1);
update(a[k].h, 1);
}
for(int i = 0; i < n; ++i)
printf("%d\n", cnt1[i] + cnt2[i] - cnt3[i]);
return 0;
}