题目:

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. str 操作

    str 认识字符串(重点, 多) 字符: 单一的文字符号 字符按照固定的顺序连成串 被' 或者" 或者''' 或者"""括起来的内容 索引 编号, 顺序 从0开 ...

  2. 《算法》第四章部分程序 part 15

    ▶ 书中第四章部分程序,包括在加上自己补充的代码,Kruskal 算法和 Boruvka 算法求最小生成树 ● Kruskal 算法求最小生成树 package package01; import e ...

  3. javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.util.Date'.

    开始是 javax.validation.UnexpectedTypeException: No validator could be found for type: java.lang.Intege ...

  4. Apache 服务器 基础知识小结

    Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软 ...

  5. scrapy执行流程

    def _next_request_from_scheduler(self, spider): fetch一个request,调用http11 handler,client的agent,返回的是def ...

  6. springBoot整合MongoDB(单机)

    依赖: <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mon ...

  7. [手把手教你] 用Swoft 搭建微服务(TCP RPC)

    序言 Swoft Framework 基于 Swoole 原生协程的新时代 PHP 全栈式协程框架 Swoft 是什么? Swoft 框架是首个基于Swoole 原生协程的新时代 PHP高性能协程全栈 ...

  8. Valgrind简单用法 (转)

    转自 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian Seward刚获得了今年的G ...

  9. 机器学习进阶-案例实战-图像全景拼接-书籍SIFT特征点连接 1.cv2.drawMatches(对两个图像的关键点进行连线操作)

    1.cv2.drawMatches(imageA, kpsA, imageB, kpsB, matches[:10], None, flags=2)  # 对两个图像关键点进行连线操作 参数说明:im ...

  10. redis异常和注意点

    目录: 1. 修改配置不起作用 2.Connection reset by peer: socket write error 3. redis-cli 查看中文乱码 1. 修改配置不起效果 我们修改了 ...