把题目分成两步来考虑。
第一步是一棵怎样的树是后手必胜的,以及后手如何走到那棵树去
第二步是把这个二维平面图转换成树。
考虑第一步,手完几个数据可以发现,当每种深度的黑色点都是偶数的时候,后手是必胜的,否则先手是必胜的。
考虑证明,当不是每种深度的点都是偶数的时候,先手可以一步走到那个状态去。
否则,无论先手如何选择,后手总有对称的选择方式(复读才是人类的本质),此时先手必败。
也就是说,后手现在的目的就是把每种深度的黑色点都变成偶数。
考虑后手现在的操作为:选择一个点,将其到根的路径上的所有点反色,我们把每种深度的黑色点的数目的奇偶性写成一个 序列,奇数为 ,偶数为 ,可以发现,后手可以用 的连续段的次数把所有的 的连续段全部消掉,而假如一开始深度为 的数就是 ,那么我们甚至还可以省一次操作(如果没有理解可以自己写一些 序列手玩一下),这样我们就将第一步分析清楚了。
考虑第二步,可以发现我们只需要建出这个图的虚树,就可以进行求解,考虑如何来建虚树,建虚树需要知道两个东西, 序和 ,知道了这两个东西之后,我们就可以用虚树板子来建了。
考虑 序,其实 序不重要,我们只需要知道它的相对顺序,容易发现这是一棵二叉树,我们钦定当一个点有两个儿子时先走 大的,现在我们要判断两个点 的 相对大小关系。
考虑每次从上一次的点向下/右扩展,假如 已经在不同子树了,那么就可以很好的判断出大小关系,否则就继续分下去,可以证明从 走到任何一个点都只需要 步,复杂度 。
求 可以用类似的思路,也是向下/右扩展,然后考虑 应该在哪边,分类讨论一下,留作练习。
总复杂度为 。
然后就可以上代码了:
#include<bits/stdc++.h>
#define N 400005
using namespace std;
int n,cnt,tot,ans,pw[32],top;
struct node {
int x,y;
node(int X=0,int Y=0) {x=X,y=Y;}
bool operator<(node a) const {return x==a.x?y<a.y:x<a.x;}
bool operator==(node a) const {return x==a.x&&y==a.y;}
} a[N][2],p[N],fp[N],stk[N];
map<node,int>mp;
map<int,int>sum;
int read() {
int s=0,f=0;char ch=getchar();
while(ch<48||ch>57)f|=(ch=='-'),ch=getchar();
while(ch>47&&ch<58)s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
return f?-s:s;
}
int Num(node x) {if(mp.find(x)==mp.end())fp[mp[x]=++tot]=x;return mp[x];}
int Check(node a,int x,int y,int i) {if(a.x<x+pw[i]&&a.y<y+pw[i])return 0;return 1+(a.x<x+pw[i]);}
bool cmp(node a,node b) {//return dfn[a]<dfn[b]
for(int i=30,x=0,y=0; ~i; --i) {
int A=Check(a,x,y,i),B=Check(b,x,y,i);
if(A==B) {if(A==1)x+=pw[i];if(A==2)y+=pw[i];}
else {
if(A==0){if(B==1)return 1;if(B==2)return a.x==x;}
if(B==0){if(A==1)return 0;if(A==2)return b.x!=x;}return A>B;
}
} return 0;
}
node Lca(node a,node b,int x=0,int y=0) {
for(int i=30; ~i; --i) {
int A=Check(a,x,y,i),B=Check(b,x,y,i);
if(A==B) {if(A==1)x+=pw[i];if(A==2)y+=pw[i];}
else {
if(A+B==3)return node(x,y);if(B==0)swap(A,B),swap(a,b);
if(B==1)b=node(x+pw[i]-1,y);if(B==2)b=node(x,y+pw[i]-1);
}
} return a;
}
int h[N],nx[N<<1],to[N<<1],s[N],s1[N],ed;
void Addedge(int x,int y) {nx[++ed]=h[x],to[h[x]=ed]=y;}
void BWT() {
#define Work() Addedge(Num(stk[top-1]),Num(stk[top])),--top
#define Do(x) !(x==stk[top])&&(stk[++top]=x,1)
Num(stk[top=1]=node(0,0));
for(int i=1; i<=cnt; ++i)if(!(p[i]==stk[top])) {
node lca=Lca(p[i],stk[top]);if(lca==stk[top]){stk[++top]=p[i];continue;}
while(cmp(lca,stk[top-1]))Work();Addedge(Num(lca),Num(stk[top--])),Do(lca),Do(p[i]);
} while(top>1)Work();
}
void Dfs(int x,int p) {
for(int i=h[x]; i; i=nx[i])Dfs(to[i],x),s[x]+=s[to[i]];
if(s[x])sum[fp[x].x+fp[x].y]^=1,sum[fp[x].x+fp[x].y+1]^=1;s[x]+=s1[x];
if(s[x]&&p)sum[fp[p].x+fp[p].y+1]^=1,sum[fp[x].x+fp[x].y]^=1;
}
int main() {
for(int i=pw[0]=1; i<=30; ++i)pw[i]=1<<i;
for(int i=n=read(),xa,xb,ya,yb; i; --i) {
xa=read(),ya=read(),xb=read(),yb=read();
p[++cnt]=a[i][0]=node(xa,ya),p[++cnt]=a[i][1]=node(xb,yb);
} sort(p+1,p+cnt+1,cmp),BWT();
for(int i=1; i<=n; ++i)++s[Num(a[i][0])],++s[Num(a[i][1])],s1[Num(Lca(a[i][0],a[i][1]))]-=2;
Dfs(1,0);for(map<int,int>::iterator it=sum.begin(); it!=sum.end(); ++it)ans+=it->second;cout<<ans-sum[0];
return 0;
}