P3203 弹飞绵羊-分块

观察数据范围,发现可以分块。只需要处理每个点跳出所在块后的位置和次数即可。目的是为了加速查询并降低修改复杂度。

对于修改,重构整个块内信息即可。

时间复杂度正确的一批

具体实现也挺简单。注意重构时从后往前贡献即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read(){
int w=0,x=0;char c=getchar();
while(!isdigit(c))w|=c=='-',c=getchar();
while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
return w?-x:x;
}
namespace star
{
const int maxn=2e5+10;
int n,N,out[maxn],cos[maxn],a[maxn],belong[maxn],l[maxn],r[maxn];
void sudo(int l,int r){for(int i=r;i>=l;i--)if(i+a[i]>n or belong[i+a[i]]!=belong[i])out[i]=i+a[i],cos[i]=1;else out[i]=out[i+a[i]],cos[i]=1+cos[i+a[i]];}
inline void work(){
n=read();
N=sqrt(n);
for(int i=1;i<=n;i++)
belong[i]=(i-1)/N+1;
for(int i=1;i<=n;i++){
if(!l[belong[i]])l[belong[i]]=i;
r[belong[i]]=i;
}
for(int i=1;i<=n;i++)a[i]=read();
sudo(1,n);
int q=read();
while(q--){
if(read()==1){
int sum=0,pos=read()+1;
while(pos<=n)sum+=cos[pos],pos=out[pos];
printf("%d\n",sum);
}else{
int pos=read()+1;
a[pos]=read();
sudo(l[belong[pos]],r[belong[pos]]);
}
}
}
}
signed main(){
star::work();
return 0;
}

那我要是想知道最后弹飞前的点在哪呢?

我们只需要在重构的时候记录一下从哪来的就好了。

双倍经验:CF13E

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read(){
int w=0,x=0;char c=getchar();
while(!isdigit(c))w|=c=='-',c=getchar();
while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
return w?-x:x;
}
namespace star
{
const int maxn=2e5+10;
int n,N,out[maxn],last[maxn],cos[maxn],a[maxn],belong[maxn],l[maxn],r[maxn];
void sudo(int l,int r){for(int i=r;i>=l;i--)if(i+a[i]>min(n,r))out[i]=i+a[i],last[i]=i,cos[i]=1;else out[i]=out[i+a[i]],last[i]=last[i+a[i]],cos[i]=1+cos[i+a[i]];}
inline void work(){
n=read();
int q=read();
N=sqrt(n);
for(int i=1;i<=n;i++)
belong[i]=(i-1)/N+1;
for(int i=1;i<=n;i++){
if(!l[belong[i]])l[belong[i]]=i;
r[belong[i]]=i;
}
for(int i=1;i<=n;i++)a[i]=read();
for(int i=1;i<=belong[n];i++)sudo(l[i],r[i]);
while(q--){
if(read()==1){
int sum,pos,lpos;
sum=0,pos=lpos=read();
while(pos<=n)sum+=cos[pos],lpos=last[pos],pos=out[pos];
printf("%d %d\n",lpos,sum);
}else{
int pos=read();
a[pos]=read();
sudo(l[belong[pos]],r[belong[pos]]);
}
}
}
}
signed main(){
star::work();
return 0;
}

P3203 弹飞绵羊-分块的更多相关文章

  1. 【BZOJ 2002】【Hnoi 2010】弹飞绵羊 分块||Link Cut Tree 两种方法

    ShallWe,Yveh,hmy,DaD3zZ,四人吃冰糕从SLYZ超市出来后在马路上一字排开,,,吃完后发现冰糕棍上写着:“向狮子座表白:愿做你的小绵羊”,,, 好吧在这道题里我们要弹飞绵羊,有分块 ...

  2. 【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊 分块

    [bzoj2002][Hnoi2010]Bounce 弹飞绵羊 2014年7月30日8101 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀 ...

  3. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

  4. BZOJ 2002 Bounce 弹飞绵羊 (分块或动态树)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 13768  Solved: 6989[Subm ...

  5. [HNOI2010] 弹飞绵羊 (分块)

    [HNOI2010] 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上 ...

  6. 【BZOJ2002】【HNOI2010】弹飞绵羊 [分块]

    弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description 某天,Lostmonkey发明了一 ...

  7. BZOJ 2002 Bounce 弹飞绵羊 —— 分块算法

    题目链接:https://vjudge.net/problem/HYSBZ-2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Li ...

  8. 【BZOJ2002】 [Hnoi2010]Bounce 弹飞绵羊 分块/LCT

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...

  9. bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...

随机推荐

  1. git介绍及使用

    一.架构 版本库(仓库):工作区中有一个隐藏目录.git,这个目录不属于工作区,而是git的版本库,是git管理的所有内容. 暂存区:版本库中包含一个临时区域,保存下一步要提交的文件. 分支:版本库中 ...

  2. thymeleaf模板引擎基础知识

    一.表达式 分为四类: 1.变量表达式 ${} :获取容器上下文变量的值. 举例: 获取application域中的username: ${application.username} 获取sessio ...

  3. Java8 中使用Stream 让List 转 Map使用总结

    在使用 Java 的新特性 Collectors.toMap() 将 List 转换为 Map 时存在一些不容易发现的问题,这里总结一下备查. 空指针风险 java.lang.NullPointerE ...

  4. 【题解】Luogu2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

  5. 【题解】Luogu P3110 [USACO14DEC]驮运Piggy Back

    [题解]Luogu P3110 [USACO14DEC]驮运Piggy Back 题目描述 Bessie and her sister Elsie graze in different fields ...

  6. NOIP模拟测试21「折纸&#183;不等式」

    折纸 题解 考试时无限接近正解,然而最终也只是接近而已了 考虑模拟会爆炸,拿手折纸条试一试,很简单 考你动手能力 代码 #include<bits/stdc++.h> using name ...

  7. Mysql优化(出自官方文档) - 第十二篇(优化锁操作篇)

    Mysql优化(出自官方文档) - 第十二篇(优化锁操作篇) 目录 Mysql优化(出自官方文档) - 第十二篇(优化锁操作篇) 1 Internal Locking Methods Row-Leve ...

  8. js笔记15

    DOM2动态创建节点 1.生成节点的方法 document.createElement("div") 2.插入节点的方法 父元素.appendChild(新节点) 在父节点的子节点 ...

  9. flex mx:TabNavigator进行选项卡切换,需要进行交互时。发生Error #1009错误

    当需要进行 mx:TabNavigator选项卡进行切换时,需要进行交互,然后却报了"TypeError: Error #1009: 无法访问空对象引用的属性或方法."错误,产生这 ...

  10. Redis 实战篇:GEO助我邂逅附近女神

    码老湿,阅读了你的巧用数据类型实现亿级数据统计之后,我学会了如何游刃有余的使用不同的数据类型(String.Hash.List.Set.Sorted Set.HyperLogLog.Bitmap)去解 ...