HDU6315 Naive Operations 线段树
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
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:

HDU6315 Naive Operations 线段树的更多相关文章
- HDU6315 Naive Operations(线段树 复杂度分析)
题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...
- hdu Naive Operations 线段树
题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...
- 杭电多校第二场 hdu 6315 Naive Operations 线段树变形
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- 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,另一种操 ...
- HDU 6315 Naive Operations(线段树区间整除区间)
Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...
- 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, ...
- HDU 6315 Naive Operations(线段树+复杂度均摊)
发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...
- HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树
hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...
- 2018HDU多校二 -F 题 Naive Operations(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315 In a galaxy far, far away, there are two integer ...
随机推荐
- 【leetcode】967. Numbers With Same Consecutive Differences
题目如下: Return all non-negative integers of length N such that the absolute difference between every t ...
- springMVC配置文件 的约束
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- mysql8.*忘记密码
1.关闭mysql服务 2.打开cmd窗口,找到安装目录下的bin然后复制命令 mysqld --console --skip-grant-tables --shared-memory 3.再打开一个 ...
- [bzoj2839]集合计数 题解 (组合数+容斥)
Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得 它们的交集的元素个数为K,求取法的方案数,答案模1000000007 ...
- .gitignore 文件使用说明
我们在使用 Git 进行版本控制的时候,有些文件是无需纳入 Git 管理的,通常都是些自动 生成的文件,像日志或者编译过程中创建的文件.我们可以创建一个名为 .gitignore 的文件,列出要忽略的 ...
- mongodb导入csv
主要介绍使用自带工具mongoimport工具将 CSV 格式数据导入到 MongoDB 的详细过程. 由于官方提供了mongoimport工具,所以实际上导入 CSV 格式数据的过程非常简单,再次体 ...
- (1)sqlserver2017安装
本体 https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 图形管理工具ssm 文档 https://docs.microso ...
- checkbox、radio使用jquery改变状态以及其他操作
$('input[type=checkbox]:checked').each(function(index,elem){ $(elem).attr("checked",false) ...
- linux下根据根据进程号查端口、根据端口号查进程号汇总,以及netstat的相关资料(工作中匮乏的知识)
根据端口查进程: lsof -i:port netstat -nap | grep port 根据进程号查端口: lsof -i|grep pid netstat -nap | grep pid 根据 ...
- PAT_A1091#Acute Stroke
Source: PAT A1091 Acute Stroke (30 分) Description: One important factor to identify acute stroke (急性 ...