原题地址

Naive Operations

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

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
 
题意:给两个长度为N 数组 A和B,B是1-N的某一种排序,A全为0;
两种操作:
1.给你l,r 把数组A[l]到A[r]全部加1
2.给你l,r求A[l]/B[l]一直加到A[r]/B[r]的和;
 
思路:
数据量十万所以复杂度只能在NlogN到Nsqrt(N)左右;
而且很自然想到线段树操作;
区间加法 线段树的基本操作;
但是没办法每次除法都要处理到叶子节点;
所以如何处理除法是关键;
在比赛中想了无数办法,因为各种中途脑子短路和智商不行而打不出来(太蠢
✔正确做法 利用线段树维护区间最小值,每次区间加1时最小值减去1,当区间最小值减为0时说明这个区间中有叶子节点的A[X]/B[X]多了1;然后递归下去找到这个节点加1,跟他关联的区间的值都加上1;
所以复杂度每次区间加法和查询为logn寻找最小值减为0的叶子节点复杂度为logn 所以总复杂度为(NlogN*logN)
 
代码:
#include<bits/stdc++.h>

using namespace std;

#define ls (id<<1)
#define rs ((id<<1)|1)
#define mid ((l+r)>>1)
typedef long long LL; const int maxn=+;
int n,q;
int b[maxn];
struct Node{ int mina;
int valb;
int zero_sum;
int id;
int lazy; }node[maxn<<];
void pushdown(int id,int l,int r){//往下更新
node[ls].lazy+=node[id].lazy;
node[rs].lazy+=node[id].lazy;
node[ls].mina-=node[id].lazy;
node[rs].mina-=node[id].lazy;
node[id].lazy=;
}
void pushup(int id,int l,int r){//往上更新
node[id].zero_sum=node[ls].zero_sum+node[rs].zero_sum;
node[id].mina=min(node[ls].mina,node[rs].mina);
}
void build(int l,int r,int id){
node[id].zero_sum=node[id].lazy=;
if(l==r){ node[id].valb=node[id].mina=b[l];
//cout<<"id=="<<id<<" l=="<<l<<" r=="<<r<<"node[id].mina="<<node[id].mina<<endl;
return;
}
build(l,mid,ls);
build(mid+,r,rs);
pushup(id,l,r);
}
void update(int l,int r,int id,int ql,int qr){
//cout<<"id=="<<id<<" l=="<<l<<" r=="<<r<<"node[id].mina="<<node[id].mina<<endl;
if(l>r)return;
if(node[id].mina>&&ql<=l&&qr>=r){///到时候修改一下!!有包含的区间并且不存在要为0的叶子节点;
node[id].lazy++;
node[id].mina--;
return;
}
if(l==r&&node[id].mina==){//正好整除的叶子节点
node[id].zero_sum++;
node[id].lazy=;
node[id].mina=node[id].valb;
return;
}
if(node[id].lazy)pushdown(id,l,r);
if(qr<=mid)update(l,mid,ls,ql,qr);
else if(ql>mid)update(mid+,r,rs,ql,qr);
else{
update(l,mid,ls,ql,qr);
update(mid+,r,rs,ql,qr);
}
pushup(id,l,r);
}
int query(int l,int r,int id,int ql,int qr){
// cout<<"id=="<<id<<" l=="<<l<<" r=="<<r<<"node[id].mina="<<node[id].mina<<endl;
if(l>r)return ;
if(ql<=l&&r<=qr)return node[id].zero_sum;//被需要查询包含的区间;
/*if (node[id].mina <= 0) {
update(1, 1, n, ql, qr);
}*/
if(qr<=mid)return query(l,mid,ls,ql,qr);
else if(ql>mid)return query(mid+,r,rs,ql,qr);
else{
return query(l,mid,ls,ql,qr)+query(mid+,r,rs,ql,qr);
}
}
int main()
{
std::ios::sync_with_stdio(false);
while(cin>>n>>q){
for(int i=;i<=n;i++)cin>>b[i];
build(,n,);
while(q--){
char s[];
int ll,rr;
cin>>s>>ll>>rr;
if(s[]=='a')update(,n,,ll,rr);
else
cout<<query(,n,,ll,rr)<<endl;
}
}
return ;
}

HDU-6315 Naive Operations//2018 Multi-University Training Contest 2___1007 (线段树,区间除法)的更多相关文章

  1. HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

    http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...

  2. hdu 6315 Naive Operations (2018 Multi-University Training Contest 2 1007)

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

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

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

  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(线段树区间整除区间)

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

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

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

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

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

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

随机推荐

  1. appium-手势密码实现-automationName 是Appium的情况

    1. 红色区域的范围为:[66,575][1014,1523], 由于这块是一个整块,所以无法使用每个点的数据:因此只能使用LockPatternView对象拿到左上角的坐标值 2.  原理, 将九宫 ...

  2. 不吹不擂,你想要的Python面试都在这里了【315+道题】+精心整理的解答

    Part01-Py基础篇(80) Part02-网络编程和并发(34) Part03-数据库和缓存(46) Part04-前端框架和其他(155) Part01-Py基础篇(80) 1.为什么学习Py ...

  3. Android之测试相关知识点

    程序员在开发的过程中一定要进行严格的测试: --->相关概念 * 根据是否知道源代码可以分为: 黑盒测试:只关心程序执行的过程和结果并不知道程序源代码. 白盒测试: 根据源代码写测试方法 或者 ...

  4. 更换checkbox的原有样式

    通常情况下,各个浏览器对的样式不一致,并且不那么美观.所以有时候设计需要我们更换原有的样式: html: <span><input type="checkbox" ...

  5. jsp页面提示“Multiple annotations found at this line: - The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path”解决方案

    Multiple annotations found at this line: - The superclass "javax.servlet.http.HttpServlet" ...

  6. reinterpret_cast and const_cast

    reinterpret_cast reinterpret意为“重新解释” reinterpret_cast是C++中与C风格类型转换最接近的类型转换运算符.它让程序员能够将一种对象类型转换为另一种,不 ...

  7. HDU 1937 J - Justice League

    J - Justice League Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  8. [bzoj] 1043 下落的圆盘 || 圆上的“线段覆盖”

    原题 n个圆盘,求下落后能看到的总周长. 红色即为所求 借鉴于黄学长的博客 对于每下落的一个圆盘,处理他后面的圆盘会挡住哪些区域,然后把一整个圆(2\(/pi\))当做一整个区间,每个被覆盖的部分都可 ...

  9. BZOJ 3052: [wc2013]糖果公园 | 树上莫队

    题目: UOJ也能评测 题解 请看代码 #include<cstdio> #include<algorithm> #include<cstring> #includ ...

  10. saltstack 实现系统初始化

    1.整体结构如下 [root@zabbix init]# pwd /srv/salt/base/init [root@zabbix init]# ll total -rw-r--r-- root ro ...