NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
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(线段树/树状数组区间更新单点查询)的更多相关文章
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Sub ...
- hdu3966 树链剖分点权模板+线段树区间更新/树状数组区间更新单点查询
点权树的模板题,另外发现树状数组也是可以区间更新的.. 注意在对链进行操作时方向不要搞错 线段树版本 #include<bits/stdc++.h> using namespace std ...
- hdu1556 树状数组区间更新单点查询板子
就是裸的区间更新: 相对于直观的线段树的区间更新,树状数组的区间更新原理不太相同:由于数组中的一个结点控制的是一块区间,当遇到更新[l,r]时,先将所有能控制到 l 的结点给更新了,这样一来就是一下子 ...
- HDU 1556 Color the ball (树状数组 区间更新+单点查询)
题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...
- 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers
http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- hdu-3584 Cube---三维树状数组+区域更新单点查询
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3584 题目大意: 给定一个N*N*N多维数据集A,其元素是0或是1.A[i,j,k]表示集合中第 i ...
- POJ-2155 Matrix---二维树状数组+区域更新单点查询
题目链接: https://vjudge.net/problem/POJ-2155 题目大意: 给一个n*n的01矩阵,然后有两种操作(m次)C x1 y1 x2 y2是把这个小矩形内所有数字异或一遍 ...
随机推荐
- [Android Pro] Android签名与认证详细分析之二(CERT.RSA剖析)
转载自: http://www.thinksaas.cn/group/topic/335449/ http://blog.csdn.net/u010571535/article/details/899 ...
- MVC文件夹
应用程序信息: Properties 引用应用程序文件夹: App_Data 文件夹 Content 文件夹 Controllers 文件夹 Models 文件夹 Scripts 文件夹 Views ...
- css3之currentColor
一个css3的高效变量currentColor,能够继承(父级)当前字体的颜色属性(代表当前的标签所继承的文字颜色). 参考demo:http://www.zhangxinxu.com/study/2 ...
- html select 下拉箭头隐藏
html select 下拉箭头隐藏 <!DOCTYPE html> <html> <head lang="en"> <meta char ...
- phpcms筛选功能
phpcms论坛的看到的-----做筛选功能-----自定义函数 <?php /** * extention.func.php 用户自定义函数库 * * @copyright (C) 2005- ...
- poj 2392 多重背包
题意:有几个砖,给出高度,能放的最大高度和数目,求这些砖能垒成的最大高度 依据lim排个序,按一层一层进行背包 #include<cstdio> #include<iostream& ...
- x264测试代码
建立一个工程,将头文件,库文件加载到工程,测试代码如下:#include <iostream>#include <string>#include "stdint.h& ...
- Android中动画
两种动画 view动画 属性动画 (也可以使用xml描述动画) view 4动画 补间动画 渐变 AlphaAnimation 缩放 ScaleAnimation 平移 TranslateAnima ...
- SQLServer2005利用维护计划自动备份数据库
经常性忘了给数据库备份,结果当数据库发生问题的时候,才发现备份是1个月以前的,那个后悔与懊恼还加惭愧啊,别提有对难受了.要认为的记住去备份比较难,每天事情又那么多,所以有了这个自动备份就不用愁了.先拷 ...
- Selenium学习
Web测试:Selenium使用 2008-12-23 10:49 by 敏捷的水, 17940 阅读, 16 评论, 收藏, 编辑 本文包含的主要内容: Selenium简介 我应该使用哪一个Sel ...