Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

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
 
思路:
很明显的线段树,难点在于如何处理ai/bi的向下取整,我们可以对bi建一棵线段树,那么每次add(l,r)操作我们就可以抽象成(l,r)减一,如果区间【l,r】中有数字变为0,则表示b[i]这个数+1,再建一棵线段树储存每个区间+1的信息并将这个数变为原先的值继续循环就好了,querry(l,r),就直接在第二棵线段树上求区间值的和就可以了。
 
之前想到这个思路一直不敢写,感觉会超时,后面问了一下学长,复杂度没问题就AC了 。
实现代码:
 
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int inf = 0x3f3f3f3f;
const int M = 1e5+;
double lazy[M<<];
double b[M],num[M],sum[M<<],minn[M<<];
vector<int>v;
void pushup(int rt){
minn[rt] = min(minn[rt<<],minn[rt<<|]);
} void pushdown(int rt){
if(lazy[rt]){
lazy[rt<<] += lazy[rt];
lazy[rt<<|] += lazy[rt];
minn[rt<<] -= lazy[rt];
minn[rt<<|] -= lazy[rt];
lazy[rt] = ;
}
} void build(int l,int r,int rt){
lazy[rt] = ; minn[rt] = inf;
if(l == r){
minn[rt] = b[l];
return ;
}
mid;
build(lson);
build(rson);
pushup(rt);
} void update(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
minn[rt] -= ;
lazy[rt] += ;
return ;
}
pushdown(rt);
mid;
if(L <= m) update(L,R,lson);
if(R > m) update(L,R,rson);
pushup(rt);
} void update2(int p,int l,int r,int rt){
if(l == r){
minn[rt] = b[l];
return ;
}
mid;
pushdown(rt);
if(p <= m) update2(p,lson);
else update2(p,rson);
pushup(rt);
} int Query(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return minn[rt];
}
mid;
pushdown(rt);
int ret = inf;
if(L <= m) ret = min(Query(L,R,lson),ret);
if(R > m) ret = min(Query(L,R,rson),ret);
return ret;
} void query1(int l,int r,int rt){
if(l == r){
v.push_back(l);
return ;
}
mid;
pushdown(rt);
pushdown(rt);
if(minn[rt<<] == ) query1(lson);
if(minn[rt<<|] == ) query1(rson);
} void pushup1(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
} void build1(int l,int r,int rt){
sum[rt] = ;
if(l == r){
sum[rt] = ;
return ;
}
mid;
build1(lson);
build1(rson);
pushup1(rt);
} void update1(int p,int l,int r,int rt){
if(l == r){
sum[rt] += ;
return ;
}
mid;
if(p <= m) update1(p,lson);
else update1(p,rson);
pushup1(rt);
} int query(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return sum[rt];
}
mid;
int ret = ;
if(L <= m) ret += query(L,R,lson);
if(R > m) ret += query(L,R,rson);
return ret;
} int main()
{
int n,q,x,l,r;
char op[];
while(scanf("%d%d",&n,&q)!=EOF){
for(int i = ;i <= n;i ++){
scanf("%d",&x);
b[i] = x;
}
build(,n,);
build1(,n,);
while(q--){
scanf("%s",op);
if(op[] == 'a'){
//cout<<"update"<<endl;
scanf("%d%d",&l,&r);
update(l,r,,n,); //将第一棵树的l,r区间-1
int cnt = Query(l,r,,n,);//求第一棵树的l,r区间的最小值
//cout<<"cnt: "<<cnt<<endl;
if(cnt == ){ //如果区间最小值为0
query1(,n,); //找到区间所有0的下表寸进vector里
for(int i = ;i < v.size(); i++){ //遍历
//cout<<"v[i] :"<<v[i]<<endl;
update1(v[i],,n,); //在第二颗数记下更新值
update2(v[i],,n,); //将现在为0的数变成原来的值
}
v.clear();
}
}
else {
//cout<<"query"<<endl;
scanf("%d%d",&l,&r);
int ans = query(l,r,,n,);
printf("%d\n",ans);
}
}
}
return ;
}

hdu 6315 Naive Operations (2018 Multi-University Training Contest 2 1007)的更多相关文章

  1. HDU 6315 Naive Operations(线段树+区间维护)多校题解

    题意:a数组初始全为0,b数组题目给你,有两种操作: 思路:dls的思路很妙啊,我们可以将a初始化为b,加一操作改为减一,然后我们维护一个最小值,一旦最小值为0,说明至少有一个ai > bi,那 ...

  2. HDU 6351 Naive Operations(线段树)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=6315 Naive Operations Time Limit: 6000/3000 MS (Java/O ...

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

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

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

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

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

  6. HDU 6315: Naive Operations

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

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

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

  8. HDU 6315 Naive Operations 【势能线段树】

    <题目链接> 题目大意: 给出两个序列,a序列全部初始化为0,b序列为输入值.然后有两种操作,add x y就是把a数组[x,y]区间内全部+1,query x y是查询[x,y]区间内∑ ...

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

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

随机推荐

  1. C# 取两个集合的交集\并集\差集

    交集:Intersect 并集:Union 差集:Except , , , , , }; , , , ,,, }; var C= A.Intersect(B); //交集 { 3, 4, 5, 6 } ...

  2. 解决Win10家庭版没有‘本地用户和组’问题

    今天偶然发现我的win10系统是家庭版,并且没有本地用户和组. 处理方法:将系统升至为win10专业版,然后下载microKMS_v17.02.14做的激活.参考网站 1.打开运行窗口,输入 gped ...

  3. Java关键字(二)——native

    本篇博客我们将介绍Java中的一个关键字——native. native 关键字在 JDK 源码中很多类中都有,在 Object.java类中,其 getClass() 方法.hashCode()方法 ...

  4. 在win10下使用docker快速搭建ruby开发环境

    docker在windows下发力的时候必将取代各种虚拟机,并改变程序员的开发习惯,或许还会改变infra的工作. 概要: 在Windows下搭建开发环境一直是infra(我)头疼的事情.为了解决这个 ...

  5. 20155333 《网络对抗》 Exp5 MSF基础应用

    20155333 <网络对抗> Exp5 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode exploit:攻击手段,是能使攻击武器(payl ...

  6. 原创zynq文章整理(MiZ702教程+例程)

    MiZ702教程+例程  网盘链接:  http://pan.baidu.com/s/1sj23yxv 不时会跟新版本,增加勘误之类的,请关注--

  7. PowerBI开发 第八篇:查询参数

    在PowerBI Desktop中,用户可以定义一个或多个查询参数(Query Parameter),参数的功能是为了实现PowerBI的参数化编程,使得Data Source的属性.替换值和过滤数据 ...

  8. 用opencv实现工控机的开机录像

    需要训练一个神经网络模型,可能需要用到很多视频数据,所以我想把手头的工控机设置为上电自启动,再借助opencv编译一个可执行文件,放在windows开机启动文件夹里,这样只要连接好摄像头和工控机以及电 ...

  9. Jenkins下载安装

    Jenkins是什么? Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成.集成Jenkins可以用于一些测 ...

  10. Unity特殊路径

    Resources: Resources文件可以在根目录下,也可以在子目录下,只要叫Resources就好.Resources目录下所有资源将被打包进游戏存放资源的archive中,Resources ...