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

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. 【leetcode】962. Maximum Width Ramp

    题目如下: Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j]. ...

  2. 【leetcode】953. Verifying an Alien Dictionary

    题目如下: In an alien language, surprisingly they also use english lowercase letters, but possibly in a ...

  3. 跨域解决方法之window.name

    window对象有一个name属性,该属性有一个特征:即在一个窗口的生命周期内,窗口载入的所有的页面都是共享一个window.name的,每一个页面对window.name都有读写的权限,window ...

  4. 架构-Java-Netty:Netty框架

    ylbtech-架构-Java-Netty:Netty框架 Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网 ...

  5. (7)centos7 用户管理

    1.创建用户 useradd meng 如果创建用户时没有指定属于哪个组,则默认会创建一个名字与用户相同的组并归属于此组 对应的home目录下回创建一个meng的文件夹 2.设置密码 passwd m ...

  6. appium无法定位连接的真机元素

    报错信息: com.android.ddmlib.SyncException:Remote object dosen't exist! 解决办法: 错误原因是因为没有dump下来界面的信息保存到uid ...

  7. yang文件语法格式

    前言 NETCONF(Network Configuration Protocol),该协议的配置功能非常强大,同时兼顾监控和故障管理,安全验证和访问控制,得到业界的一致认可,被广泛用来配置网络 NE ...

  8. PAT_A1041#Be Unique

    Source: PAT A1041 Be Unique (20 分) Description: Being unique is so important to people on Mars that ...

  9. Codeforces 1189B Number Circle

    题目链接:http://codeforces.com/problemset/problem/1189/B AC代码: #include<bits/stdc++.h> using names ...

  10. 剑指offer——54数组中的逆序对

    题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...