【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 ...
随机推荐
- fdtd simulation, plotting with gnuplot, writting in perl
# 9月13日 于成都黄龙溪 1 #!/usr/bin/perl # Author : Leon Email: yangli0534@gmail.com # fdtd simulation , plo ...
- [转载]ExtJs4 笔记(5) Ext.Button 按钮
作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- [ORACLE错误]oracle 不能更新 PL/SQL 点击“edit data”报“ these query results are not updateable”
你可以选择在查询语句的最后加上 for update,就可以打开编辑锁,直接修改数据. 而在默认查询下,点击Edit data,会报错:The query results are not update ...
- iOS数组使用
相关链接: ios数组基本用法和排序 NSArray 排序汇总 iOS 数组排序方法 IOS-筛选数组内的元素 关于EnumerateObjectsUsingBlock和for-in之间的较量 [iO ...
- 利用exif.js解决ios手机上传竖拍照片旋转90度问题
html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...
- linux64需要增加的依赖库
sudo apt-get install git-core gnupg flex bison gperf build-essential \ zip curl zlib1g-dev gcc-multi ...
- 【C#】IDispose接口的应用
.net的GC机制有两个问题: 一.GC并不能释放所有资源,它更不能释放非托管资源. 二.GC也不是实时的,所有GC存在不确定性.所以需要使用析构函数,但是为了不重复GC,需要做一些处理. publi ...
- 跳台阶 一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
class Solution { public: int jumpFloor(int number) { ) ; ) ; )+jumpFloor(number-); } }; 如果先建立数组,然后利用 ...
- MSSQL 分组后取每组第一条(group by order by)
查询中经常遇到这种查询,分组后取每组第一条.分享下一个SQL语句: --根据 x 分组后.根据 y 排序后取第一条 select * from ( select ROW_NUMBER() over(p ...