题意:a数组初始全为0,b数组题目给你,有两种操作:

思路:dls的思路很妙啊,我们可以将a初始化为b,加一操作改为减一,然后我们维护一个最小值,一旦最小值为0,说明至少有一个ai > bi,那么找出所有为0的给他的最终结果加上一并且重置为bi,维护一个区间和,询问时线段树求和。一开始updateMin没加判断,单个复杂度飙到nlog(n),疯狂TLE...

代码:

#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define ll long long
const int maxn = 100000+5;
const int maxm = 100000+5;
const int MOD = 1e7;
const int INF = 0x3f3f3f3f;
using namespace std;
int n,q;
ll Min[maxn<<2],lazy[maxn<<2],sum[maxn << 2],b[maxn];
void push_down(int rt){
if(lazy[rt]){
Min[rt << 1] -= lazy[rt];
Min[rt << 1 | 1] -= lazy[rt];
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void push_up(int rt){
Min[rt] = min(Min[rt << 1],Min[rt << 1 | 1]);
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void build(int l,int r,int rt){
if(l == r){
lazy[rt] = sum[rt] = 0;
Min[rt] = b[l];
return;
}
int m = (l + r) >> 1;
build(l,m,rt << 1);
build(m + 1,r,rt << 1 | 1);
lazy[rt] = 0;
push_up(rt);
}
void updateMin(int l,int r,int rt){
if(l == r){
if(Min[rt] == 0){
Min[rt] = b[l];
sum[rt]++;
}
return;
}
push_down(rt);
int m = (l + r) >> 1;
if(!Min[rt << 1])
updateMin(l,m,rt << 1);
if(!Min[rt << 1 | 1])
updateMin(m + 1,r,rt << 1 | 1);
push_up(rt);
}
void update(int L,int R,int l,int r,int rt){
if(L <= l && R >= r){
Min[rt]--;
lazy[rt]++;
while(!Min[rt]){
updateMin(l,r,rt);
}
return;
}
push_down(rt);
int m = (l + r) >> 1;
if(L <= m)
update(L,R,l,m,rt << 1);
if(R > m)
update(L,R,m + 1,r,rt << 1 | 1);
push_up(rt);
}
ll query(int L,int R,int l,int r,int rt){
if(L <= l && R >= r){
return sum[rt];
}
//push_down(rt);
ll ans = 0;
int m = (l + r) >> 1;
if(L <= m)
ans += query(L,R,l,m,rt << 1);
if(R > m)
ans += query(L,R,m + 1,r,rt << 1 | 1);
return ans;
}
int main(){
while(~scanf("%d%d",&n,&q)){
for(int i = 1;i <= n;i++)
scanf("%lld",&b[i]);
build(1,n,1);
char s[20];
int l,r;
while(q--){
scanf("%s%d%d",s,&l,&r);
if(s[0] == 'a'){
update(l,r,1,n,1);
}
else{
ll ans = query(l,r,1,n,1);
printf("%lld\n",ans);
}
}
}
return 0;
}
/*
5 12
1 5 2 4 3
add 1 4
query 1 4
add 2 5
query 2 5
add 3 5
query 1 5
add 2 4
query 1 4
add 2 5
query 2 5
add 2 2
query 1 5
*/

HDU 6315 Naive Operations(线段树+区间维护)多校题解的更多相关文章

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

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

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

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

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

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

  4. 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, ...

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

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

  6. 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,另一种操 ...

  7. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  8. hdu Naive Operations 线段树

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

  9. HDU 6315.Naive Operations-线段树(两棵树合并)(区间单点更新、区间最值、区间求和)+思维 (2018 Multi-University Training Contest 2 1007)

    6315.Naive Operations 题意很好理解,但是因为区间求和求的是向下取整的a[i]/b[i],所以直接分数更新区间是不对的,所以反过来直接当a[i]==b[i]的时候,线段树对应的位置 ...

  10. HDU 4902 Nice boat --线段树(区间更新)

    题意:给一个数字序列,第一类操作是将[l,r]内的数全赋为x ,第二类操作是将[l,r]中大于x的数赋为该数与x的gcd,若干操作后输出整个序列. 解法: 本题线段树要维护的最重要的东西就是一个区间内 ...

随机推荐

  1. 设计模式之装饰模式(Java实现)

    “怎么了,鱼哥?” “唉,别提了,网购了一件衣服,结果发现和商家描述的差太多了,有色差就算了,质量还不好,质量不好就算了,竟然大小也不行,说好的3个X,邮的却是一个X的,不说了,退货去.你先开讲吧,你 ...

  2. JS-缓冲运动:菜单栏型悬浮框

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Linux学习之批量修改文件名

    1. 通过专业的改名命令rename实现 [root@oldboy oldboy]# ll total -rw-r--r-- root root Nov : stu_102999_1_finished ...

  4. web站点检查简易shell脚本

    1.web样式 <h4>THE STATUS OF RS:</h4> <meta http-equiv="> <table border=" ...

  5. Andorid- 反序列化,采用pull解析 xml 文件

    MainActivity.java 主入口,通过获得 XML文件 ,然后将解析后的文件标签以及文本内容拼接到 StringBuffer中,最后显示在TextView上 package com.exam ...

  6. .Net 获取前端传递的数据

    1. DotNet MVC: form是用来获得表单提交的数据:querystring是用来获得标识在URL后面的所有返回的变量及其值. 比如常见的URL网页地址都有xxx.asp?pn=123456 ...

  7. 向Docx4j生成的word文档添加图片和布局--第一部分

    原文标题:Adding images and layout to your Docx4j-generated word documents, part 1 原文链接:http://blog.iprof ...

  8. Python量化教程 常用函数

    # -*- coding: utf-8 -*- # @Author: fangbei # @Date: 2017-08-26 # @Original: price_str = '30.14, 29.5 ...

  9. 基于Nginx+FastDFS搭建图片文件系统

    Nginx+fastdfs:https://www.cnblogs.com/chiangchou/p/fastdfs.html#_label0_1 缩略图:https://blog.csdn.net/ ...

  10. python pip源配置

    一.Linux版本: linux的文件存放在:~/.pip/pip.conf 二.windows版本: 在用户文件夹下创建pip目录,并在pip目录下创建pip.ini文件(%HOME%\pip\pi ...