Problem 1050: Just Go

Time Limits:  3000 MS   Memory Limits:  65536 KB

64-bit interger IO format:  %lld   Java class name:  Main

Description

There is a river, which contains n stones from left to right. These stones are magic, each

one has a magic number Ai which means if you stand on the ith stone, you can jump to (i +1)th stone, (i+2)th stone, ..., (i+Ai)th stone(when i+Ai > n, you can only reach as far as n), at first, you stand on 1th
stone, you want to calculate the number of ways to reach the nth stone.

Notice: you can not jump from right to left!

Input

Input starts with an integer T(1 <= T <= 10), denoting the number of test cases. Each test case contains an integer n(1 <= n <= 1e5), denoting the number stones. Next line contains n integers Ai(1 <= Ai <= 1e8).

Output

For each test case, print the number of way to reach the nth stone module 1e9+7.

Sample Input

3
5
1 2 3 4 5
1
10
2
2 1

Output for Sample Input

3
1
1

校赛那会儿的题目,主要操作就是区间更新、单点查询,树状数组和线段树都可以,树状数组的简单很多也好写很多,线段树嘛,线段树的模版题自行体会。

主要解题思路:刚开始肯定是1号石头初始化为1,其他的都是0,这个相信很好理解,然后就是往后覆盖区间,但却不是简单的覆盖,为了弄懂到底如何操作举个例子先,比如我到3号有a种路线,到4号有几种(假设4号只与3号连通)?当然跟3号一样,就是a种,如果3号可以跳跃的步数不止1即可以跳到5号上呢?此时会变成P5=P4+P3,即3号过来有三种,4号过来有1种(只能跳一步过来)。加起来就是4种,3->5一种,3->4>-5三种,推广出去就是不停地往后覆盖自己这个点的可到达情况数就好了,最后的答案当然就是Pn

树状数组代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
const LL mod=1000000007;
LL tree[N],arr[N]; inline int lowbit(int k)
{
return k&(-k);
}
void add(int k,LL val)
{
while (k<=100000)
{
tree[k]+=val;
k+=lowbit(k);
}
}
LL getsum(int k)
{
LL r=0;
while (k)
{
r+=tree[k];
r%=mod;
k-=lowbit(k);
}
return r%mod;
}
void init()
{
MM(tree,0);
MM(arr,0);
}
int main(void)
{
int tcase,i,j,l,r,n;
scanf("%d",&tcase);
while (tcase--)
{
scanf("%d",&n);
init();
add(1,1);
add(2,-1);
for (i=1; i<=n; i++)
{
scanf("%I64d",&arr[i]);
}
for (i=1; i<=n; i++)
{
l=i+1;
r=i+arr[i];
if(r>n)
r=n;
LL pres=getsum(i);
add(l,pres);
add(r+1,-pres);
}
printf("%I64d\n",getsum(n));
}
return 0;
}

线段树代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
const LL mod=1e9+7;
struct info
{
LL l,mid,r;
LL sum,add;
};
info T[N<<2];
LL arr[N];
void pushup(int k)
{
T[k].sum=T[LC(k)].sum+T[RC(k)].sum;
}
void pushdown(int k)
{
T[RC(k)].add+=T[k].add;
T[RC(k)].sum+=T[k].add*(T[RC(k)].r-T[RC(k)].l+1);
T[LC(k)].add+=T[k].add;
T[LC(k)].sum+=T[k].add*(T[LC(k)].r-T[LC(k)].l+1);
T[k].add=0;
}
void build(int k,LL l,LL r)
{
T[k].l=l;
T[k].r=r;
T[k].mid=MID(T[k].l,T[k].r);
T[k].add=0;
T[k].sum=0;
if(l==r)
T[k].sum=(l==1&&r==1?1:0);
else
{
build(LC(k),l,T[k].mid);
build(RC(k),T[k].mid+1,r);
pushup(k);
}
}
void update(int k,LL l,LL r,LL val)
{
if(r<T[k].l||l>T[k].r)
return ;
if(l<=T[k].l&&r>=T[k].r)
{
T[k].add+=val;
T[k].sum+=val*(T[k].r-T[k].l+1);
T[k].sum%=mod;
}
else
{
if(T[k].add)
pushdown(k);
update(LC(k),l,r,val);
update(RC(k),l,r,val);
pushup(k);
}
}
LL query(int k,LL x)
{
if(T[k].l==T[k].r&&T[k].l==x)
return T[k].sum%mod;
if(T[k].add)
pushdown(k);
if(x<=T[k].mid)
return query(LC(k),x)%mod;
else if(x>T[k].mid)
return query(RC(k),x)%mod;
}
int main(void)
{
int tcase,i,j,n;
LL l,r,x,val;
scanf("%d",&tcase);
while (tcase--)
{
scanf("%d",&n);
MM(arr,0);
for (i=1; i<=n; i++)
scanf("%I64d",&arr[i]); build(1,1,n);
for (i=1; i<=n; i++)
update(1,(LL)(i+1),(LL)(i+arr[i]>n?n:i+arr[i]),query(1,i)); printf("%I64d\n",query(1,n)%mod);
}
return 0;
}

NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)的更多相关文章

  1. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  2. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

  3. hdu3966 树链剖分点权模板+线段树区间更新/树状数组区间更新单点查询

    点权树的模板题,另外发现树状数组也是可以区间更新的.. 注意在对链进行操作时方向不要搞错 线段树版本 #include<bits/stdc++.h> using namespace std ...

  4. hdu1556 树状数组区间更新单点查询板子

    就是裸的区间更新: 相对于直观的线段树的区间更新,树状数组的区间更新原理不太相同:由于数组中的一个结点控制的是一块区间,当遇到更新[l,r]时,先将所有能控制到 l 的结点给更新了,这样一来就是一下子 ...

  5. HDU 1556 Color the ball (树状数组 区间更新+单点查询)

    题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...

  6. 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

    http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...

  7. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  8. hdu-3584 Cube---三维树状数组+区域更新单点查询

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3584 题目大意: 给定一个N*N*N多维数据集A,其元素是0或是1.A[i,j,k]表示集合中第 i ...

  9. POJ-2155 Matrix---二维树状数组+区域更新单点查询

    题目链接: https://vjudge.net/problem/POJ-2155 题目大意: 给一个n*n的01矩阵,然后有两种操作(m次)C x1 y1 x2 y2是把这个小矩形内所有数字异或一遍 ...

随机推荐

  1. VirtualBox 虚拟 Ubuntu 的一些感想

    之前有说过最近在学习Shell,公司及家里的电脑目前都是Windows系统,进行shell学习主要是用一些模拟linux环境的软件,比如banbun.cygwin banbun的功能很强大,集成了oh ...

  2. 修改PHP网站默认首页

    一般默认的首页文件是index.php index.php3 index.html index.htm之类的,要想修改为myNewIndex.php, 进入服务器Apache目录,找到httpd.co ...

  3. [Android Pro] Android Support 包里究竟有什么

    reference to : http://www.2cto.com/kf/201411/350928.html 随着 Android 5.0 Lollipop 的发布,Android 又为我们提供了 ...

  4. mysql:表注释和字段注释

    mysql:表注释和字段注释 1 创建表的时候写注释 create table test1 ( field_name int comment '字段的注释' )comment='表的注释'; 2 修改 ...

  5. Visual Studio从此走入非Windows程序猿家

    (此文章同时发表在本人微信公众号"dotNET每日精华文章") 在Build 2015大会上,微软放了很多大招,其中一个让普通(不管是微软生态还是非微软生态的)程序猿都密切关注的就 ...

  6. 中文在unicode中的编码范围

    以前写过一篇贴子是写中文在unicode中的编码范围 unicode中文范围,但写的不是很详细,今天再次研究了下unicode,并给出详细的unicode取值范围. 本次研究的unicode对象是un ...

  7. 文件上传漏洞演示脚本之js验证

    文件上传漏洞演示脚本之js验证 0 0       716   关于文件上传漏洞,想必玩web安全的同学们都有接触,之前本站也发布过一篇文章介绍文件上传漏洞的各种绕过方法,但是只是有文档却没有演示代码 ...

  8. Visual Studio提示Bonjour backend初始化失败

    Visual Studio提示Bonjour backend初始化失败 错误信息:The Bonjour backend failed to initialize, automatic Mac Bui ...

  9. Bash的基础知识man手册

    Bash的基础知识man手册 由于基于Android类设备的渗透测试都是通过各类终端实现.所以掌握Shell相关操作就显得尤为重要.Bash是一个为GNU计划编写的Unix Shell本文选自基于An ...

  10. B 倒不了的塔

    Time Limit:1000MS  Memory Limit:65535K 题型: 编程题   语言: 无限制 描述 Dota是Defense of the Ancients的简称,是一个dhk和y ...