2018HDU多校二 -F 题 Naive Operations(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...aral,al+1...ar
2. query l r: query ∑ri=l⌊ai/bi⌋∑i=lr⌊ai/bi⌋
InputThere are multiple test cases, please read till the end of input file.
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
1≤n,q≤1000001≤n,q≤100000, 1≤l≤r≤n1≤l≤r≤n, there're no more than 5 test cases.
OutputOutput the answer for each 'query', each one line.
Sample Input
5 12
1 5 2 4 3
add 1 4
query 1 4
add 2 5
query 2 5
add 3 5
query 1 5
add 2 4
query 1 4
add 2 5
query 2 5
add 2 2
query 1 5
Sample Output
1
1
2
4
4
6
题意:题目给你N个数,Q个操作,另外有个数组a,a 的初始值都是0,然后Q个操作,若是add 则在区间x~y之间的a[]都加一,query 就查找l~r之间 ∑ri=l⌊ai/bi⌋∑i=lr⌊ai/bi⌋;
题解: 由于是取下界,我们可以求每个区间内距离该位置上b[i]值最近的数,然后没加一,就把b[i]减一,如果最小值为零,就出现了a[i]/b[i]==1的情况,就将区间的sum加一,对于
每个查询操作,我们只要求区间的sun和即可; 参考代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int n,q,x,y,b[maxn];
char str[]; struct Node{
int l,r,sum,tag,minnumm;
} tree[maxn<<]; void build(int l,int r,int pos)
{
tree[pos].l=l,tree[pos].r=r;
if(l==r)
{
tree[pos].minnumm=b[l];
tree[pos].sum=;
tree[pos].tag=;
return ;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
tree[pos].minnumm=min(tree[pos<<].minnumm,tree[pos<<|].minnumm);
tree[pos].sum=tree[pos<<].sum+tree[pos<<|].sum;
tree[pos].tag=tree[pos<<].tag+tree[pos<<|].tag;
} void pushdown(int pos)
{
tree[pos<<].minnumm+=tree[pos].tag;
tree[pos<<|].minnumm+=tree[pos].tag;
tree[pos<<].tag+=tree[pos].tag;
tree[pos<<|].tag+=tree[pos].tag;
tree[pos].tag=;
} void update(int pos,int l,int r,bool temp)
{
if(tree[pos].l==l&&tree[pos].r==r)
{
if(temp)
{
tree[pos].tag--;
tree[pos].minnumm--;
}
if(tree[pos].minnumm>) return ;
if(tree[pos].l==tree[pos].r)
{
if(tree[pos].minnumm==) tree[pos].minnumm=b[tree[pos].l],tree[pos].sum++;
return ;
}
temp=false;
} if(tree[pos].tag) pushdown(pos); int mid=(tree[pos].l+tree[pos].r)>>;
if(r<=mid) update(pos<<,l,r,temp);
else if(l>=mid+) update(pos<<|,l,r,temp);
else update(pos<<,l,mid,temp),update(pos<<|,mid+,r,temp); tree[pos].minnumm=min(tree[pos<<].minnumm,tree[pos<<|].minnumm);
tree[pos].sum=tree[pos<<].sum+tree[pos<<|].sum;
} int query(int pos,int l,int r)
{
if(tree[pos].tag) pushdown(pos);
if(tree[pos].l==l&&tree[pos].r==r) return tree[pos].sum;
int mid=(tree[pos].l+tree[pos].r)>>,ans=;
if(r<=mid) ans+=query(pos<<,l,r);
else if(l>=mid+) ans+=query(pos<<|,l,r);
else ans+=query(pos<<,l,mid)+query(pos<<|,mid+,r);
return ans;
} int main()
{
while(~scanf("%d%d",&n,&q))
{
for(int i=;i<=n;i++) scanf("%d",b+i);
build(,n,);
while(q--)
{
scanf("%s%d%d",str,&x,&y);
if(str[]=='a') update(,x,y,true);
else if(str[]=='q') printf("%d\n",query(,x,y));
}
}
return ;
}
2018HDU多校二 -F 题 Naive Operations(线段树)的更多相关文章
- 杭电多校第二场 hdu 6315 Naive Operations 线段树变形
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- 2019牛客多校第八场 F题 Flowers 计算几何+线段树
2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...
- hdu Naive Operations 线段树
题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...
- HDU6315 Naive Operations(线段树 复杂度分析)
题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...
- 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(线段树+复杂度均摊)
发现每次区间加只能加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的区间和. 思路:线 ...
- 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, ...
随机推荐
- MySQL57安装与设置
安装MySQL 添加mysql源 [root@localhost ~]# rpm -ivh http://repo.mysql.com//mysql57-community-release-el7-7 ...
- Docker swarm集群增加节点和删除节点
Docker swarm集群增加节点 docker swarm初始化 docker swarm init docker swarm 增加节点 在已经初始化的机器上执行:# docker swarm j ...
- CSS(7)--- 通俗讲解清除浮动
CSS(7)--- 通俗讲解清除浮动 上一篇讲了CSS浮动 博客地址:CSS(6)---通俗讲解浮动(float) 一.理解清除浮动 1.为什么要清除浮动 我们前面说过,浮动本质是用来做一些文字混排效 ...
- [java笔记] 最近学的一些笔记
1.@Override的用法 2.父类的返回值类型的范围,与子类返回值类型的返回的大小关系: 3.子类方法的权限修饰符,与子类方法的权限修饰符: 4.如果p1是一个对象,p2也是个对象,那么代码p1= ...
- 人人都懂区块链--pdf电子版学习资料下载
人人都懂区块链 21天从区块链“小白”到资深玩家电子版pdf下载 链接:https://pan.baidu.com/s/1TWxYv4TLa2UtTgU-HqLECQ 提取码:6gy0 好的学习资料需 ...
- mysql初级了解
mysql是一个关系型数据库系统,可以存放若干个数据库,每个数据库中 可以存放若干张表,每张表中可以存放若干条记录 基本代码: 1.查看数据库 show databases: 2.创建数据库 ...
- WPS Office 2012专业版与WPS2019政府云办公增强版下载安装与体验
WPS Office 2012专业版与WPS2019政府云办公增强版下载安装与体验 一.WPS Office 2012专业版. 优点:没有广告,很清爽,界面很人性化.是我于2019年11月找出来安装测 ...
- React传值,验证值的类型和默认值
const ele = <Ff const={'哈哈'} index={55}></Ff> let box = document.querySelector('#app') / ...
- vant-ui的van-area使用
由于官方例子中并没有太多详情,因此记录之,方便以后使用. 1.配置 :area-list="areaList",以初始化全部省市区的数据,其中area.js文件在官方可以下载,放于 ...
- HTML学习 day02
1.HTML的相关概念 网站建设流程 网页组成 网页主要由三部分组成:结构(Structure).表现(Presentation)和行为(Behavior). html(Hypertext Mark ...