【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 ...
随机推荐
- codeforces 422A A. Borya and Hanabi(暴力)
题目链接: A. Borya and Hanabi time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Hanoi塔
2016-03-19 17:01:35 问题描述: 假设有三个命名为 A B C 的塔座 ,在塔座A上插有n个直径大小不相同,由小到大编号为1 ,2 ,3 ,··· ,n的圆盘,要求将A座上的圆盘移至 ...
- 关于JS获取select值的两种实现方法
前几天发了一篇关于javascript获取select值的方法,后来发现有另一种实现方法,所以就都发出来比较一下: 方法一:通过获取option标签的value值来确定: <!DOCTYPE h ...
- HASHKILL
6ac66ed89ef9654cf25eb88c21f4ecd0是flag的MD5码,(格式为ctf{XXX_XXXXXXXXXXX_XXXXX})由一个0-1000的数字,下划线,纽约的一个区,下划 ...
- C++引用和java引用的区别
在c++里的引用其实是一个变量的别名,而java则是一个变量存储实际对象的地址和C++指针很相似
- 【转】【C#】ColorMatrix
ColorMatrix(色彩矩阵),是GDI+里用来调整图片色彩的矩阵. 什么是矩阵,说白了就是C#里的二维数组. 那么这个矩阵调整色彩的原理是什么,他是怎么来调整色彩的呢?这个要从线性代数里的矩阵相 ...
- C#定制并发送HTML邮件
HTML格式的邮件能够使用所有html/css使得邮件更丰富,比如现在很多newsletter 都是使用的html邮件. 今天试了一下,如何把图片嵌入到html中呢? 方法一,你的图片host到了in ...
- Django基础 - Debug设置为False后静态文件获取404
当设置setting.py文件当中的DEBUG=FALSE后,Django会默认使用Web Server的静态文件处理,故若没设置好Web Server对静态文件的处理的话,会出现访问静态文件404的 ...
- JS调试加断点
js在回调函数执行时直接就跳过了,想看下回调函数也看不了,调试的debug代码一时半会儿想不起来,找了几分钟找到了,还是记一下好. 1 debugger;
- IIS增加并发数
网站出现这样的错误信息:Error Summary: HTTP Error 503.2 - Service UnavailableThe serverRuntime@appConcurrentRequ ...