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. zabbix部署(1)(lnmp转)

    1.lnmp 首先 确保CentOS7上网络配置正确,可以正常访问互联网. 确保已经关闭了iptables. CentOS7上是firewall,关闭命令: 1 2 # systemctl stop  ...

  2. OpenGL笔记(三) GLSL语法与内建函数

    GLSL,OpenGL Shading Language,GLSL中没有指针,并且没有任何类型的字符串或字符. (1)GLSL的修饰符与基本数据类型 const:用于声明非可写的编译时常量变量: at ...

  3. STM32 & FreeRTOS & KFIFO (巧夺天工)

    巧夺天工 的 KFIFO ,用STM32实现. 实现源文件如下: /********************************************************** * * 文件名 ...

  4. 利尔达推出工控解决方式 串口转以太网模块LSD1ES-W5500_S2E0

    利尔达最近推出工控解决方式,串口转以太网模块LSD1ES-W5500_S2E0,模块基于WIZnet-W5500. 同一时候,这也是利尔达科技集团成为WIZnet代理商后,自行推出的第一款基于WIZn ...

  5. [Baltic2013]ballmachine BZOJ3133

    分析: 我们考虑,因为每次放置的时候,都是向子树中含有的编号最小的哪一个走,那么放置的顺序是固定的,我们将边以to的子树最小排序,之后得到的出栈序就是球的放入顺序.目测可以使用堆来实现,线段树也能实现 ...

  6. Flutter - 添加从左向右滑动,返回上一个页面

    很多App比如微信.IT之家等都支持从屏幕左侧向右滑动,来返回上一个页面. 很多iOS上的App也都支持. 那么这个神奇的手势滑动是怎么实现的呢? 其实非常简单,只需要添加一句话即可. platfor ...

  7. 20155202张旭 Exp3 免杀原理与实践

    20155202张旭 Exp3 免杀原理与实践 AV厂商检测恶意软件的方式主流的就三种: 基于特征码的检测 启发式恶意软件检测 基于行为的恶意软件检测 我们要做的就是让我们的恶意软件没法被这三种方式找 ...

  8. Spring-data-jpa 学习笔记(一)

    Spring家族越来越强大,作为一名javaWeb开发人员,学习Spring家族的东西是必须的.在此记录学习Spring-data-jpa的相关知识,方便后续查阅. 一.spring-data-jpa ...

  9. [Oracle]System 表空间的文件丢失

    如果system 表空间的文件丢失,假设有备份的情况,可以恢复.数据库需要设置为mount 状态,然后restore/recover datafile 模拟实验: SQL> select nam ...

  10. mfc 类三种继承方式下的访问

    知识点 public private protected 三种继承方式 三种继承方式的区别 public 关键字意味着在其后声明的所有成员及对象都可以访问. private 关键字意味着除了该类型的创 ...