题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315

In a galaxy far, far away, there are two integer sequence a and b of length n. 
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(线段树)的更多相关文章

  1. 杭电多校第二场 hdu 6315 Naive Operations 线段树变形

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  2. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

  3. hdu Naive Operations 线段树

    题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...

  4. HDU6315 Naive Operations(线段树 复杂度分析)

    题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...

  5. 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,另一种操 ...

  6. HDU 6315 Naive Operations(线段树区间整除区间)

    Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...

  7. HDU 6315 Naive Operations(线段树+复杂度均摊)

    发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...

  8. HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树

    hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...

  9. 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, ...

随机推荐

  1. 从EFCore上下文的使用到深入剖析DI的生命周期最后实现自动属性注入

    故事背景 最近在把自己的一个老项目从Framework迁移到.Net Core 3.0,数据访问这块选择的是EFCore+Mysql.使用EF的话不可避免要和DbContext打交道,在Core中的常 ...

  2. 【原创】使用批处理脚本生成包并自动上传到nuget

    Hello 大家好,我是TANZAME,我们又见面了. NuGet 是什么这里就不再重复啰嗦,园子里一搜一大把.今天要跟大家分享的是,在日常开发过程中如何统一管理我们的包,如何通过批处理脚本生成包并自 ...

  3. Algorithm: GCD、EXGCD、Inverse Element

    数论基础 数论是纯数学的一个研究分支,主要研究整数的性质.初等数论包括整除理论.同余理论.连分数理论.这一篇主要记录的是同余相关的基础知识. 取模 取模是一种运算,本质就是带余除法,运算结果就是余数. ...

  4. nyoj 991 Registration system (map)

    Registration system 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 A new e-mail service "Berlandesk&q ...

  5. synchronized:内部锁

    synchronized:内部锁 起源: 并行程序开发涉及多线程.多任务间的协作和数据共享 一).内部锁:synchronized 1).定义在方法上 public synchronized void ...

  6. 真的,Kafka 入门一篇文章就够了

    初识 Kafka 什么是 Kafka Kafka 是由 Linkedin 公司开发的,它是一个分布式的,支持多分区.多副本,基于 Zookeeper 的分布式消息流平台,它同时也是一款开源的基于发布订 ...

  7. xpath选择兄弟节点、返回上一级和选择多个属性

    本文链接:https://blog.csdn.net/ZincZhang/article/details/80248297选择兄弟节点选择前N位的div标签 preceding-sibling::di ...

  8. 使用selenium模拟登陆新浪微博

    1.selenium基本使用 1.selenium安装及基本操作 selenium是一个自动化测试工具,它支持各种浏览器,包括Chrome,Safari,Firefox等主流界面浏览器驱动,也包括Ph ...

  9. 使用FastReport报表工具生成图片格式文档

    之前我在随笔<使用FastReport报表工具生成报表PDF文档>介绍过使用FastReport.Net来根据报表模板进行生成PDF,以及随笔<使用FastReport报表工具生成标 ...

  10. JavaScript-----3.变量

    1.变量的使用 变量在使用的时候分两步:1. 声明变量 2. 赋值 1.1声明变量 //声明变量 var age;//声明一个名字为age的变量 var是JS的一个关键字,用于声明变量,使用该关键字声 ...