forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
116 lines (103 loc) · 3.32 KB
/
B.cpp
File metadata and controls
116 lines (103 loc) · 3.32 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
using namespace std;
const int MN = 100111;
const ll MOD = 1e9 + 9;
map< pair<int,int>, int> support;
set< pair<int, pair<int,int> > > all;
map< pair<int,int>,int > exist;
pair<int,int> a[MN];
int n;
int isGood(int x0, int y0) {
if (!exist.count(make_pair(x0, y0))) return -1;
FOR(x,x0-1,x0+1) {
if (exist.count(make_pair(x, y0+1))
&& support[make_pair(x, y0+1)] == 1) {
return -1;
}
}
return exist[make_pair(x0, y0)];
}
int main() {
while (scanf("%d", &n) == 1) {
support.clear();
exist.clear();
all.clear();
REP(i,n) {
scanf("%d%d", &a[i].first, &a[i].second);
exist[a[i]] = i;
}
REP(i,n) {
if (a[i].second == 0) support[a[i]] = MN;
else {
int cnt = 0;
FOR(x,a[i].first-1,a[i].first+1)
if (exist.count(make_pair(x, a[i].second-1))) {
++cnt;
}
support[a[i]] = cnt;
}
// cout << a[i].first << ' ' << a[i].second << ' ' << support[a[i]] << endl;
}
REP(i,n) {
if (isGood(a[i].first, a[i].second) == i) {
all.insert(make_pair(i, a[i]));
// DEBUG(i);
}
}
ll res = 0;
REP(turn,n) {
ll cur = 0;
int x, y;
if (turn % 2 == 0) {
auto it = all.end(); --it;
cur = it->first;
x = it->second.first;
y = it->second.second;
all.erase(it);
}
else {
auto it = all.begin();
cur = it->first;
x = it->second.first;
y = it->second.second;
all.erase(it);
}
exist.erase(make_pair(x, y));
// DEBUG(cur);
// Support can become available
FOR(xx,x-1,x+1) {
int t = isGood(xx, y-1);
if (t >= 0) {
all.insert(make_pair(t, make_pair(xx, y-1)));
}
}
// Support count needs to be changed
FOR(xx,x-1,x+1) {
if (exist.count(make_pair(xx, y+1))) {
support[make_pair(xx, y+1)] -= 1;
}
}
// Neighbours can become unavailable
FOR(xx,x-2,x+2) {
if (exist.count(make_pair(xx, y))) {
int t = exist[make_pair(xx, y)];
if (isGood(xx, y) < 0) {
all.erase(make_pair(t, make_pair(xx, y)));
}
}
}
res = (res * n + cur) % MOD;
}
cout << res << endl;
}
return 0;
}