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
 

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

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <queue>
  9. #include <deque>
  10. #include <map>
  11. #define range(i,a,b) for(auto i=a;i<=b;++i)
  12. #define LL long long
  13. #define itrange(i,a,b) for(auto i=a;i!=b;++i)
  14. #define rerange(i,a,b) for(auto i=a;i>=b;--i)
  15. #define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
  16. using namespace std;
  17. int b[int(1e5+)],n,q;
  18. template <class T>
  19. class segtree{
  20. private:
  21. T *add,*cnt,*minb,*maxa;
  22. void pushup(int rt){
  23. minb[rt]=min(minb[rt<<],minb[rt<<|]);
  24. cnt[rt]=cnt[rt<<]+cnt[rt<<|];
  25. maxa[rt]=max(maxa[rt<<],maxa[rt<<|]);
  26. }
  27. void pushdown(int rt){
  28. if(add[rt]){
  29. int v=add[rt];
  30. add[rt]=;
  31. maxa[rt<<]+=v;
  32. maxa[rt<<|]+=v;
  33. add[rt<<]+=v;
  34. add[rt<<|]+=v;
  35. }
  36. }
  37. public:
  38. explicit segtree(int len=int(1e5+)){
  39. add=new T[len<<];fill(add,);
  40. cnt=new T[len<<];fill(cnt,);
  41. minb=new T[len<<];fill(minb,);
  42. maxa=new T[len<<];fill(maxa,);
  43. }
  44. void build(int l,int r,int rt){
  45. add[rt]=;
  46. if(l==r){
  47. cnt[rt]=maxa[rt]=;
  48. minb[rt]=b[l];
  49. return;
  50. }
  51. int m=(l+r)>>;
  52. build(l,m,rt<<);
  53. build(m+,r,rt<<|);
  54. pushup(rt);
  55. }
  56. void update(int L,int R,T c,int l,int r,int rt){
  57. if(L<=l&&r<=R){
  58. maxa[rt]++;
  59. if(maxa[rt]<minb[rt]){
  60. ++add[rt];
  61. return;
  62. }
  63. if(l==r&&maxa[rt]>=minb[rt]){
  64. ++cnt[rt];
  65. minb[rt]+=b[l];
  66. return;
  67. }
  68. }
  69. pushdown(rt);
  70. int m=(l+r)>>;
  71. if(L<=m)update(L,R,,l,m,rt<<);
  72. if(m<R)update(L,R,,m+,r,rt<<|);
  73. pushup(rt);
  74. }
  75. T query(int L,int R,int l,int r,int rt){
  76. if(L<=l&&r<=R)return cnt[rt];
  77. int m=(l+r)>>;
  78. pushdown(rt);
  79. T ret=;
  80. if(L<=m)ret+=query(L,R,l,m,rt<<);
  81. if(m<R)ret+=query(L,R,m+,r,rt<<|);
  82. return ret;
  83. }
  84. };
  85. segtree<int>tree;
  86. void init(){}
  87. void solve(){
  88. while(cin>>n>>q){
  89. range(i,,n)scanf("%d",b+i);
  90. tree.build(,n,);
  91. char op[];int l,r;
  92. while(q--){
  93. scanf("%s%d%d",op,&l,&r);
  94. if(op[]=='a')tree.update(l,r,,,n,);
  95. else printf("%d\n",tree.query(l,r,,n,));
  96. }
  97. }
  98. }
  99. int main() {
  100. init();
  101. solve();
  102. return ;
  103. }

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. [Leetcode] unique paths 独特路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  2. 【BZOJ 1647】[Usaco2007 Open]Fliptile 翻格子游戏 模拟、搜索

    第一步我们发现对于每一个格子,我们只有翻和不翻两种状态,我们发现一旦确定了第一行操作,那么第二行的操作也就随之确定了,因为第一行操作之后我们要想得到答案就得把第一行全部为0,那么第二行的每一个格子的操 ...

  3. CodeForces743E. Vladik and cards 二分+状压dp

    这个题我们可以想象成_---___-----__的一个水柱它具有一遍优一遍行的性质因此可以用来二分最小值len,而每次二分后我们都要验根,we可以把这个水柱想成我们在每个数段里取前一段的那个数后一段有 ...

  4. 微信小程序,设置所有标签样式

    page, view, scroll-view, swiper, movable-area, cover-view, text, icon, rich-text, progress, button, ...

  5. CSS3中transform属性的用法

    有时候网站也要愚弄一下访客,比如愚人节.下面我给大家推荐个效果,就是整个页面左右颠倒了.css3 很强大,简单的几行代码就可以帮我们实现这个效果. view source   print? 01 &l ...

  6. tyvj1305 最大子序和(单调队列

    题目地址:http://www.joyoi.cn/problem/tyvj-1305 最大子序和 题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Loc ...

  7. Java之戳中痛点 - (3)三目运算符的两个操作数类型尽量一致

    先看一个例子: package com.test; public class TernaryOperator { public static void main(String[] args) { in ...

  8. 利用java.lang.reflect.Constructor动态实例化对象

         }                    }                    }                    }  }              Student t = co ...

  9. 学习正则表达式及c#应用

    1.0正则表达式语法   正则表达式是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符”).模式描述在搜索文本时要匹配的一个或多个字符串. 正则表达式示例   表达式 ...

  10. 将数据导入hive,再将hive表导入hbase

    将数据到入hive的无分区表,再将无分区表导入hive的有分区表: --备份 create table tds_package_secinfobk as select * from tds_packa ...