hdu Naive Operations 线段树
题目大意
题目链接Naive Operations
题目大意:
区间加1(在a数组中)
区间求ai/bi的和
ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5
axmorz
思路
因为是整除,所以一次加法可以对ans 没有影响
当ai是bi的倍数,对ans会有贡献
所以我们维护一个sum,初值为bi(只对于线段树的叶子节点有用)
当区间+1的时候
我们对sum-1
当sum=0的时候(倍数)
ans++,sum=bi
然后再维护一个区间最小值
当区间内的mi>0时
显然、对答案没有贡献
当区间内的mi<=0时
对答案一定有贡献
所以再在l到r内进行递归
对于g()函数的复杂度为
ans=n/1+n/2+n/3+n/4````+n/n=nlogn级别(看的别的题解护臂逼的)
代码
#include <iostream>
#include <cstring>
#include <cstdio>
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
const int maxn = 1e5 + 7;
const int maxm = 4e5 + 7;
const int inf = 0x3f3f3f3f;
int n, m, b[maxn];
struct node {
int l, r;
int ans, sum, mi, lazy;
} e[maxm];
int read() {
int x = 0, f = 1; char s = getchar();
for (; s < '0' || s > '9'; s = getchar()) if (s == '-') f = -1;
for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
}
void pushup(int rt) {
e[rt].ans = e[ls].ans + e[rs].ans;
e[rt].mi = min(e[ls].mi, e[rs].mi);
}
void pushdown(int rt) {
if (e[rt].lazy) {
e[ls].lazy += e[rt].lazy;
e[rs].lazy += e[rt].lazy;
e[ls].mi -= e[rt].lazy;
e[rs].mi -= e[rt].lazy;
e[rt].lazy = 0;
}
}
void build(int l, int r, int rt) {
e[rt].l = l, e[rt].r = r;
if (l == r) {
e[rt].sum = e[rt].mi = b[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, ls);
build(mid + 1, r, rs);
pushup(rt);
}
void g(int L, int R, int rt) {
if (e[rt].mi > 0) {
return ;
} else {
if (e[rt].l == e[rt].r) {
e[rt].ans++;
e[rt].mi = e[rt].sum = b[e[rt].l];
return ;
}
}
pushdown(rt);
if (e[ls].mi <= 0) g(L, R, ls);
if (e[rs].mi <= 0) g(L, R, rs);
pushup(rt);
}
void add(int L, int R, int rt) {
if (L <= e[rt].l && e[rt].r <= R) {
e[rt].mi--;
e[rt].lazy++;
g(e[rt].l, e[rt].r, rt);
//cout<<e[rt].l<<" "<<e[rt].r<<" "<<e[rt].mi<<"<M<<<<<\n";
return;
}
pushdown(rt);
int mid = (e[rt].l + e[rt].r) >> 1;
if (L <= mid) add(L, R, ls);
if (R > mid) add(L, R, rs);
pushup(rt);
}
void debug() {
printf("debug\n");
printf(" %d\n", e[1].mi);
printf(" %d %d\n", e[2].mi, e[3].mi );
printf(" %d %d %d %d\n", e[4].mi, e[5].mi, e[6].mi, e[7].mi );
printf(" %d %d %d %d %d %d %d %d\n", e[8].mi,
e[9].mi, e[10].mi, e[11].mi, e[12].mi, e[13].mi, e[14].mi, e[15].mi);
}
int query(int L, int R, int rt) {
if (L <= e[rt].l && e[rt].r <= R) {
return e[rt].ans;
}
pushdown(rt);
int mid = (e[rt].l + e[rt].r) >> 1, ans = 0;
if (L <= mid) ans += query(L, R, ls);
if (R > mid) ans += query(L, R, rs);
pushup(rt);
return ans;
}
int main() {
while(scanf("%d%d",&n,&m)!=EOF) {
for (int i = 1; i <= n; ++i)
b[i] = read();
memset(e,0,sizeof(e));
build(1, n, 1);
for (int i = 1; i <= m; ++i) {
char s[10];
scanf("%s", s);
int a = read(), b = read();
if (s[0] == 'a') {
add(a, b, 1);
} else {
printf("%d\n", query(a, b, 1));
}
}
}
return 0;
}
hdu Naive Operations 线段树的更多相关文章
- 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 线段树变形
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- 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-DuoXiao第二场hdu 6315 Naive Operations 线段树
hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...
- 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\)次,复杂度可以得当保证. 然后问题就是怎么判 ...
- HDU6315 Naive Operations(线段树 复杂度分析)
题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...
- 2018HDU多校二 -F 题 Naive Operations(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315 In a galaxy far, far away, there are two integer ...
- HDU6315 Naive Operations 线段树
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. Solution ...
随机推荐
- Flip Game---poj1753(状压+bfs)
题目链接:http://poj.org/problem?id=1753 题意:是有一个4X4的图,b代表黑色,w代表白色,问最少翻转几次可以把所有的点变成白色或者黑色,每次翻转一个点时,可以把它 ...
- 自定义WordPress文件上传路径
自WordPress 3.5版本开始,隐藏了后台媒体设置页面的“默认上传路径和文件的完整URL地址”选项,可以通过下面的代码将该选项调出来. 将下面的代码添加到当前主题functions.php文件中 ...
- spring的bean容器加载
1.在单独使用的时候可以通过ClassPathXmlApplicationContext(“配置文件.xml”);来启动容器. 2.在MVC下是通过启动servlet容器,初始化DispatcherS ...
- Ng线性回归实现学习[转载]
转自:https://github.com/huanting74/Coursera-ML-AndrewNg 1.可视化数据 import pandas as pd import seaborn as ...
- Sublime text 3搭建Python-Anaconda开发环境
网络上的教程各种各样,大同小异.自己安装时还是出了些问题,因此总结一篇博文. Sublime Text 是一款轻量级跨平台的文本编辑器,可通过包(Package)扩充自身功能. 有很多搭建python ...
- java 字节流与字符流的区别详解
字节流与字符流 先来看一下流的概念: 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入 ...
- MVC5+Easyui1.3.6+EF6 开发部分备忘笔记
一点一点增加,后面继续. 1.Row Editing in DataGrid 编辑,总是绑定不了checkbox的问题
- DataTable转换成IList 【转载】
链接:http://www.cnblogs.com/hlxs/archive/2011/05/09/2087976.html#2738813 留着学习 using System; using Syst ...
- STA分析(三) cmos模型
CMOS集成电路的基本结构是以P型材料作为衬底(p-substrate),直接生成NMOS, 同时增加N肼(n-well),在其上制造PMOS. 增加两个bulk(P+,N+)防止非MOS管内的PN结 ...
- 2018-2019-2 20165209 《网络对抗技术》Exp5:MSF基础应用
2018-2019-2 20165209 <网络对抗技术>Exp5:MSF基础应用 目录 一.基础问题回答和实验内容 二.攻击实例 主动攻击的实践 ms08_067 payload/gen ...