大致思路:
先手始终取出可拆点中的最大值,后手始终取出可拆点中的最小值
所以每次把可拆点放在最大堆和最小堆里,然后每次拆掉一个点,就类似和拓扑排序一样,更新其他点是否可拆
吐槽一下居然是mod 1e9+9..而不是+7
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define rep(i,n) for ( int i=0; i<int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<int,int> pii;
template <class T>
inline bool RD(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void PT(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) pt(x / 10);
putchar(x % 10 + '0');
}
const int N = 1e5+100;
map<pii,int>id;
pii poi[N];
int in[N];
bool vis[N];
vector<int>G[N];
priority_queue<int> qma;
priority_queue<int,vector<int>,greater<int> > qmi;
bool ok(int u,int v){
int sx = poi[u].X, sy = poi[u].Y;
int x = poi[v].X, y = poi[v].Y;
if( sx == x-1 && sy == y-1 ) return true;
if( sx == x && sy == y-1) return true;
if( sx == x+1 && sy == y-1 ) return true;
return false;
}
bool judge(int u){
foreach(it,G[u]){
int v = *it;
if( vis[v] || poi[v].Y == 0 || ok(v,u) ) continue;
if( in[v] == 1) return false;
}
return true;
}
vector<int> ans;
bool inqmi[N],inqma[N];
const ll mod = 1e9+9;
int main(){
int m;
cin>>m;
int n = m;
rep(i,n){
int x,y;
RD(x),RD(y);
id[pii(x,y)] = i;
poi[i] = pii(x,y);
}
rep(u,n){
int x = poi[u].X, y = poi[u].Y;
if( id.count(pii(x-1,y-1)) ){
int v = id[pii(x-1,y-1)];
G[u].push_back(v);
G[v].push_back(u);
in[u]++;
}
if( id.count(pii(x,y-1))){
int v = id[pii(x,y-1)];
G[u].push_back(v);
G[v].push_back(u);
in[u]++;
}
if( id.count(pii(x+1,y-1)) ){
int v = id[pii(x+1,y-1)];
G[u].push_back(v);
G[v].push_back(u);
in[u]++;
}
}
rep(i,n){
if( judge(i) ) {
qmi.push(i),qma.push(i);
inqmi[i] = inqma[i] = 1;
}
}
REP(CNT,n){
if( CNT&1 ){
int u;
while(true){
u = qma.top();
qma.pop();
inqma[u] = 0;
if( vis[u] ) continue;
if( judge(u) ) break;
}
vis[u] = 1;
ans.push_back(u);
foreach(it,G[u]) {
int v = *it;
if( vis[v] ) continue;
if( ok(u,v) ) in[v]--;
else{
if(judge(v)) {
if( !inqma[v] ) qma.push(v),inqma[v] = 1;
if( !inqmi[v] ) qmi.push(v),inqmi[v] = 1;
}
}
}
}else{
int u;
while(true){
u = qmi.top();
qmi.pop();
inqmi[u] = 0;
if( vis[u]) continue;
if( judge(u) ) break;
}
ans.push_back(u);
vis[u] = 1;
foreach(it,G[u]){
int v = *it;
if( vis[v] ) continue;
if( ok(u,v) ) in[v]--;
else{
if(judge(v)){
if( !inqma[v] ) qma.push(v),inqma[v] = 1;
if( !inqmi[v] ) qmi.push(v),inqmi[v] = 1;
}
}
}
}
}
ll res = 0;
ll tmp = 1;
reveach(it,ans){
res = (res+tmp*(*it))%mod;
tmp = tmp*(ll)m%mod;
}
cout<<res<<endl;
}