Naive Operations

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

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 $a_l,a_{l+1}...a_r$
2. query l r: query $\sum_{i=l}^r \lfloor a_i / b_i \rfloor$
 
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 \leq n,q \leq 100000$, $1 \leq l \leq r \leq 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
 

分析:线段树模板改一改,维护最大值最小值就好了。

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#define range(i,a,b) for(auto i=a;i<=b;++i)
#define LL long long
#define itrange(i,a,b) for(auto i=a;i!=b;++i)
#define rerange(i,a,b) for(auto i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
int b[int(1e5+)],n,q;
template <class T>
class segtree{
private:
T *add,*cnt,*minb,*maxa;
void pushup(int rt){
minb[rt]=min(minb[rt<<],minb[rt<<|]);
cnt[rt]=cnt[rt<<]+cnt[rt<<|];
maxa[rt]=max(maxa[rt<<],maxa[rt<<|]);
}
void pushdown(int rt){
if(add[rt]){
int v=add[rt];
add[rt]=;
maxa[rt<<]+=v;
maxa[rt<<|]+=v;
add[rt<<]+=v;
add[rt<<|]+=v;
}
}
public:
explicit segtree(int len=int(1e5+)){
add=new T[len<<];fill(add,);
cnt=new T[len<<];fill(cnt,);
minb=new T[len<<];fill(minb,);
maxa=new T[len<<];fill(maxa,);
}
void build(int l,int r,int rt){
add[rt]=;
if(l==r){
cnt[rt]=maxa[rt]=;
minb[rt]=b[l];
return;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
pushup(rt);
}
void update(int L,int R,T c,int l,int r,int rt){
if(L<=l&&r<=R){
maxa[rt]++;
if(maxa[rt]<minb[rt]){
++add[rt];
return;
}
if(l==r&&maxa[rt]>=minb[rt]){
++cnt[rt];
minb[rt]+=b[l];
return;
}
}
pushdown(rt);
int m=(l+r)>>;
if(L<=m)update(L,R,,l,m,rt<<);
if(m<R)update(L,R,,m+,r,rt<<|);
pushup(rt);
}
T query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R)return cnt[rt];
int m=(l+r)>>;
pushdown(rt);
T ret=;
if(L<=m)ret+=query(L,R,l,m,rt<<);
if(m<R)ret+=query(L,R,m+,r,rt<<|);
return ret;
}
};
segtree<int>tree;
void init(){}
void solve(){
while(cin>>n>>q){
range(i,,n)scanf("%d",b+i);
tree.build(,n,);
char op[];int l,r;
while(q--){
scanf("%s%d%d",op,&l,&r);
if(op[]=='a')tree.update(l,r,,,n,);
else printf("%d\n",tree.query(l,r,,n,));
}
}
}
int main() {
init();
solve();
return ;
}

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

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

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

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

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

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

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

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

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

  9. HDU 6351 Naive Operations(线段树)

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

随机推荐

  1. hdu4418 Time travel 【期望dp + 高斯消元】

    题目链接 BZOJ4418 题解 题意:从一个序列上某一点开始沿一个方向走,走到头返回,每次走的步长各有概率,问走到一点的期望步数,或者无解 我们先将序列倍长形成循环序列,\(n = (N - 1) ...

  2. 门户系统整合sso cookie共享及显示用户信息

    1.1 门户系统整合sso 在门户系统点击登录连接跳转到登录页面.登录成功后,跳转到门户系统的首页,在门户系统中需要从cookie中 把token取出来.所以必须在登录成功后把token写入cooki ...

  3. Visaul Studio 常用快捷键动画演示

    从本篇文章开始,我将会陆续介绍提高 VS 开发效率的文章,欢迎大家补充~ 在进行代码开发的时候,我们往往会频繁的使用键盘.鼠标进行协作,但是切换使用两种工具会影响到我们的开发速度,如果所有的操作都可以 ...

  4. mysql共享表空间和独立表空间

    innodb这种引擎,与MYISAM引擎的区别很大.特别是它的数据存储格式等. 对于innodb的数据结构,首先要解决两个概念性的问题: 共享表空间以及独占表空间. 什么是共享表空间和独占表空间 共享 ...

  5. css实现九宫格图片自适应布局

    我之前写九宫格自适应布局的时候,每个格子是使用媒体查询器(@media)或者js动态设置css,根据不同的手机屏幕宽度,适配不同手机,但是这样有个很大的缺点,那就是移动端的屏幕尺寸太多了,就得写很多代 ...

  6. python最简单发送邮件

    #!/usr/bin/env python #coding:utf8 #Author:lsp #Date:下午5:51:13 #Version:0.1 #Function: #导入smtplib和MI ...

  7. RPC-Thrift(四)

    Client Thrift客户端有两种:同步客户端和异步客户端. 同步客户端 同步客户端比较简单,以RPC-Thrift(一)中的的例子为基础进行研究源码,先看一下类图. TServiceClient ...

  8. AQS同步组件及ReentrantLock和synchronized的区别

    AQS同步组件 CountDownLatch(只有一个线程对他进行操作): 主线程必须在启动其它线程后立即调用await()方法.这样主线程的操作就会在这个方法上阻塞,直到其它线程完成各自的任务. S ...

  9. bzoj1040 内向树DP

    2013-11-17 08:52 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1040 N个骑士,每个人有一个仇人,那么,每个骑士只有一个 ...

  10. cube中的判断类型

    import { createAddAPI } from '../util' const DATE_RE = /^(1|2)\d{3}[.\-/]\d{1,2}[.\-/]\d{1,2}$/ cons ...