NOJ/HUST 1095 校赛 Just Go 线段树模板题
Description
Input
Output
Sample Input
3
5
1 2 3 4 5
1
10
2
2 1
Sample Output
3
1
1 题意:给你n个数,对于ai表示可以跳到i+1,i+2,i+3 …i + ai的位置,问到达最后一个位置有几种方法。
思路:第一次接触线段树。由于转移的关系,刚开始想使用DP,但是需要转移的数是一个区间,后来想到因为修改的是一个区间的值,想到用树状数组,但是区间修改的操作好像很烦阿
最终被告知是线段树。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#define nods(x) tree[x]
#define lefs(x) tree[x << 1]
#define rigs(x) tree[x << 1 | 1]
using namespace std; const int INF = 0x3f3f3f3f;
const int MAX = ;
const int MOD = 1e9 + ; struct segtree
{
int l, r, ans; //区间范围
int add; //懒惰标记
}tree[MAX << ]; //单点修改
void change(int &bep, int val)
{
bep += val;
bep = (bep % MOD + MOD) % MOD;
}
//父节点更新
void pushup(int pos)
{
change(nods(pos).ans, lefs(pos).ans + rigs(pos).ans);
}
//构造线段树
void build(int p, int l, int r)
{
nods(p).l = l;
nods(p).r = r;
nods(p).ans = nods(p).add = ;
if(l == r && l == )
nods(p).ans = ;
if(l == r)
return ; int mid = (l + r) >> ;
build( p << , l, mid );
build( p << | , mid + , r);
pushup(p);
}
//向下更新叶 通过延迟标记更新
void pushdown(int p)
{
if(nods(p).add)
{
change(lefs(p).add, nods(p).add);
change(rigs(p).add, nods(p).add);
change(lefs(p).ans, nods(p).add);
change(rigs(p).ans, nods(p).add);
nods(p).add = ;
}
}
//询问
int query(int p, int bor)
{
if(nods(p).l == nods(p).r)//递归到最小叶,直接返回
return nods(p).ans;
pushdown(p); //先应用标记 再查询 int mid = (nods(p).l + nods(p).r) >> ;
if(bor <= mid)
return query(p << , bor); //查询下一层左儿子
else return query(p << | , bor); //查询下一层右儿子
}
//区间修改
void update(int p, int l, int r, int val)
{
if(nods(p).l >= l && nods(p).r <= r) //完全包含
{
change(nods(p).add, val); //延迟标记
change(nods(p).ans, val); //更新该点
return ;
}
pushdown(p); //向下更新
int mid = (nods(p).l + nods(p).r) >> ;
if(r <= mid) //[l,r] 被包含于 [pl,mid]
update(p << , l, r , val);
else if(l > mid) //[l,r] 被包含于 [mid,pr]
update(p << | , l, r, val);
else //中间截断
{
update(p << , l, mid, val);
update(p << | , + mid, r, val);
}
pushup(p);
} int main()
{
int T, t;
int ans, ss, ee, n;
cin >> T;
while(T--)
{
scanf("%d", &n);
build(, , n);
for(int i = ; i <= n; i++)
{
scanf("%d", &t);
ss = i + ;
ee = min(i+t, n);
ans = query(, i);
if(i!=n)
update(, ss, ee, ans);
}
printf("%d\n", query(, n));
}
}
NOJ/HUST 1095 校赛 Just Go 线段树模板题的更多相关文章
- [AHOI 2009] 维护序列(线段树模板题)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
- hdu1823(二维线段树模板题)
hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询 ...
- [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]
可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...
- HDU 1698 Just a Hook (线段树模板题-区间求和)
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
- UESTC - 1057 秋实大哥与花 线段树模板题
http://acm.uestc.edu.cn/#/problem/show/1057 题意:给你n个数,q次操作,每次在l,r上加上x并输出此区间的sum 题解:线段树模板, #define _CR ...
- 2016多校8th 1008【线段树-神题】
题意: T N M N个数 M个操作 一个数组A, 有3个操作 1 l r x,a[l]-a[r]都+x 2 l r,a[i]=sqrt(a[i]),l<=i<=r 3 l r,求和,a[ ...
- POJ - 3264 线段树模板题 询问区间最大最小值
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...
- 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)
思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...
- zkw线段树模板题
学了zkw线段树,觉得没什么必要刷专题的吧(切不动啊).. 那先放一个模板题吧(我绝不会和你说搬了一道树状数组模板题的!!!) 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加 ...
随机推荐
- SPOJ 694 Distinct Substrings/SPOJ 705 New Distinct Substrings(后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)
Description Facer is addicted to a game called "Tidy is learning to swim". But he finds it ...
- 嵌入式码农的10年Bug调试经验,值得一看
下面这些都是我经历过的会导致难点bug的问题: 1.事件顺序.在处理事件时,提出下列问题会很有成效:事件可以以不同的顺序到达吗?如果我们没有接收到此事件会怎么样?如果此事件接连发生两次会怎么样?哪怕通 ...
- PAT 1058 选择题
https://pintia.cn/problem-sets/994805260223102976/problems/994805270356541440 批改多选题是比较麻烦的事情,本题就请你写个程 ...
- PHP实现大文件分割上传与分片上传
转载:http://www.zixuephp.com/phpstudy/phpshilie/20170829_43029.html 服务端为什么不能直接传大文件?跟php.ini里面的几个配置有关 u ...
- java.awt.AWTError: Can't connect to X11 window server using ':20' as the value of the DISPLAY variable
1.使用pio在Linux服务器上创建window文件时,需要使用到Linux的图形界面服务,出现以下问题需确认用户权限. 参考文献:https://zhidao.baidu.com/question ...
- 手机端浏览器适配,background 背景平铺 ,有的出不来
.mobilePage .report { background: url(../images/mobile-report.png) repeat; background-size: 100% :/* ...
- Python使用requests模块下载图片
MySQL中事先保存好爬取到的图片链接地址. 然后使用多线程把图片下载到本地. # coding: utf-8 import MySQLdb import requests import os imp ...
- CodeForces 185A. Plant (矩阵快速幂)
CodeForces 185A. Plant (矩阵快速幂) 题意分析 求解N年后,向上的三角形和向下的三角形的个数分别是多少.如图所示: N=0时只有一个向上的三角形,N=1时有3个向上的三角形,1 ...