【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 ...
随机推荐
- 【转】java序列化一定要应该注意的6个事项!
1.如果子类实现Serializable接口而父类未实现时,父类不会被序列化,但此时父类必须有个无参构造方法,否则会抛InvalidClassException异常. 2.静态变量不会被序列化,那是类 ...
- 1. UI Tests简介
(1) User Interface Testing UI Testing库主要提供了与App中的UI元素进行查找和交互的能力,这使得我们可以通过验证UI元素的状态来测试App是否正常运行. ...
- 搜索模板elasticsearch
搜索: like 对中文分词效率与支持都不太友好elasticsearch 实时的(效率高).分布式(可扩展)的搜索和分析引擎,基于Lucene全文搜索引擎工具包,算法基于倒排索引算法(eg:一篇文章 ...
- android 代码中及xml中设置透明
在布局文件的属性中,比如要设置一个LineaerLayout的背景为灰色透明.首先查RGB颜色表灰色是:#9E9E9E,AA代表透明,(透明度从00到FF,00表示完全透明),所以,设置其属性:and ...
- [Python學習筆記] 使用xlwings 插入註解 (forked 版本)
到今天為止 xlwings 還沒有插入註解的功能 去原始開發者的 Github Pull Requests 他說之前有人有建議要加入這個功能 但他還沒更新~ 如果需要使用 Python 來插入註解的話 ...
- APP设计细节总结-摘录
视觉表现型问题 1. 统一的图标设计风格 2. 图标大小的视觉平衡(根据图标的体量对其大小做出相应的调整) 3. 优化你的分割线(通常我们会选择浅色而否定深色) 4. 合理的运用投影的颜色与透明度 5 ...
- Pygame - Python游戏编程入门
>>> import pygame>>> print(pygame.ver)1.9.2a0 如果没有报错,应该是安装好了~ 如果报错找不到模块,很可能是安装版本的问 ...
- T1订正记-AC自动机-从树到图
AC自动机已经足够棒了. 但是,好像有时还是要TLE的. 一般的AC自动还是比较好,如果在某些情况下还是会被卡掉,像是这个水题 考试的感觉 我看到这个题后,我清清楚楚的知道,这是个AC自动机+栈. 经 ...
- 下拉列表事件 Dropdown iview
<Dropdown @on-click="export"> <Button icon='md-log-out'> 000l <Icon type=&q ...
- Ubuntu下压缩与解压各种文件的命令
1.压缩与解压xz文件 (1)压缩 xz -z filename (2)解压 xz -d filename.xz 2.压缩与解压tar文件 (1)压缩 tar -cvf filename(压缩到 ...