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, ...
随机推荐
- 删除TFS上的团队项目
Visual Studio 提供了一个工具 在X:\X\Microsoft Visual Studio X\Common7\IDE Visual Studio安装路径 下 TFSDeletepr ...
- nyoj 204-Coin Test (python count)
204-Coin Test 内存限制:64MB 时间限制:3000ms 特判: No 通过数:2 提交数:2 难度:1 题目描述: As is known to all,if you throw a ...
- MySQL/MariaDB读写分离配置
DB读写分离描述 数据库的读写分离其实就是为了加减少数据库的压力:数据库的写入操作由主数据库来进行,读取操作由从数据库来进行操作.实现数据库读写分离技术是有很多方法的,在这里我就用一个比较简单的mys ...
- ArcGIS API For Javascript :读取 CSV 文件的方法
我们临时会遇到一些测试数据,通常从数据库中以 CSV 格式导出.最简单实用的方法就是使用 ajax 去读取文件,记得引入 jQuery . 例如,在<ArcGIS JS API :新增一个热力图 ...
- vue—自定义指令
今日分享—自定义指令 需要学习的点: modifiers属性的具体实例就是v-on:click.stop=”handClick” 一样,为指令添加一个修饰符. 全局指令:新建一个newDir.js i ...
- git 删除误上传的.idea文件
问题: 提交项目的时候忘记添加.gitignore文件,误上传了文件(如.idea)如何解决?(本文以.idea文件夹举例) 1.将项目文件拉取下来 git pull origin master 2. ...
- 2019-11-24:postgresql数据库安装,最后报错failed to load SQLModule 问题的解决方案
安装环境:Windows 10 问题描述:Failed to load sql modules into the database cluster 原因在于 Postgresql 没有安装完全. 解决 ...
- 常见 MIME 类型列表(转)
本文转自:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MI ...
- linuxshell编程之数组和字符串处理工具
数组:存放多个元素的连续内存空间. 声明数组:bash-4以后支持除默认的0,1,2……还可以自定义索引格式,此类数组称之为“关联数组” 声明索引数组:declare -a NAME 声明关联数组:d ...
- ReactRouter中HashRouter和BrowserRouter的区别
仅个人理解,如有不当请指正 一.从原理上 HashRouter在路径中包含了#,相当于HTML的锚点定位.(# 符号的英文叫hash,所以叫HashRouter,和散列没关系哦)) 而BrowserR ...