【HDOJ6315】Naive Operations(线段树,树状数组)
题意:
两个序列a和b,初始a[i]=0,b[i]给定且为一个1到n的排列,要求维护以下两种操作:
1.区间[L,R]内a[i]加1
2.询问[L,R]内a[i]/b[i](下取整)之和
n,q<=1e5
思路:

实际上a[i]/b[i]的线段树可以改为树状数组,因为只需要支持单点修改和前缀区间求和
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
const int N=;
int ad[N<<],mn[N<<],T[N],b[N],n,m,cas; void pushdown(int p)
{
if(ad[p])
{
ad[p<<]+=ad[p];
mn[p<<]+=ad[p];
ad[p<<|]+=ad[p];
mn[p<<|]+=ad[p];
ad[p]=;
}
} void pushup(int p)
{
mn[p]=min(mn[p<<],mn[p<<|]);
} void build(int l,int r,int p)
{
ad[p]=;
if(l==r)
{
mn[p]=b[l];
return;
}
int mid=(l+r)>>;
build(l,mid,p<<);
build(mid+,r,p<<|);
pushup(p);
} void modify(int x)
{
for(int i=x;i<=n;i+=i&-i) T[i]++;
} int ask(int x)
{
int ans=;
for(int i=x;i;i-=i&-i) ans+=T[i];
return ans;
} void update(int l,int r,int x,int y,int p)
{
if((x<=l)&&(r<=y))
{
ad[p]--;
mn[p]--;
return;
}
pushdown(p);
int mid=(l+r)>>;
if(x<=mid) update(l,mid,x,y,p<<);
if(y>mid) update(mid+,r,x,y,p<<|);
pushup(p);
} void clear(int l,int r,int p)
{
if(l==r)
{
if(!mn[p])
{
mn[p]=b[l];
modify(l);
}
return;
}
pushdown(p);
int mid=(l+r)>>;
if(!mn[p<<]) clear(l,mid,p<<);
if(!mn[p<<|]) clear(mid+,r,p<<|);
pushup(p);
} int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++) scanf("%d",&b[i]);
build(,n,);
for(int i=;i<=n;i++) T[i]=;
for(int i=;i<=m;i++)
{
char ch[];
scanf("%s",ch);
if(ch[]=='a')
{
int l,r;
scanf("%d%d",&l,&r);
update(,n,l,r,);
clear(,n,);
}
else
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",ask(r)-ask(l-));
}
}
}
return ;
}
【HDOJ6315】Naive Operations(线段树,树状数组)的更多相关文章
- 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, ...
- HDU6315 Naive Operations(线段树 复杂度分析)
题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...
- 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 ...
- HDU6315 Naive Operations 线段树
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. Solution ...
随机推荐
- 将vue-cli项目配置在nginx上
登录使用的是node.js搭建的注册登录接口,关于对数据库的读写则是用spring boot的框架来实现的. 1.首先是vue-cli项目里的前端页面的配置: location / { root ...
- 如何正确理解关键字"with"与上下文管理器
转自:https://foofish.net/with-and-context-manager.html 如果你有阅读源码的习惯,可能会看到一些优秀的代码经常出现带有 “with” 关键字的语句,它通 ...
- ios https 安全证书配置
原定于2017年1月1日起所有提交到 App Store 的App必须强制开启 ATS,需要配置Https.但是现在不需要了,无固定期限的往后延期了,但是这个还是得弄明白下为好,说不定哪天突然就让弄了 ...
- 《Python基础教程》 读书笔记 第五章(上)条件语句
5.1 print和import的更多信息 5.1.1使用逗号输出 打印多个表达式也是可行的,只要将它们用逗号隔开就好: >>>print'age:',42 age: 42 要同时输 ...
- ASP.NET MVC IIS7 403.14-Forbidden
问题描述 IIS 7上发布ASP.NET MVC程序报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 折腾了半天,提示里面的解决方法是: 如果不希望启用目录浏览,请确保 ...
- How to proxy a web site by apache2 in Ubuntu
Install apache2 To execute the install command in terminal: sudo apt-get install apache2 Then, we ca ...
- C# 移动控件
最近要做车牌识别的,不同地区收费标准不一,所以想做个可以移动控件来给客户选择停车场收费条件的. 首先因为要自动排序控件选FlowLayoutPanel做容器,加若干Panel和FlowLayout ...
- 在ubuntun虚拟机里安装goLang语言编程环境
Go语言是谷歌2009发布的第二款开源编程语言. Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全.支持并行进程. 北京时间2010年 ...
- 配置maven报错 the java_home environment variable is not defined correctly ......
the java_home environment variable is not defined correctly This environment variable is needed to r ...
- mysql中ibatis的limit动态传参
param.put("pageNo",pageNo); param.put("pageSize",pageSize); sqlMap中的用法 limit $ ...