2016暑假多校联合---Rikka with Sequence (线段树)

Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Yuta has an array A with n numbers. Then he makes m operations on it.

There are three type of operations:

1 l r x : For each i in [l,r], change A[i] to A[i]+x
2 l r : For each i in [l,r], change A[i] to ⌊A−−√[i]⌋
3 l r : Yuta wants Rikka to sum up A[i] for all i in [l,r]

It is too difficult for Rikka. Can you help her?

 
Input
The first line contains a number t(1<=t<=100), the number of the testcases. And there are no more than 5 testcases with n>1000.

For each testcase, the first line contains two numbers n,m(1<=n,m<=100000). The second line contains n numbers A[1]~A[n]. Then m lines follow, each line describe an operation.

It is guaranteed that 1<=A[i],x<=100000.

 
Output
For each operation of type 3, print a lines contains one number -- the answer of the query.
 
Sample Input
1
5 5
1 2 3 4 5
1 3 5 2
2 1 4
3 2 4
2 3 5
3 1 5
 
Sample Output
5
6
 
Author
学军中学
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5831 5830 5829 5827 5826 
 

题意: 三种操作,1、区间上加上一个数;
                       2、区间上所有数开根号向下取整;
                       3、区间求和;

思路: 对于记录区间的最大值和最小值,如果相等的话,那么只需要对一个数开根号,算出开根号前后的差值,这样区间开根号就变成了区间减去一个数了; 
         由于是开根,所以存在两个数刚开始差为1,加上某数再开根依旧是差1,这样维护相同数区间的就没用了
         比如(2,3) +6-->(8,9)开根-->(2,3)如果全是这样的操作,即使维护相同的数,每次开根的复杂度都是O(N),不T才怪
         这样只需要维护区间最大值最小值,当差1的时候,看看是否开根后还是差1,如果还是差1,那么对区间开根号相当于整个区间减去同一个数,
         这样就可以变开根为减了

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cmath>
  5. using namespace std;
  6. typedef long long LL;
  7. const int BufferSize=<<;
  8. char buffer[BufferSize],*head,*tail;
  9. inline char Getchar()
  10. {
  11. if(head==tail)
  12. {
  13. int l=fread(buffer,,BufferSize,stdin);
  14. tail=(head=buffer)+l;
  15. }
  16. return *head++;
  17. }
  18. inline int read()
  19. {
  20. int x=,f=;char c=Getchar();
  21. for(;!isdigit(c);c=Getchar()) if(c=='-') f=-;
  22. for(;isdigit(c);c=Getchar()) x=x*+c-'';
  23. return x*f;
  24. }
  25. ///----------------------------------------------------------------------
  26. const int N=1e5+;
  27. LL sum[N<<],lz[N<<],mx[N<<],mn[N<<];
  28.  
  29. void up(int rt)
  30. {
  31. sum[rt]=sum[rt<<]+sum[rt<<|];
  32. mx[rt]=max(mx[rt<<],mx[rt<<|]);
  33. mn[rt]=min(mn[rt<<],mn[rt<<|]);
  34. }
  35.  
  36. void build(int rt,int l,int r)
  37. {
  38. lz[rt]=;
  39. if(l==r){sum[rt]=read();mn[rt]=mx[rt]=sum[rt];return;}
  40. int mid=l+r>>;
  41. build(rt<<,l,mid);build(rt<<|,mid+,r);
  42. up(rt);
  43. }
  44.  
  45. void down(int rt,int l,int r)
  46. {
  47. if(lz[rt]!=)
  48. {
  49. int mid=l+r>>;
  50. lz[rt<<]+=lz[rt];
  51. lz[rt<<|]+=lz[rt];
  52. mn[rt<<]+=lz[rt];
  53. mx[rt<<]+=lz[rt];
  54. mx[rt<<|]+=lz[rt];
  55. mn[rt<<|]+=lz[rt];
  56. sum[rt<<]+=lz[rt]*(mid-l+);
  57. sum[rt<<|]+=lz[rt]*(r-mid);
  58. lz[rt]=;
  59. }
  60. }
  61.  
  62. int x,y,t,T,n,m;
  63.  
  64. void kaigen(int rt,int l,int r)
  65. {
  66. if(x<=l&&r<=y)
  67. {
  68. if(mx[rt]==mn[rt])
  69. {
  70. lz[rt]-=mx[rt];
  71. mx[rt]=sqrt(mx[rt]);
  72. mn[rt]=mx[rt];
  73. lz[rt]+=mx[rt];
  74. sum[rt]=mx[rt]*(r-l+);
  75. return;
  76. }
  77. else if(mx[rt]==mn[rt]+)
  78. {
  79. LL x1=sqrt(mx[rt]);
  80. LL x2=sqrt(mn[rt]);
  81. if(x1==x2+)
  82. {
  83. lz[rt]-=(mx[rt]-x1);
  84. sum[rt]-=(mx[rt]-x1)*(r-l+);
  85. mx[rt]=x1;mn[rt]=x2;
  86. return;
  87. }
  88. }
  89. }
  90. int mid=l+r>>;down(rt,l,r);
  91. if(x<=mid)kaigen(rt<<,l,mid);
  92. if(y>mid)kaigen(rt<<|,mid+,r);
  93. up(rt);
  94. }
  95.  
  96. void add(int rt,int l,int r)
  97. {
  98. if(x<=l&&r<=y)
  99. {
  100. lz[rt]+=t;
  101. sum[rt]+=(long long)(r-l+)*t;
  102. mx[rt]+=t;mn[rt]+=t;
  103. return ;
  104. }
  105. int mid=l+r>>;down(rt,l,r);
  106. if(x<=mid)add(rt<<,l,mid);
  107. if(y>mid)add(rt<<|,mid+,r);
  108. up(rt);
  109. }
  110.  
  111. LL get(int rt,int l,int r)
  112. {
  113. if(x<=l&&r<=y)return sum[rt];
  114. int mid=l+r>>;down(rt,l,r);
  115. LL ret=;
  116. if(x<=mid)ret+=get(rt<<,l,mid);
  117. if(y>mid)ret+=get(rt<<|,mid+,r);
  118. return ret;
  119. }
  120.  
  121. int main()
  122. {
  123. T=read();
  124. while(T--)
  125. {
  126. n=read();m=read();
  127. build(,,n);
  128. while(m--)
  129. {
  130. int op;
  131. op=read();x=read();y=read();
  132. if(op==)
  133. {
  134. t=read();
  135. add(,,n);
  136. }
  137. else if(op==)kaigen(,,n);
  138. else printf("%I64d\n",get(,,n));
  139. }
  140. }
  141. return ;
  142. }
 

2016暑假多校联合---Rikka with Sequence (线段树)的更多相关文章

  1. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

  2. 2016暑假多校联合---Substring(后缀数组)

    2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...

  3. 2016暑假多校联合---To My Girlfriend

    2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...

  4. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

  5. 2016暑假多校联合---Another Meaning

    2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...

  6. hdu 5828 Rikka with Sequence 线段树

    Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

  7. 2016暑假多校联合---Death Sequence(递推、前向星)

    原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...

  8. 2016暑假多校联合---GCD

    Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...

  9. 2016暑假多校联合---Counting Intersections

    原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need t ...

随机推荐

  1. [Redis]Redis 概述及基本使用规范.

    1 nosql的简介 1.1 nosql简介 随着互联网Web2.0网站的兴起,传统的关系数据库在应付Web2.0网站,特别是超大规模和高并发的SNS类型的Web2.0纯动态网站已经显得力不从心,暴露 ...

  2. [开发工具]Java开发常用的在线工具

    注明: 本文转自http://www.hollischuang.com/archives/1459.作为一个Java开发人员,经常要和各种各样的工具打交道,除了我们常用的IDE工具以外,其实还有很多工 ...

  3. Atitti css3 新特性attilax总结

    Atitti css3 新特性attilax总结 图片发光效果2 透明渐变效果2 文字描边2 背景拉伸2 CSS3 选择器(Selector)4 @Font-face 特性7 Word-wrap &a ...

  4. Java-set集合

    package exception; import java.util.Map; import java.util.TreeMap; import java.util.HashMap; public ...

  5. 一次SSIS Package的调试经历

    SSIS Package的调试有时是一个非常艰难的过程,由于SSIS 编译器给出的错误信息,可能并不完善,需要程序员根据错误信息抽丝拨茧,寻找错误的根源,进而解决问题. 第一部分:SSIS提供的调试工 ...

  6. oc连接signalr,各种填坑

    在网上搜了signalr的oc客户端,基本上都指向同一个东西https://github.com/DyKnow/SignalR-ObjC 但是这个也有日子没更新了,用cocoapods安装下来是编译不 ...

  7. mac+apache+php+phpmyadmin集成php开发环境配置

    刚开始才接触php才发现macos还是比较强大了,macbook不仅是时尚达品还很实用哦. --------------他山之石-------------------------- http://da ...

  8. Spring MVC 学习总结(四)——视图与综合示例

    一.表单标签库 1.1.简介 从Spring2.0起就提供了一组全面的自动数据绑定标签来处理表单元素.生成的标签兼容HTML 4.01与XHTML 1.0.表单标签库中包含了可以用在JSP页面中渲染H ...

  9. ShineTime - 带有 CSS3 闪亮特效的缩略图相册

    ShineTime 是一个效果非常精致的缩略图相册,鼠标悬停到缩略图的时候有很炫的闪光效果,基于 CSS3 实现,另外缩略图也会有立体移动的效果.特别适用于个人摄影作品,公司产品展示等用途,快来来围观 ...

  10. 十款让 Web 前端开发人员更轻松的实用工具

    这篇文章介绍十款让 Web 前端开发人员生活更轻松的实用工具.每个 Web 开发人员都有自己的工具箱,这样工作中碰到的每个问题都有一个好的解决方案供选择. 对于每一项工作,开发人员需要特定的辅助工具, ...