hdu6315 /// 线段树区间更新
题目大意:
给定n q 为序列的个数和操作的个数
给定n个数的序列b[]作为分母
初始全为0的序列a[]作为分子
两种操作
add l r 为a[]的l到r区间全部+1
query l r 为查询l到r区间a[i]/b[i]的总和(向下取整)
因为是向下取整 所以线段树维护区间的min(b[i]-a[i]%b[i])
那么当区间的这个最小的sub值为0时 说明这个区间内至少有一个点有新的贡献
所以当sub值为0时 才更新答案并向下更新 否则更新lazy和sub即可
即在代码中 更新lazy和sub 当sub恰好等于0 那么更新now
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
#define mem(i,j) memset(i,j,sizeof(i))
using namespace std;
const int N=1e5+;
const int MOD=1e9+;
int n, m, b[N]; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
LL lazy[N<<], now[N<<], sub[N<<];
void pushUp(int rt) {
sub[rt]=min(sub[rt<<],sub[rt<<|]);
now[rt]=now[rt<<]+now[rt<<|];
}
void pushDown(int rt) {
if(lazy[rt]) {
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
sub[rt<<]-=lazy[rt];
sub[rt<<|]-=lazy[rt];
lazy[rt]=;
}
}
void build(int l,int r,int rt) {
lazy[rt]=; now[rt]=;
if(l==r) {
sub[rt]=b[l];
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushUp(rt);
}
void updatesub(int l,int r,int rt) {
if(l==r) {
now[rt]++; sub[rt]=b[l];
return;
}
pushDown(rt);
int m=(l+r)>>;
if(sub[rt<<]==) updatesub(lson);
if(sub[rt<<|]==) updatesub(rson);
pushUp(rt);
}
void update(int L,int R,int l,int r,int rt) {
if(L<=l && r<=R) {
lazy[rt]++; sub[rt]--;
if(sub[rt]==) updatesub(l,r,rt);
return;
}
pushDown(rt);
int m=(l+r)>>;
if(L<=m) update(L,R,lson);
if(m<R) update(L,R,rson);
pushUp(rt);
}
int query(int L,int R,int l,int r,int rt) {
if(L<=l && r<=R) return now[rt];
pushDown(rt);
int m=(l+r)>>;
int res=;
if(L<=m) res+=query(L,R,lson);
if(m<R) res+=query(L,R,rson);
return res;
}
void init() {
mem(now,); mem(sub,); mem(lazy,);
} int main()
{
while(~scanf("%d%d",&n,&m)) {
for(int i=;i<=n;i++) scanf("%d",&b[i]);
init(); build(,n,);
while(m--) {
char op[]; scanf("%s",op);
int l,r; scanf("%d%d",&l,&r);
if(op[]=='a') update(l,r,,n,);
else printf("%d\n",query(l,r,,n,));
}
} return ;
}
hdu6315 /// 线段树区间更新的更多相关文章
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- HDU5039--Hilarity DFS序+线段树区间更新 14年北京网络赛
题意:n个点的树,每个条边权值为0或者1, q次操作 Q 路径边权抑或和为1的点对数, (u, v)(v, u)算2个. M i修改第i条边的权值 如果是0则变成1, 否则变成0 作法: 我们可以求出 ...
随机推荐
- vue自定义指令clickoutside实现点击其他元素才会触发
clickoutside.js // 代码内容 const clickoutsideContext = '@@clickoutsideContext'; export default { bind(e ...
- docker swarm创建swarm集群
三台linux主机 manager:192.168.100.151 work1:192.168.100.156 work2:192.168.100.157 manager docker swarm i ...
- zabbix--External checks 外部命令检测
概述zabbix server 运行脚本或者二进制文件来执行外部检测,外部检测不需要在被监控端运行任何 agentditem key 语法如下: 参数 定义 script shell 脚本或者二进制文 ...
- JUnit中Assert简单介绍
junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...
- InnoDB中没有主键是如何运转的
本文章翻译自 https://blog.jcole.us/2013/05/02/how-does-innodb-behave-without-a-primary-key/ 原文作者的创作背景 一个下午 ...
- 深入Dagger:自定义AutoValue
前言 上一篇文章介绍了JavaPoet的原理和使用,这里在介绍一下AutoValue的原理,并模仿自定义实现一个AutoValue. AutoValue的是Google为了实现ValueClass设计 ...
- Docker-搭建Docker Registry
私有Docker Registry的部署和配置 从Docker Hub上可以获取官方的Registry的镜像,Registry 默认的对外服务端口是 5000,如果我们宿主机上运行的 Registry ...
- 使用androidstudio时遇到的一些小错误
1 路径名字中不能有汉字 报如下错误:Error:(1, 0) Your project path contains non-ASCII characters. This will most lik ...
- 【Luogu】【关卡2-11】简单数学问题(2017年10月)【还差三道题】
火星人 麦森数 P1403 [AHOI2005]约数研究 f(n)表示n的约数个数,现在给出n,要求求出f(1)到f(n)的总和. 解答:有几个1做约数的个数 = n /1; 有几个2做约数的个数 = ...
- Angularjs书写规范
文件命名原则: 遵循以描述组件功能,然后是类型(可选)的方式来给所有的组件提供统一的命名 命名:feature.type.js. 测试文件名(feature.type.spec.js) 大多数文件都有 ...