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

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. python读取ini配置文件的示例代码(仅供参考)

    这篇文章主要介绍了python读取ini配置文件过程示范,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装 pip install configp ...

  2. go结构体上的函数

    go结构体上的函数 我们可以将一个方法和一个结构体关联: type Saiyan struct { Name string Power int } func (s *Saiyan) Super() { ...

  3. posix_rpi_common.cmake学习

    # This file is shared between posix_rpi_native.cmake 这个文件在posix_rpi_native.cmake和posix_rpi_cross.cma ...

  4. mvn eclipse:eclipse

    pom.xml 在哪个文件夹, 你就在哪里按shift 右键,,[在此处打开命令窗口]  执行那个命令. mvn eclipse:eclipse

  5. smf和mmf分别是什么?

    单模光纤/缩写SMF(single mode fiber) 多模光纤/缩写MMF(multi mode (optical) fibre)

  6. 将某个Qt4项目升级到Qt5遇到的问题

    本文转载自http://hi.baidu.com/xchinux/item/9044d8ce986accbb0d0a7b87 一.将某个QT4项目改成QT5遇到的问题 该Qt4项目以前是使用Qt4.7 ...

  7. AVR446步进电机算法推导及应用

    https://blog.csdn.net/Renjiankun/article/details/80513839?utm_source=copy

  8. 自定义可点击的ImageSpan并在TextView中内置“View“

    有的时候可能想在TextView中添加一些图片,比如下图,发短信输入联系人时,要把联系人号码换成一个图片,但这个图片无法用固定的某张图,而是根据内容进行定制的,这更像一个view. 当然,如果你不是v ...

  9. python之保留有限的历史记录(collections.deque)

    1.deque(maxlen=N)创建一个固定长度的队列,当有新的记录加入而队列已经满时,会自动移除老的记录. from collections import deque q = deque(maxl ...

  10. 2019_7_31python

    练习  输入三条边长如果能构成三角形就计算周长和面积 import math a,b,c = input().split(',') a = float(a) b = float(b) c = floa ...