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, ...
随机推荐
- FileStream相关知识分享
一.如何理解FIleStream 通过前3章的学些,相信大家对于Stream已经有一定的了解,但是又如何去理解FileStream呢?请看下图: 我们磁盘中的任何文件都是通过二进制数组组成,最为直观的 ...
- 什么是ping通
ping这个命令是用来检测你的电脑和你所输入的IP地址127.0.01是否有数据通讯,以判断网络通不通的问题,执行这个命令也很简单,在开始——运行,输入ping 127.0.01,上面会出现一些数据, ...
- nyoj 63-小猴子下落 (模拟)
63-小猴子下落 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:2 submit:5 题目描述: 有一颗二叉树,最大深度为D,且所有叶子的深度都相同 ...
- codeblocks 调试
codeblocks 调试工具使用的注意事项: 1.codebloccks 调试,必须要在一个项目下才可以,也就是说“单独的文件是不能运行debug工具的” 2.项目的目录文件名必须是全英文,同时文件 ...
- shell配置文件
个人配置主要集中在-/.profile文件中 打开新的交互式shell时,配置文件的执行顺序是/etc/profile /etc/bashrc ~/.profile 最后是~/.bashrc ...
- 官宣!Amazon EMR正式支持Apache Hudi
Apache Hudi是一个开源的数据管理框架,其通过提供记录级别的insert, update, upsert和delete能力来简化增量数据处理和数据管道开发.Upsert指的是将记录插入到现有 ...
- 勾股数专题-SCAU-1079 三角形-18203 神奇的勾股数(原创)
勾股数专题-SCAU-1079 三角形-18203 神奇的勾股数(原创) 大部分的勾股数的题目很多人都是用for来便利,然后判断是不是平方数什么什么的,这样做的时候要对变量类型和很多细节都是要掌握好的 ...
- SpringBoot 项目脚手架
写在前面 之前也一直很少有写SpringBoot项目相关的文章,今天 准备整理一个我自己初始化SpringBoot项目时的一个脚手架,便于自己后面查阅.因为SpringBoot的约定大于配置,在整合各 ...
- vue 优化小技巧 之 require.context()
1.require.context() 回忆一下 当我们引入组件时 第一步 创建一个子组件 第二步 import ... form ... 第三步 components:{..} 第四步 页面使用 & ...
- 使用 Topshelf 组件一步一步创建 Windows 服务 (2) 使用Quartz.net 调度
上一篇说了如何使用 Topshelf 组件快速创建Windows服务,接下来介绍如何使用 Quartz.net 关于Quartz.net的好处,网上搜索都是一大把一大把的,我就不再多介绍. 先介绍需要 ...