# Luogu P5352 Terrible Homework

• and：判断这一位上$1$的个数是否等于子树大小
• or：判断这一位上是否有$1$
• xor：判断这一位上$1$的个数的奇偶性
• sum：大力累加起来就好了

CODE

#include<cstdio>
#include<cctype>
#define RI register int
#define CI const int&
#define Tp template <typename T>
using namespace std;
typedef long long LL;
const int N=100005,RG=30;
int n,m,val[N],x,y,z; char opt;
class FileInputOutput
{
private:
static const int S=1<<21;
#define pc(ch) (Ftop<S?Fout[Ftop++]=ch:(fwrite(Fout,1,S,stdout),Fout[(Ftop=0)++]=ch))
char Fin[S],Fout[S],*A,*B; int Ftop,pt[25];
public:
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));
}
inline void get_alpha(char& ch)
{
while (!isalpha(ch=tc()));
}
Tp inline void write(T x)
{
if (!x) return (void)(pc('0'),pc('\n')); RI ptop=0;
while (x) pt[++ptop]=x%10,x/=10; while (ptop) pc(pt[ptop--]+48); pc('\n');
}
inline void Fend(void)
{
fwrite(Fout,1,Ftop,stdout);
}
#undef tc
#undef pc
}F;
{
private:
struct splay
{
int ch[2],fa,s[RG],size,tag; bool rev;
}node[N]; int stack[N],top;
#define lc(x) node[x].ch[0]
#define rc(x) node[x].ch[1]
#define fa(x) node[x].fa
#define S(x,y) node[x].s[y]
#define SZ(x) node[x].size
#define R(x) node[x].rev
#define T(x) node[x].tag
inline void swap(int& x,int& y)
{
int t=x; x=y; y=t;
}
inline void rever(CI now)
{
swap(lc(now),rc(now)); R(now)^=1;
}
inline void upt(CI now,CI xv)
{
for (RI i=0;i<RG;++i) if ((xv>>i)&1)
S(now,i)=SZ(now)-S(now,i); val[now]^=xv; T(now)^=xv;
}
inline void pushup(CI now)
{
SZ(now)=SZ(lc(now))+SZ(rc(now))+1; for (RI i=0;i<RG;++i)
S(now,i)=S(lc(now),i)+S(rc(now),i)+((val[now]>>i)&1);
}
inline void pushdown(CI now)
{
if (R(now)) rever(lc(now)),rever(rc(now)),R(now)=0;
if (T(now)) upt(lc(now),T(now)),upt(rc(now),T(now)),T(now)=0;
}
inline int identify(CI now)
{
return rc(fa(now))==now;
}
inline void connect(CI x,CI y,CI d)
{
node[fa(x)=y].ch[d]=x;
}
inline bool isroot(CI now)
{
return lc(fa(now))!=now&&rc(fa(now))!=now;
}
inline void rotate(CI now)
{
int x=fa(now),y=fa(x),d=identify(now); if (!isroot(x)) node[y].ch[identify(x)]=now;
fa(now)=y; connect(node[now].ch[d^1],x,d); connect(x,now,d^1); pushup(x); pushup(now);
}
inline void splay(int now)
{
int t=now; while (stack[++top]=t,!isroot(t)) t=fa(t);
while (top) pushdown(stack[top--]); for (;!isroot(now);rotate(now))
t=fa(now),!isroot(t)&&(rotate(identify(now)!=identify(t)?now:t),0);
}
inline void access(int x)
{
for (int y=0;x;x=fa(y=x)) splay(x),rc(x)=y,pushup(x);
}
inline void makeroot(CI now)
{
access(now); splay(now); rever(now);
}
inline int findroot(int now)
{
for (access(now),splay(now);lc(now);now=lc(now)) pushdown(now); return splay(now),now;
}
inline void split(CI x,CI y)
{
makeroot(x); access(y); splay(y);
}
public:
inline void build(void)
{
for (RI i=1;i<=n;++i) for (RI j=0;j<RG;++j) S(i,j)=(val[i]>>j)&1;
}
{
makeroot(x); if (findroot(y)!=x) fa(x)=y;
}
inline void cut(CI x,CI y)
{
makeroot(x); if (findroot(y)==x&&fa(y)==x&&!lc(y)) rc(x)=fa(y)=0; pushup(x);
}
inline void modify_xor(CI x,CI y,CI z)
{
split(x,y); upt(y,z);
}
inline int query_and(CI x,CI y,int ret=0)
{
split(x,y); for (RI i=0;i<RG;++i)
if (S(y,i)==SZ(y)) ret|=1<<i; return ret;
}
inline int query_or(CI x,CI y,int ret=0)
{
split(x,y); for (RI i=0;i<RG;++i)
if (S(y,i)) ret|=1<<i; return ret;
}
inline int query_xor(CI x,CI y,int ret=0)
{
split(x,y); for (RI i=0;i<RG;++i)
if (S(y,i)&1) ret|=1<<i; return ret;
}
inline LL query_sum(CI x,CI y,LL ret=0)
{
split(x,y); for (RI i=0;i<RG;++i)
ret+=1LL*(1<<i)*S(y,i); return ret;
}
#undef lc
#undef rc
#undef fa
#undef S
#undef SZ
#undef R
#undef T
}LCT;
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
for (LCT.build(),i=1;i<=m;++i)
{
switch (opt)
{
case 'L':
case 'C':
LCT.cut(x,y); break;
case 'U':
}