Luogu P5352 Terrible Homework
神仙@TheLostWeak出的题,因为他最近没时间所以我先写一下sol(其实我也没什么时间)
作为一道简单的数据结构题想必大家都能看出必须用LCT维护信息吧
一个朴素的想法就是直接维护四种操作的值,但是这样修改除了异或好像都不能维护
既然一个\(\log\)不可行那么就大力两个\(\log\)吧,很容易想到直接把信息拆分成二进制来存,然后记一下子树内所有数每一位上\(1\)的个数和
同时我们再维护一个子树大小,那么对于每种询问时直接大力枚举每一位的情况判断即可:
and
:判断这一位上\(1\)的个数是否等于子树大小or
:判断这一位上是否有\(1\)xor
:判断这一位上\(1\)的个数的奇偶性sum
:大力累加起来就好了
具体的,修改的时候由于是异或操作,因此直接把对应要变的那一位\(0,1\)的个数调换,即用子树大小减去\(1\)的个数
然后还需要下传标记,这个直接lazy tag维护一下就好了,类似于Luogu P1501 [国家集训队]Tree II
然后我们就把这道水水的送分题写完了,LCT由于是板子所以会比较长,其实是核心代码是很短的
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 tc() (A==B&&(B=(A=Fin)+fread(Fin,1,S,stdin),A==B)?EOF:*A++)
#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:
Tp inline void read(T& x)
{
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;
class Link_Cut_Tree
{
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;
}
inline void link(CI x,CI y)
{
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);
RI i; for (F.read(n),F.read(m),i=1;i<=n;++i) F.read(val[i]);
for (LCT.build(),i=1;i<=m;++i)
{
F.get_alpha(opt); F.read(x); F.read(y);
switch (opt)
{
case 'L':
LCT.link(x,y); break;
case 'C':
LCT.cut(x,y); break;
case 'U':
F.read(z); LCT.modify_xor(x,y,z); break;
case 'A':
F.write(LCT.query_and(x,y)); break;
case 'O':
F.write(LCT.query_or(x,y)); break;
case 'X':
F.write(LCT.query_xor(x,y)); break;
case 'S':
F.write(LCT.query_sum(x,y)); break;
}
}
return F.Fend(),0;
}
Luogu P5352 Terrible Homework的更多相关文章
- bzoj 4320: ShangHai2006 Homework
4320: ShangHai2006 Homework Time Limit: 10 Sec Memory Limit: 128 MB Description 1:在人物集合 S 中加入一个新的程序员 ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- HDU 1789 Doing Homework again(贪心)
Doing Homework again 这只是一道简单的贪心,但想不到的话,真的好难,我就想不到,最后还是看的题解 [题目链接]Doing Homework again [题目类型]贪心 & ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- POJ 2082 Terrible Sets
Terrible Sets Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2747 Accepted: 1389 Des ...
- hdu-1789-Doing Homework again
/* Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Terrible Sets_单调栈
Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all re ...
- [luogu P2170] 选学霸(并查集+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...
- [luogu P2647] 最大收益(贪心+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...
随机推荐
- sql语句之连表操作
内连接 select * from employee inner join department on employee.dep_id = department.id 左连接 在内连接的基础上保留左表 ...
- app UI测试之UIAutomator
执行UIAutomator测试步骤 1.新建Java项目,导入android.jar和uiautomator.jar包,继承UiAutomatorTestCase 2.生成编译文件 android c ...
- Centos Mininal最小安装
第一步.下载Minimal镜像,http://mirrors.163.com/centos/7/isos/x86_64/ 第二步.安装VMWare,在VMWare上新建一个Centos虚拟机,可以在建 ...
- Ubuntu安装vnc+gnome的xstartup配置
Log 1 安装vncserver并且在xstartup配置gnome 背景:学习Ruby,想在ubuntu下使用rubymine 时间:2014-3-10 环境:Ubuntu 记录:Roy 其实这个 ...
- 关于如何隐藏UITabbar的问题
关于如何隐藏UITabbar的问题,曾经困扰过很多人. 1,设为Hidden, 这种方法虽然将TabBar隐藏掉,但是下面是一片空白,没有起到隐藏的实际功效 2,设置tabbar.frame = CG ...
- sublime text 插件的删除方法
1.ctr+shift+P,输入Package Control: Remove Package 2.回车, 3.出现一个弹出框,输入你要删除的package 4.回车,OK!!!
- SQL COUNT DISTINCT 函数
定义和用法 可以一同使用 DISTINCT 和 COUNT 关键词,来计算非重复结果的数目. 语法 SELECT COUNT(DISTINCT column(s)) FROM table 例子 注意: ...
- C#基础之析构函数
- Java中 Collection 、 List 、 Set 、 Map详解
一.容器( Collection ) 接口 容器( Collection )是最基本的集合接口,一个容器( Collection )保存一组对象( Object ),即对象是容器的元素( Ele ...
- 神奇的VIM
1. di'.di".di`.di( .di{ .dt 'abc' ==> '' di' "abc"==> "" di" `ab ...