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, ...
随机推荐
- Vue项目性能优化整理
以下方式基于 @vue/cli 快速搭建的交互式项目脚手架 1. 路由懒加载 当打包构建应用时,JavaScript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分割成不同的代码块,然 ...
- Matlab 文件格式化/Matlab Source File Formator
由于需要使用到别人编写的Matlab代码文件,但是呢不同的人有不同的风格,有的写得就比较糟糕了. 为了更好地理解代码的内容,一个比较美观的代码会让人身心愉悦. 但是在网上并没有找到一个比较好的实现,此 ...
- 公众号第三方平台开发 component_verify_ticket和accessToken的获取
公众号第三方平台审核通过之后,微信的服务器会定时(10分钟一次)给"授权事件接收URL"发送component_verify_ticket,这里我们需要及时更新component_ ...
- 堡垒机的核心武器:WebSSH录像实现
WebSSH终端录像的实现终于来了 前边写了两篇文章『Asciinema:你的所有操作都将被录制』和『Asciinema文章勘误及Web端使用介绍』深入介绍了终端录制工具Asciinema,我们已经可 ...
- MySQL 1364 错误提示:#1364 - Field "details" doesn't have a default value
原因:mysql字段设计的时候为not null,结果此字段没有插入值,解决方法: 运行以下命令. SET @@GLOBAL.sql_mode="NO_AUTO_CREATE_USER,NO ...
- lldb调试使用python脚本问题总结
lldb调试器可以使用python脚本实现功能增强,但也不是可以随心所欲的,在实际中有很多地方需要注意. 首先是对多线程环境调试使用python脚本,也要考虑python脚本有多线程安全,尤其是有许多 ...
- [Part 4] 在Windows 10上源码编译PCL 1.8.1支持VTK和QT,可视化三维点云
本文首发于个人博客https://kezunlin.me/post/2d809f92/,欢迎阅读! Part-4: Compile pcl with vtk qt5 support from sour ...
- MAC终端中tree命令
Mac没有自带的tree命令,需要额外安装才可以,操作方法有两种: 一.用find命令模拟tree效果 1.mac下默认是没有 tree命令的,不过我们可以使用find命令模拟出tree命令的效果,如 ...
- bind cname
$TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS ...
- windows下搭建dubbo 环境(dubbo-admin和服务提供者消费者)
---恢复内容开始--- 一. dubbo-admin管理控制台 从 https://github.com/apache/dubbo-admin clone项目到本地. 修改dubbo-admin- ...