[BZOJ 2819]Nim

时间:2021-12-26 22:39:08

最近都忙的没空写题解了喵~

看到 1= 终于是保住了也算是一个小小的安慰吧 555……

湖北省队互测题,据说会爆栈,但 Linux 下 栈空间=内存=128M 真的吃不下?

反正我是写了个人工栈~

这似乎是我近 4 天里写的第 3 道树链剖分?

 #include <cstdio>
#include <cstring>
#include <cstdlib>
const int sizeOfPoint=;
const int sizeOfEdge=; int n, m, q;
int w[sizeOfPoint];
int f[sizeOfPoint], d[sizeOfPoint], s[sizeOfPoint];
int num, idx[sizeOfPoint], top[sizeOfPoint], son[sizeOfPoint];
inline char getch();
inline int getint();
inline void putint(int); struct edge {int point; edge * next;};
edge memory[sizeOfEdge], * port=memory;
edge * e[sizeOfPoint];
inline edge * newedge(int, edge * );
inline void link(int, int); int tot, stack[sizeOfPoint];
edge * t[sizeOfPoint];
inline void dfsTree();
inline void dfsChain(); int T[sizeOfPoint<<];
inline void build();
inline void updateItem(int, int);
inline int querySegment(int, int); inline void swap(int & , int & );
inline int queryChain(int, int); int main()
{
char ch;
int u, v; n=getint();
for (int i=;i<=n;i++)
w[i]=getint();
for (int i=;i<n;i++)
{
int u=getint(), v=getint();
link(u, v);
} dfsTree();
dfsChain();
build(); q=getint();
for (int i=;i<=q;i++)
{
ch=getch(), u=getint(), v=getint(); if (ch=='Q') putint(queryChain(u, v));
else updateItem(idx[u], v);
} return ;
}
inline char getch()
{
register char ch;
do ch=getchar(); while (ch!='Q' && ch!='C');
return ch;
}
inline int getint()
{
register int num=;
register char ch;
do ch=getchar(); while (ch<'' || ch>'');
do num=num*+ch-'', ch=getchar(); while (ch>='' && ch<='');
return num;
}
inline void putint(int num)
{
if (num>) putchar('Y'), putchar('e'), putchar('s');
else putchar('N'), putchar('o');
putchar('\n');
}
inline edge * newedge(int point, edge * next)
{
edge * ret=port++;
ret->point=point; ret->next=next;
return ret;
}
inline void link(int u, int v)
{
e[u]=newedge(v, e[u]); e[v]=newedge(u, e[v]);
}
inline void dfsTree()
{
int u; memcpy(t, e, sizeof(e));
memset(d, 0xFF, sizeof(d)); d[]=;
s[]=;
for (stack[++tot]=;tot; )
{
u=stack[tot]; edge *& i=t[u];
for ( ;i && d[i->point]>=;i=i->next);
if (i)
{
stack[++tot]=i->point;
f[i->point]=u;
d[i->point]=d[u]+;
s[i->point]=;
}
else
{
if (!f[u]) break;
s[f[u]]+=s[u];
if (s[u]>s[son[f[u]]])
son[f[u]]=u;
tot--;
}
}
}
inline void dfsChain()
{
int u; memcpy(t, e, sizeof(e));
idx[]=num=; top[]=;
for (stack[++tot]=;tot; )
{
u=stack[tot];
if (son[u] && !idx[son[u]])
{
stack[++tot]=son[u];
idx[son[u]]=++num;
top[son[u]]=top[u];
continue;
} edge *& i=t[u];
for ( ;i && idx[i->point];i=i->next);
if (i)
{
stack[++tot]=i->point;
idx[i->point]=++num;
top[i->point]=i->point;
}
else
{
if (!f[u]) break;
tot--;
}
}
}
inline void build()
{
for (m=;m<n+;m<<=);
for (int i=;i<=n;i++) T[idx[i]+m]=w[i];
for (int i=m;i>=;i--) T[i]=T[i<<]^T[i<<|];
}
inline void updateItem(int i, int t)
{
for (T[i+=m]=t, i>>=;i;i>>=) T[i]=T[i<<]^T[i<<|];
}
inline int querySegment(int l, int r)
{
int sum=;
for (l=l+m-, r=r+m+;l^r^;l>>=, r>>=)
{
if (~l&) sum^=T[l^];
if ( r&) sum^=T[r^];
}
return sum;
}
inline void swap(int & u, int & v)
{
int t=u; u=v; v=t;
}
inline int queryChain(int u, int v)
{
int sum=;
while (top[u]!=top[v])
{
if (d[top[u]]<d[top[v]]) swap(u, v);
sum^=querySegment(idx[top[u]], idx[u]);
u=f[top[u]];
}
if (d[u]>d[v]) swap(u, v);
sum^=querySegment(idx[u], idx[v]);
return sum;
}

1A 好评如潮