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. day53

    JS常用类 一.Number 1.常用数字 整数:10 小数:3.14 科学计数法:1e5 | 1e-5 正负无穷:Infinity | -Infinity 2.常用进制 二进制:0b1010 八进制 ...

  2. VBA 连接,提醒 rs AS new adodb.recordset 的变量未定义

    解决方法: 菜单-工程-引用Microsoft ActiveX Data Objects 2.x Library 定位……msado15.dll

  3. SonarQube6.7.4安装部署

    1.准备工作 https://www.sonarqube.org Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具.比如p ...

  4. WPF listview Test Message list

    UI: <Window x:Class="WoZhuLianyuanTool.SendContentsWind" xmlns="http://schemas.mic ...

  5. 云计算背后的秘密:NoSQL诞生的原因和优缺点

    转载收藏一篇对nosql讲解的比较全面的文章:http://blog.csdn.net/xlgen157387/article/details/47908797 这篇文章将和大家聊聊为什么NoSQL会 ...

  6. LoRa---官方例程移植

    SX1278芯片上移植Semtech官方PING-PONG例程 移植环境:keil5.20 硬件平台:stm32f051+sx1278 1.下载源码:Semtech官网下载最新例程链接:http:// ...

  7. app.use( )做一个静态资源服务

    var express = require("express"); var app = express(); //静态服务 app.use("/jingtai" ...

  8. 手撸orm框架

    一 前言 1 我在实例化一个user对象的时候,可以user=User(name='lqz',password='123') 2 也可以 user=User() user['name']='lqz' ...

  9. R绘图 第五篇:绘制散点图(ggplot2)

    ggplot2包中绘制点图的函数有两个:geom_point和 geom_dotplot,当使用geom_dotplot绘图时,point的形状是dot,不能改变点的形状,因此,geom_dotplo ...

  10. 小知识点--crontab

    前言 这两周学了很多东西,还把golang语言基本掌握了,收获还是挺多的.在做安全的过程中,有很多需要定时执行的任务,比如抓取主机数量,端口数据等,这都逃不开linux中的crontab命令,今天分享 ...