题目:

http://acm.hdu.edu.cn/showproblem.php?pid=6315

Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)

Problem Description
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...ar
2. query l r: query ∑ri=l⌊ai/bi⌋
 
Input
There 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≤100000, 1≤l≤r≤n, there're no more than 5 test cases.
 
Output
Output 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
 
题意:给定一个初始数组b和一个初始值全部为0的数组a,每次操作可以在给定的区间(l,r)内让a[i](l=<i<=r)加一,或者查询区间区间(l,r)中a[i]/b[i](l=<i<=r)(取整)的和。
思路:
用线段树存放a数组,做好最小更新标记,达到则向下更新
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=3e6+;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+;
using namespace std;
struct node{
LL l,r,sum,g,mi;
LL lazy;
LL mid(){
return (l+r)>>;
}
}a[maxn];
int b[maxn];
void build(int l,int r,int num){
a[num].l=l;
a[num].r=r;
a[num].lazy=;
if(l==r){
a[num].sum=;
a[num].g=;
a[num].mi=b[l];
}
else{
build(l,a[num].mid(),num<<);
build(a[num].mid()+,r,(num<<)|);
a[num].g=a[num<<].g+a[(num<<)|].g;
a[num].mi=min(a[num<<].mi,a[(num<<)|].mi);
}
}
void as(int d)
{
if(a[d].lazy)
{
a[(d<<)].lazy+=a[d].lazy;
a[(d<<|)].lazy+=a[d].lazy;
a[(d<<)].mi-=a[d].lazy;
a[(d<<|)].mi-=a[d].lazy;
a[d].lazy=;
}
}
LL Find(int l,int r,int num){
if(a[num].l==l&&a[num].r==r){
return a[num].g;
}
if(l>a[num].mid()){
return Find(l,r,(num<<)|);
}
else if(r<=a[num].mid()){
return Find(l,r,num<<);
}
else{
return Find(l,a[num].mid(),num<<)+Find(a[num].mid()+,r,(num<<)|);
}
}
void add(int l,int r,int num,LL x){
if(a[num].l==l&&a[num].r==r||x==){
a[num].lazy+=x;
a[num].mi-=x;
if(a[num].mi>){
return ;
}
else if(l!=r){
as(num);
add(l,a[num].mid(),num<<,);
add(a[num].mid()+,r,(num<<)|,);
a[num].mi=min(a[num<<].mi,a[(num<<)|].mi);
a[num].g=a[num<<].g+a[(num<<)|].g;
return;
}
}
if(l==r&&a[num].l==l&&a[num].r==r)
{
if(a[num].mi<=)
{
a[num].mi=a[num].lazy=;
a[num].mi=b[l];
a[num].g++;
}
return;
}
as(num);
if(l>a[num].mid()){
add(l,r,(num<<)|,x);
}
else if(r<=a[num].mid()){
add(l,r,num<<,x);
}
else {
add(l,a[num].mid(),num<<,x);
add(a[num].mid()+,r,(num<<)|,x);
}
a[num].mi=min(a[num<<].mi,a[(num<<)|].mi);
a[num].g=a[num<<].g+a[(num<<)|].g;
//cout<<'a'<<a[num].l<<' '<<a[num].r<<' '<<a[num].g<<endl;
}
int main(){
fio;
int n,m;
string op;
int x,y;
while(cin>>n>>m){
for(int i=;i<=n;i++){
cin>>b[i];
}
build(,n,);
while(m--){
cin>>op>>x>>y;
if(op[]=='a'){
add(x,y,,);
}
else if(op[]=='q'){ add(x,y,,);
cout<<Find(x,y,)<<endl;
}
}
}
}

HDU 6351 Naive Operations(线段树)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  7. hdu Naive Operations 线段树

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

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

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

  9. 2018HDU多校二 -F 题 Naive Operations(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315 In a galaxy far, far away, there are two integer ...

随机推荐

  1. Enterprise Library

    https://msdn.microsoft.com/en-us/library/ff648951.aspx

  2. Python :数据结构

    LearnPython :数据结构 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .labe ...

  3. twisted reactor执行流程

    #reactorbase的主循环 def mainLoop(self): while self._started: try: while self._started: # Advance simula ...

  4. Redis 五大数据类型及常用操作

    # 更详细的操作命令请查看 => http://redisdoc.com/ 1: KEY ( 键值 )  => 常见操作 2: String ( 字符串 ), 最大支持 512M 2.1: ...

  5. LinkedHashMap结构get和put源码流程简析及LRU应用

    原理这篇讲得比较透彻Java集合之LinkedHashMap. 本文属于源码阅读笔记,因put,get调用逻辑及链表维护逻辑复杂(至少网上其它文章的逻辑描述及配图,我都没看明白LinkedHashMa ...

  6. SonarQube与Jenkins结合提高代码质量

    首先,为什么需要SonarQube? 1.WriteClean Code 1)全局健康报告  2)关注新增的问题  3)强制QualityGate 4)Issue详情及建议- 详情参考:https:/ ...

  7. C++/C#:类Class与结构体Struct的区别

    C++中: 默认的访问控制.继承访问权限不同:struct时public的,class时 private的: 其它基本一样. C#中: struct是值类型,class是引用类型的: struct S ...

  8. javascript的DOM操作获取元素

    一.document.getElementById()    根据Id获取元素节点 <div id="div1"> <p id="p1"> ...

  9. Lazarus的DBGrid中回车键的处理

    Lazarus的DBGrid中回车键默认行为是向下移动一个记录,如果想对这一事件做处理,请不要在onkeypress里处理,而在onkeydown事件里处理.

  10. 1.ECS(CentOS7)主机名修改命令

    命令:hostnamectl 使用--help参数查看hostnamectl里面每个参数对应的含义: [root@localhost ~]# hostnamectl --help Query or c ...