(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

Catalog

Problem:Portal传送门

 原题目描述在最下面。

Solution:

 每个节点用一个变量储存它所覆盖区间最少需要加多少次答案能加1.

 如果这次更新不能使答案加1,则更新到lazy标记就可以了,如果能让答案加1,就更新到叶子节点,具体看代码。

AC_Code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<cctype>
#include<string>
#include<cmath>
#define lson rt<<1
#define rson rt<<1|1
#define all(x) (x).begin(),(x).end()
#define mme(a,b) memset((a),(b),sizeof((a)))
#define fuck(x) cout<<"* "<<x<<"\n"
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int MXN = 2e5 + 7;
const int MXE = 1e6 + 7;
const int INF = 0x3f3f3f3f;
int n, m;
int ar[MXN], br[MXN];
struct lp{
int l, r;
int nd, sum, lazy;
}cw[MXN<<2];
void push_up(int rt){
cw[rt].nd = min(cw[lson].nd, cw[rson].nd);
cw[rt].sum = cw[lson].sum + cw[rson].sum;
}
void build(int l,int r,int rt){
cw[rt].l = l; cw[rt].r = r;
cw[rt].sum = cw[rt].lazy = 0;
if(l == r){
cw[rt].nd = br[l];
return;
}
int mid = (l + r)>>1;
build(l,mid,lson);build(mid+1,r,rson);
push_up(rt);
}
void push_down(int rt){
if(cw[rt].lazy){
cw[lson].lazy += cw[rt].lazy;
cw[rson].lazy += cw[rt].lazy;
cw[lson].nd -= cw[rt].lazy;
cw[rson].nd -= cw[rt].lazy;
cw[rt].lazy = 0;
}
}
void update(int L,int R,int l,int r,int rt){
if(L<=l && r<= R){
if(cw[rt].nd <= 1){
if(l == r){
--cw[rt].nd;
if(cw[rt].nd <= 0){
cw[rt].nd = br[l];
cw[rt].sum++;
}
return;
}
}else{
--cw[rt].nd;
++cw[rt].lazy;
return;
}
}
if(l == r)return;
push_down(rt);
int mid = (l+r)>>1;
if(L > mid)update(L,R,mid+1,r,rson);
else if(R <= mid)update(L,R,l,mid,lson);
else {
update(L,mid,l,mid,lson);
update(mid+1,R,mid+1,r,rson);
}
push_up(rt);
}
int query(int L, int R,int l,int r,int rt){
if(L <= l&&r <= R){
return cw[rt].sum;
}
if(l == r)return 0;
push_down(rt);
int mid = (l+r)>>1, ans = 0;
if(L > mid)ans = query(L,R,mid+1,r,rson);
else if(R <= mid)ans = query(L,R,l,mid,lson);
else {
ans = query(L,mid,l,mid,lson);
ans += query(mid+1,R,mid+1,r,rson);
}
return ans;
}
int main(int argc, char const *argv[]){
while(~scanf("%d%d", &n, &m)){
for(int i = 1; i <= n; ++i){
scanf("%d", &br[i]);
}
char op[10];
int a, b;
build(1,n,1);
while(m--){
scanf("%s%d%d", op, &a, &b);
if(op[0] == 'a'){
update(a, b, 1, n, 1);
}else{
printf("%d\n", query(a, b, 1, n, 1));
}
}
}
return 0;
}

####Problem Description:
![这里写图片描述](https://img-blog.csdn.net/20180910205114800)

HDU6315 Naive Operations 线段树的更多相关文章

  1. HDU6315 Naive Operations(线段树 复杂度分析)

    题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...

  2. hdu Naive Operations 线段树

    题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...

  3. 杭电多校第二场 hdu 6315 Naive Operations 线段树变形

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  4. HDU - 6315(2018 Multi-University Training Contest 2) Naive Operations (线段树区间操作)

    http://acm.hdu.edu.cn/showproblem.php?pid=6315 题意 a数组初始全为0,b数组为1-n的一个排列.q次操作,一种操作add给a[l...r]加1,另一种操 ...

  5. HDU 6315 Naive Operations(线段树区间整除区间)

    Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...

  6. HDU - 6315 Naive Operations (线段树+思维) 2018 Multi-University Training Contest 2

    题意:数量为N的序列a和b,a初始全为0,b为给定的1-N的排列.有两种操作:1.将a序列区间[L,R]中的数全部+1:2.查询区间[L,R]中的 ∑⌊ai/bi⌋(向下取整) 分析:对于一个位置i, ...

  7. HDU 6315 Naive Operations(线段树+复杂度均摊)

    发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...

  8. HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树

    hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...

  9. 2018HDU多校二 -F 题 Naive Operations(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315 In a galaxy far, far away, there are two integer ...

随机推荐

  1. C判断语句

    C 判断 判断结构要求程序员指定一个或多个要评估或测试的条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的). C 语言把任何非零和非空的值假定为 true,把零或 null ...

  2. Linux常用命令入门

    在Linux早期的版本中,由于不支持图形化操作,用户基本上都是使用命令行方式来对系统进行操作.掌握常用 的一些Linux命令是非常有必要的,下面将分类进行介绍.由于篇幅有限,在这里我们介绍命令时有些不 ...

  3. 树分基础 bzoj 1468

    我们对于一棵树,我们找一个根,然后统计过这个点的路径有多少符合要求.怎么搞呢?我们可以先维护一个数据结构,然后把先把根作为一个距离自己为0的点放进去,然后对于每一棵子树,先找出所有的与之前的数据结构的 ...

  4. BZOJ 3230: 相似子串(后缀数组)

    传送门 解题思路 其实题目挺好想的.首先子串排名可以由后缀数组求得,因为不算重复的,所以后缀数组的每个后缀排名的去掉\(lcp\)的前缀排名为当前后缀的子串排名.这样就可以预处理出每个后缀的\(l,r ...

  5. hdu2089数位DP

    旁听途说这个名字很久了,了解了一下. 改题目的意思是给你若干区间,让你找寻区间内不含62或4的数. 首先暴力必然T...那么实际上就是说,想办法做一种预处理,在每次输入的时候取值运算就可以了. 既然是 ...

  6. CSS:CSS 字体

    ylbtech-CSS:CSS 字体 1.返回顶部 1. CSS 字体 CSS字体属性定义字体,加粗,大小,文字样式. serif和sans-serif字体之间的区别  在计算机屏幕上,sans-se ...

  7. 剑指offer——72圆圈中最后剩下的数字

    题目描述 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随机指 ...

  8. 剑指offer——65和为S的连续正数序列

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...

  9. EM相关两个算法 k-mean算法和混合高斯模型

    转自http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006924.html http://www.cnblogs.com/jerrylead/ ...

  10. 32-Ubuntu-用户权限-03-修改文件权限

    chmod 简介 chmod可以修改用户或组对文件或目录的权限. 命令格式如下: chmod +/-rwx 文件名/目录名 修改文件权限 例:demo.txt 1.增加权限 例:增加demo.txt的 ...