【BZOJ 2002】【Hnoi 2010】弹飞绵羊 分块||Link Cut Tree 两种方法
ShallWe,Yveh,hmy,DaD3zZ,四人吃冰糕从SLYZ超市出来后在马路上一字排开,,,吃完后发现冰糕棍上写着:“向狮子座表白:愿做你的小绵羊”,,,
好吧在这道题里我们要弹飞绵羊,有分块和lct两种方法。
分块:
#include<cmath>
#include<cstdio>
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
#define for3(i,a,n) for(int i=(a);i>=(n);i--)
#define read(x) x=getint()
using namespace std;
inline int getint(){char c;int ret=0;for(c=getchar();c<'0'||c>'9';c=getchar());for(;c>='0'&&c<='9';c=getchar())ret=ret*10+c-'0';return ret;}
const int N=200003;
int n,o[N],t[N],w[N],step[N];
inline void work(int i){
int j=i+t[i];
if (j>n) w[i]=n+1,step[i]=1;
if (o[i]==o[j]){
w[i]=w[j];
step[i]=step[j]+1;
}else{
w[i]=j;
step[i]=1;
}
}
int main(){
read(n);
for1(i,1,n) read(t[i]);
int pn=floor(sqrt(n)),q,id,x,y;
for1(i,1,n) o[i]=(i-1)/pn+1;
for3(i,n,1) work(i);
read(q);
while (q--){
read(id);
if (id==1){
read(x); x++; y=0;
while (x<=n){
y+=step[x];
x=w[x];
}
printf("%d\n",y);
}else{
read(x); read(y); x++;
t[x]=y;
for3(i,x,1)
if (o[x]==o[i]) work(i);
else break;
}
}
return 0;
}
LCT(为什么我的lct的常数辣么大T_T)
#include<cstdio>
#include<algorithm>
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
#define read(x) x=getint()
using namespace std;
inline int getint(){char c;int ret=0;for(c=getchar();c<'0'||c>'9';c=getchar());for(;c>='0'&&c<='9';c=getchar())ret=ret*10+c-'0';return ret;}
const int N=200003;
struct node *null;
struct node{
node();
node *fa,*ch[2];
int s;
void count() {s=1+ch[0]->s+ch[1]->s;}
bool pl() {return fa->ch[1]==this;}
void setc(node *r,bool c) {ch[c]=r; r->fa=this;}
bool check() {return fa==null||((fa->ch[0]!=this)&&(fa->ch[1]!=this));}
}*rt[N];
node::node(){s=1;fa=ch[0]=ch[1]=null;}
int n;
inline void Build(){null=new node;null->s=0;null->ch[0]=null->ch[1]=null->fa=null;}
inline void rotate(node *r){
node *f=r->fa; bool c=r->pl();
if (f->check()) r->fa=f->fa;
else f->fa->setc(r,f->pl());
f->setc(r->ch[c^1],c);
r->setc(f,c^1);
f->count(); r->count();
}
inline void splay(node *r){
for(;!r->check();rotate(r))
if (!r->fa->check()) rotate(r->fa->pl()==r->pl()?r->fa:r);
r->count();
}
inline void access(node *r){
for(node *c=null;r!=null;r=r->fa){
splay(r);
r->setc(c,1);
r->count();
c=r;
}
}
inline void link(node *r,node *f){
access(r); splay(r);
r->ch[0]->fa=null; r->ch[0]=null; r->fa=f; r->count();
}
int main(){
Build();
int t,q,x,y;
read(n);
for1(i,1,n) rt[i]=new node;
for1(i,1,n) {read(t); if (i+t<=n) rt[i]->fa=rt[i+t];}
read(q);
while (q--){
read(x);
if (x==1){
read(y); y++;
access(rt[y]); splay(rt[y]); printf("%d\n",rt[y]->s);
}else{
read(x); read(y); x++;
if (x+y<=n) link(rt[x],rt[x+y]);
else link(rt[x],null);
}
}
return 0;
}
BeiYu的常数总是比我的要小,,本蒟蒻还得继续努力啊
【BZOJ 2002】【Hnoi 2010】弹飞绵羊 分块||Link Cut Tree 两种方法的更多相关文章
- bzoj 2002 HNOI 2010 弹飞绵羊
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- BZOJ 2002:Bounce 弹飞绵羊(分块)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 14944 Solved: 7598 [Su ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 【分块】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 ...
- bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 题面: 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9071 Solved: 4652[Submi ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 動態樹
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 4055 Solved: 2172[Submi ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2843 Solved: 1519[Submi ...
随机推荐
- 边工作边刷题:70天一遍leetcode: day 84-3
Meeting Rooms I/II 要点:这题和skyline类似,利用了interval start有序的特点,从左向右处理,用一个heap来动态表示当前占用rooms的时间段,所以heap的si ...
- opencv3.1 + opencv_contrib编译记事(win7下)
折腾了好几天,终于把opencv3.1加上一个额外的包opencv_contrib编译好了.(总体来说编译opencv就是填坑!!!) 最后我编译成功的是mingw版本的.也就是结合了Qt4.7+cm ...
- codeforces 710E E. Generate a String(dp)
题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...
- Fidder--实现手机的抓包
今天闲着没吊事,来写一篇关于怎么抓取Android中的app数据包?工欲行其事,必先利其器,上网google了一下,发现了一款神器:Fiddler,这个貌似是所有软件开发者必备神器呀!这款工具不仅可以 ...
- man命令中的文本操作
转自:http://www.cnblogs.com/chengmo/archive/2010/10/26/1861809.html 下面说下less命令操作: 光标移动操作: e ^E j ^N ...
- 08SpringMvc_(1)继承AbstractCommandController的Action[能够以实体的形式,收集客户端参数].(2)日期转换器和编码过滤器
上一篇文章说过要介绍两个控制器.这篇文章就介绍第二个控制器AbstractCommandController(这个类已经快要被废弃了,有更好的代替者,但还是要好好学这个类).这个控制器的额作用是为了收 ...
- Delphi7 安装ICS,与简单使用
官网 http://www.overbyte.be/ 下载 OverbyteIcsV816 完成后解压到E:\Delphi7\OverbyteIcsV816\ 1.在library里加入E:\Delp ...
- 区块链技术(一):Truffle开发入门
以太坊是区块链开发领域最好的编程平台,而truffle是以太坊(Ethereum)最受欢迎的一个开发框架,这是我们第一篇区块链技术文章介绍truffle的原因,实战是最重要的事情,这篇文章不讲原理,只 ...
- [CareerCup] 5.3 Next Binary Representation 下一个二进制表达
5.3 Given a positive integer, print the next smallest and the next largest number that have the same ...
- 报这个错 unrecognized selector sent to instance
1.给一个对象赋空值: 2.调不存在的方法:3.数据类型不对.