BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习
#include <cstdio>
const int Maxn=;
inline void Get_Int(int & x)
{
char ch=getchar(); x=;
while (ch<'' || ch>'') ch=getchar();
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
}
//========================================
char str[];
int n,m,u,v;
struct Node;
inline void Swap(Node *&x,Node *&y);
struct Node
{
Node * pre,* ch[];
int rev;
inline int d() {return pre->ch[]==this;}
inline void Rev() {Swap(ch[],ch[]),rev^=;}
inline void setc(Node * r,int d) {ch[d]=r; r->pre=this;}
};
Node Memory[Maxn],* Port=Memory,* null,* S[Maxn],* t[Maxn];
inline void Swap(Node *&x,Node *&y) {Node * t=x;x=y;y=t;}
inline void Init() {null=Port++; null->rev=; null->pre=null->ch[]=null->ch[]=null;}
inline Node * NewNode(){Node * Ret=Port++; Ret->rev=; Ret->pre=Ret->ch[]=Ret->ch[]=null; return Ret;}
inline bool IsRoot(Node * x) {return x->pre==null || (x->pre->ch[]!=x && x->pre->ch[]!=x);}
inline bool Push_Down(Node * x)
{
if (x->rev)
{
x->rev=;
x->ch[]->Rev();
x->ch[]->Rev();
}
}
inline void Rotate(Node * x)
{
Node * y=x->pre; int d=x->d();
Push_Down(y); Push_Down(x);
if (IsRoot(y)) x->pre=y->pre; else y->pre->setc(x,y->d());
y->setc(x->ch[!d],d);
x->setc(y,!d);
}
inline void Splay(Node * x)
{
Push_Down(x);
while (!IsRoot(x))
if (IsRoot(x->pre)) Rotate(x); else
(x->pre->d()==x->d()) ? (Rotate(x->pre),Rotate(x)) : (Rotate(x),Rotate(x));
}
inline void Access(Node * f)
{
Node * c=null;
while (f!=null)
{
Splay(f);
f->ch[]=c;
c=f;
f=f->pre;
}
}
inline void Make_Root(Node * x)
{
Access(x),Splay(x),x->Rev();
}
inline void Link(Node * x,Node * y)
{
Make_Root(x),x->pre=y;
}
inline void Cut(Node * x,Node * y)
{
Access(x),Splay(x),Splay(y);
if (y->pre!=x) Swap(x,y);
Access(x),Splay(x),Splay(y);
y->pre=null;
}
inline Node * Find(Node * x)
{
while (x->pre!=null) x=x->pre; return x;
}
inline bool Check(Node * x,Node * y)
{
if (Find(x)==Find(y)) return true; else return false;
}
int main()
{
Init();
Get_Int(n),Get_Int(m);
for (int i=;i<=n;i++) t[i]=NewNode();
for (int i=;i<=m;i++)
{
scanf("%s",str); Get_Int(u),Get_Int(v);
if (str[]=='C') Link(t[u],t[v]);
if (str[]=='D') Cut(t[u],t[v]);
if (str[]=='Q') puts(Check(t[u],t[v])?"Yes":"No");
}
return ;
}
BZOJ 2409
#include <cstdio>
inline void Get_Int(int &x)
{
x=; register char ch=getchar(); int f=;
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();} x*=f;
}
inline void Put_Int(int x)
{
char ch[]; register int top=;
if (x<) putchar('-'),x=-x;
if (x==) ch[++top]='';
while (x) ch[++top]=x%+'',x/=;
while (top) putchar(ch[top--]); putchar('\n');
}
inline int Min(int x,int y) {return x>y?y:x;}
//===============================================
const int Maxn=;
int n,m,k[Maxn],x,y,Type;
struct Node
{
Node * pre,* ch[];
int size,rev;
inline int d() {return pre->ch[]==this;}
inline void setc(Node * r,int d) {ch[d]=r; r->pre=this;}
};
Node Memory[Maxn],* port=Memory,* t[Maxn],* null,* S[Maxn];
inline void Swap(Node *&x,Node *&y) {Node * t=x;x=y;y=t;}
inline void Init() {null=port++; null->rev=; null->size=;null->pre=null->ch[]=null->ch[]=null;}
inline Node * NewNode(){Node * Ret=port++; Ret->ch[]=Ret->ch[]=Ret->pre=null;Ret->size=;Ret->rev=; return Ret;}
inline void Push_Up(Node * x) {x->size=x->ch[]->size+x->ch[]->size+;}
inline void Push_Down(Node * x)
{
if (x->rev)
{
x->ch[]->rev^=;
x->ch[]->rev^=;
x->rev=;
Swap(x->ch[],x->ch[]);
}
}
inline bool IsRoot(Node * x) {return x->pre==null || (x->pre->ch[]!=x && x->pre->ch[]!=x);} inline void Rotate(Node * x)
{
Node * y=x->pre; int d=x->d();
if (IsRoot(y)) x->pre=y->pre; else y->pre->setc(x,y->d());
y->setc(x->ch[!d],d);
x->setc(y,!d);
Push_Up(y),Push_Up(x);
}
inline void Splay(Node * x)
{
int s=; S[]=x; Node * r=x;
while (!IsRoot(r->pre)) S[++s]=r=r->pre;
while (s) Push_Down(S[s--]); while (!IsRoot(x))
if (IsRoot(x->pre)) Rotate(x); else
(x->pre->d()==x->d()) ? (Rotate(x->pre),Rotate(x)) : (Rotate(x),Rotate(x));
Push_Up(x);
} inline void Access(Node * f)
{
Node * c=null;
while (f!=null)
{
Splay(f);
f->ch[]=c;
c=f;
f=f->pre;
}
}
inline void Make_Root(Node * r)
{
Access(r),Splay(r),r->rev^=;
}
inline void Link(Node * x,Node * y)
{
Make_Root(x);x->pre=y;
}
inline void Cut(Node * x,Node * y)
{
Access(x),Splay(y);
if (y->pre!=x) Swap(x,y);
Access(x),Splay(y);
y->pre=null;
}
int main()
{
// freopen("c.in","r",stdin);
Init();
Get_Int(n);
for (int i=;i<=n+;i++) t[i]=NewNode();
for (int i=;i<=n;i++)
Get_Int(x),k[i]=Min(i+x,n+),Link(t[i],t[k[i]]);
Get_Int(m); Make_Root(t[n+]);
for (int i=;i<=m;i++)
{
Get_Int(Type);
if (Type==)
{
Get_Int(x); x++;
Make_Root(t[n+]);
Access(t[x]),Splay(t[x]);
Put_Int(t[x]->size-);
}
if (Type==)
{
Get_Int(x),Get_Int(y); x++;
Cut(t[x],t[k[x]]);
k[x]=Min(n+,x+y);
Link(t[x],t[k[x]]);
}
}
return ;
}
BZOJ 2002
调了好久,明天在做几题。
8.17
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
inline void Get_Int(int &x)
{
x=; char ch=getchar();
while (ch<'' || ch>'') ch=getchar();
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
}
inline int Max(int x,int y) {return x>y?x:y;}
inline int Min(int x,int y) {return x>y?y:x;}
//===========================================
const int Maxn=;
struct Node
{
Node * pre,* ch[]; int Rev,Key,v;
inline int d() {return pre->ch[]==this;}
inline void setc(Node * x,int d) {ch[d]=x; x->pre=this;}
};
Node Memory[Maxn<<],* port=Memory,* null,* Nd[Maxn];
int u,v,n,m,x,Type;
inline bool IsRoot(Node * x) {return x->pre==null || (x->pre->ch[]!=x && x->pre->ch[]!=x);}
inline void Swap(Node *&x,Node *&y) {Node * T=x;x=y;y=T;}
inline Node * NewNode(int v) {Node * Ret=port++; Ret->v=Ret->Key=v; Ret->Rev=; Ret->pre=Ret->ch[]=Ret->ch[]=null; return Ret;}
inline void Init() {null=port++; null->pre=null->ch[]=null->ch[]=null; null->Rev=null->Key=null->v=;}
inline void Get_Rev(Node * x)
{
if (x==null) return;
x->Rev^=; Swap(x->ch[],x->ch[]);
}
inline void Push_Down(Node * x)
{
if (x->Rev)
{
x->Rev=;
Get_Rev(x->ch[]);
Get_Rev(x->ch[]);
}
}
inline void Push_Up(Node * x)
{
x->v=x->ch[]->v^x->ch[]->v^x->Key;
}
inline void Rotate(Node * x)
{
Node * y=x->pre; int d=x->d();
Push_Down(y),Push_Down(x);
if (IsRoot(y)) x->pre=y->pre; else y->pre->setc(x,y->d());
y->setc(x->ch[!d],d);
x->setc(y,!d);
Push_Up(y),Push_Up(x);
}
inline void Splay(Node * x)
{
Push_Down(x);
while (!IsRoot(x))
if (IsRoot(x->pre)) Rotate(x); else
(x->d()==x->pre->d())?(Rotate(x->pre),Rotate(x)):(Rotate(x),Rotate(x));
Push_Up(x);
}
inline void Access(Node * f)
{
Node * x=null;
while (f!=null)
{
Splay(f);
f->ch[]=x;
x=f;
f=f->pre;
}
}
inline void Make_Root(Node * x)
{
Access(x); Splay(x); Get_Rev(x);
}
inline void Link(Node * x,Node * y)
{
Make_Root(x),x->pre=y;
}
inline void Cut(Node * x,Node * y)
{
Access(x),Splay(x),Splay(y);
if (y->pre!=x) Swap(x,y);
Access(x),Splay(x),Splay(y);
y->pre=null;
}
inline Node * Find(Node * x)
{
while (x->pre!=null) x=x->pre; return x;
}
inline bool Check(int p,int q)
{
if (Find(Nd[p])==Find(Nd[q])) return true; return false;
}
//==================================
inline void Query()
{
Get_Int(u),Get_Int(v);
Make_Root(Nd[u]),Access(Nd[v]),Splay(Nd[v]);
printf("%d\n",Nd[v]->v);
}
inline void Connect()
{
Get_Int(u),Get_Int(v);
if (Check(u,v)) return;
Link(Nd[u],Nd[v]);
}
inline void Delete()
{
Get_Int(u),Get_Int(v);
if (!Check(u,v)) return;
Cut(Nd[u],Nd[v]);
}
inline void Modify()
{
Get_Int(u),Get_Int(v);
Make_Root(Nd[u]); Nd[u]->Key=v; Push_Up(Nd[u]);
}
int main()
{
Get_Int(n),Get_Int(m); Init();
for (int i=;i<=n;i++) Get_Int(x),Nd[i]=NewNode(x);
for (int i=;i<=m;i++)
{
Get_Int(Type);
if (Type==) Query();
if (Type==) Connect();
if (Type==) Delete();
if (Type==) Modify();
}
return ;
}
BZOJ 3282
BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习的更多相关文章
- 以 BZOJ 2002 为例学习有根树LCT(Link-Cut Tree)
以BZOJ 2002 弹飞绵羊为例学习有根树LCT(Link-Cut Tree) 注:本文非常简单,只涉及有根树LCT,对于无根树,LCT还有几个本文没有提到的操作,以后慢慢更新 =v= 知识储备 [ ...
- lct 模版题 bzoj 2002 2049
很早就有人给我推荐的模版题,然后我最近才刷的(' ' ) 昨天的tree 不知道比他们高到哪里去了,我和他谈笑风生啊! bzoj 2002 弹飞绵羊 重点:这道题的cut和link 由于这道题链 ...
- bzoj 2002 Bounce 弹飞绵羊
bzoj 2002 Bounce 弹飞绵羊 设一个虚拟节点表示被弹飞,则每个点的后继点是唯一确定的,每个点向它的后继点连边,就形成了一颗树. 询问就是问某个节点到虚拟节点的路径长度,修改就删除原来向后 ...
- [BZOJ - 2631] tree 【LCT】
题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- ●BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题解: LCT 如果把弹跳的起点和终点连一条边,弹出去的与n+1号点连边, 则不难发现 ...
- Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路
首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...
- bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊(LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2002 [题意] 给定n个数的序列,i可以跳到i+k[i],需要能够修改k并可以查询跳出 ...
- bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 题面: 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: ...
随机推荐
- js字符串和正则表达式中的match、replace、exec等的配合使用
正则并不是经常使用,而正则和字符串之间的函数关系又错综复杂,谁是谁的函数,又是怎么样的一种结果,往往我们是看一遍忘一遍,对此我是头疼不已,感觉自己是个笨蛋^_^. 为了以后不再查文档,特此把常用的函数 ...
- Nehe Opengl
http://nehe.gamedev.net/tutorial/lessons_01__05/22004/ Nehe Opengl,据说从基础到高端进阶都是很好的教程,准备学习这个序列,顺便记录下随 ...
- 基础篇-初步认识PE格式
1 PE(Portable Executable)格式,是Win32环境可移植可执行文件(如exe.dll.vxd.sys和vdm等)的标准文件格式.PE格式衍生于早期建立在VAX(R)VMS(R)上 ...
- bzoj 3529: [Sdoi2014]数表
#include<cstdio> #include<iostream> #include<algorithm> #define M 200009 //#define ...
- jbpm的学习 出处http://blog.csdn.net/hxirui/article/details/1221911
jbpm入门例子 分类: opensourse2006-09-14 11:30 37308人阅读 评论(22) 收藏 举报 jbpmhibernate数据库oraclemysqltransition ...
- 简单研究Android View绘制三 布局过程
2015-07-28 17:29:19 这一篇主要看看布局过程 一.布局过程肯定要不可避免的涉及到layout()和onLayout()方法,这两个方法都是定义在View.java中,源码如下: /* ...
- 机器学习相关的Awesome系列
Index Awesome 备注 1 Awesome Machine Learning 机器学习资源大全中文版 2 Awesome Artificial Intelligence 人工智能 3 Awe ...
- 【初级】linux mkdir 命令详解及使用方法实战
mkdir命令详解及使用方法实战 名称 MKDIR 是 make directories 的缩写 使用方法 mkdir [选项(如-p)] ...目录名称(及子目录注意用分隔符隔开)... 如使 ...
- 运行impala tpch
1.安装git和下载tpc-h-impala脚步 [root@ip-172-31-34-31 ~]# yum install git [root@ip-172-31-34-31 ~]# git clo ...
- C#文件流读写文件的简单winform实现
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...