Rikka with Sequence

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5828

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

Hint

题意

给你n个数,m个操作

一共有三类操作

区间开根号

区间加

区间求和

题解:

线段树,区间开根号最多十几次就会变成1,我们把相同值的数合并在一起去更新就好了

区间加,就正常做,和普通的线段树一样去做就好了。

区间求和也正常做……

其实就比普通的线段树多一个lazy,表示左右是否相同。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 3e5+7;
  4. long long n;
  5. int a[maxn];
  6. int to[2000005];
  7. typedef long long ll;
  8. typedef long long SgTreeDataType;
  9. const int BUF=40000000;
  10. char Buf[BUF],*buf=Buf;
  11. const int OUT=20000000;
  12. char Out[OUT],*ou=Out;int Outn[30],Outcnt;
  13. inline void write(int x){
  14. if(!x)*ou++=48;
  15. else{
  16. for(Outcnt=0;x;x/=10)Outn[++Outcnt]=x%10+48;
  17. while(Outcnt)*ou++=Outn[Outcnt--];
  18. }
  19. }
  20. inline void writell(ll x){
  21. if(!x)*ou++=48;
  22. else{
  23. for(Outcnt=0;x;x/=10)Outn[++Outcnt]=x%10+48;
  24. while(Outcnt)*ou++=Outn[Outcnt--];
  25. }
  26. }
  27. inline void writechar(char x){*ou++=x;}
  28. inline void writeln(){*ou++='\n';}
  29. inline void read(int&a){for(a=0;*buf<48;buf++);while(*buf>47)a=a*10+*buf++-48;}
  30. inline int read()
  31. {
  32. int x=0,f=1;char ch=getchar();
  33. while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  34. while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
  35. return x*f;
  36. }
  37. inline int TransForm( long long x ){
  38. if( x <= 2000000 ) return to[x];
  39. return sqrt( x );
  40. }
  41. struct Sgtree{
  42. struct treenode{
  43. int l , r ;
  44. long long f , lzy , sum ;
  45. void Update( long long x ){
  46. lzy += x;
  47. sum += (r - l + 1) * x;
  48. if( ~f ) f += x;
  49. }
  50. void SetUp( long long x ){
  51. f = x;
  52. sum = x * ( r - l + 1 );
  53. }
  54. void SqrtUp(){
  55. f = TransForm( f );
  56. sum = f * ( r - l + 1 );
  57. }
  58. }tree[maxn << 2];
  59. void Push_Up( int o ){
  60. tree[o].sum = tree[o << 1].sum + tree[o << 1 | 1].sum;
  61. if( tree[o << 1].f == tree[o << 1 | 1].f ) tree[o].f = tree[o << 1].f;
  62. else tree[o].f = -1;
  63. }
  64. void ReleaseLabel( int o ){
  65. if( tree[o].lzy ){
  66. tree[o << 1].Update( tree[o].lzy );
  67. tree[o << 1 | 1].Update( tree[o].lzy );
  68. tree[o].lzy = 0;
  69. }
  70. if( ~tree[o].f ){
  71. tree[o << 1].SetUp( tree[o].f );
  72. tree[o << 1 | 1].SetUp( tree[o].f );
  73. }
  74. }
  75. void Build( int l , int r , int o ){
  76. tree[o].l = l , tree[o].r = r , tree[o].lzy = tree[o].sum = 0;
  77. if( r > l ){
  78. int mid = l + r >> 1;
  79. Build( l , mid , o << 1 );
  80. Build( mid + 1 , r , o << 1 | 1 );
  81. Push_Up( o );
  82. }else tree[o].f = tree[o].sum = a[l];
  83. }
  84. void Add( int ql , int qr , int v , int o ){
  85. int l = tree[o].l , r = tree[o].r;
  86. if( ql <= l && r <= qr ) tree[o].Update( v );
  87. else{
  88. int mid = l + r >> 1;
  89. ReleaseLabel( o );
  90. if( ql <= mid ) Add( ql , qr , v , o << 1 );
  91. if( qr > mid ) Add( ql , qr , v , o << 1 | 1 );
  92. Push_Up( o );
  93. }
  94. }
  95. void DFS( int o ){
  96. if( ~tree[o].f ){
  97. tree[o].SqrtUp( );
  98. }else{
  99. ReleaseLabel( o );
  100. DFS( o << 1 );
  101. DFS( o << 1 | 1 );
  102. Push_Up( o );
  103. }
  104. }
  105. void OpeSqrt( int ql , int qr , int o ){
  106. int l = tree[o].l , r = tree[o].r;
  107. if( ql <= l && r <= qr ) DFS( o );
  108. else{
  109. int mid = l + r >> 1;
  110. ReleaseLabel( o );
  111. if( ql <= mid ) OpeSqrt( ql , qr , o << 1 );
  112. if( qr > mid ) OpeSqrt( ql , qr , o << 1 | 1 );
  113. Push_Up( o );
  114. }
  115. }
  116. long long Ask( int ql , int qr , int o ){
  117. int l = tree[o].l , r = tree[o].r;
  118. if( ql <= l && r <= qr ) return tree[o].sum;
  119. else{
  120. int mid = l + r >> 1;
  121. ReleaseLabel( o );
  122. long long Result = 0;
  123. if( ql <= mid ) Result += Ask( ql , qr , o << 1 );
  124. if( qr > mid ) Result += Ask( ql , qr , o << 1 | 1 );
  125. Push_Up( o );
  126. return Result;
  127. }
  128. }
  129. }Sgtree;
  130. int main()
  131. {
  132. fread(Buf,1,BUF,stdin);
  133. for(int i=0;i<=2000000;i++)
  134. to[i]=sqrt(i);
  135. int t;
  136. read(t);
  137. while(t--){
  138. int n,m;
  139. read(n);read(m);
  140. for(int i=1;i<=n;i++) read(a[i]);
  141. Sgtree.Build( 1 , n , 1 );
  142. while(m--)
  143. {
  144. int op,x,y,z;
  145. read(op),read(x),read(y);
  146. if(op==2) Sgtree.OpeSqrt( x , y , 1 );
  147. if(op==1){
  148. read(z);
  149. Sgtree.Add( x , y , z , 1 );
  150. }
  151. if(op==3)writell(Sgtree.Ask(x,y,1)),writeln();
  152. }
  153. }
  154. fwrite(Out,1,ou-Out,stdout);
  155. return 0;
  156. }

hdu 5828 Rikka with Sequence 线段树的更多相关文章

  1. HDU 5828 Rikka with Sequence (线段树+剪枝优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828 给你n个数,三种操作.操作1是将l到r之间的数都加上x:操作2是将l到r之间的数都开方:操作3是 ...

  2. HDU 5828 Rikka with Sequence(线段树区间加开根求和)

    Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...

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

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  4. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

  5. HDU 5828 Rikka with Sequence (线段树)

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

  6. HDU 5828 Rikka with Sequence(线段树 开根号)

    Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  7. hdu 4893 Wow! Such Sequence!(线段树)

    题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...

  8. HDU 6089 Rikka with Terrorist (线段树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6089 题解 这波强行维护搞得我很懵逼... 扫描线,只考虑每个点能走到左上方(不包括正上方,但包括正左 ...

  9. HDU 5634 Rikka with Phi 线段树

    题意:bc round 73 div1 D 中文题面 分析:注意到10^7之内的数最多phi O(log(n))次就会变成1, 因此可以考虑把一段相同的不为1的数缩成一个点,用平衡树来维护. 每次求p ...

随机推荐

  1. 10.29训练赛第一场B题

    题目大意:有n个队伍之间比赛,每两个队伍之间都有一场比赛,因此一共有n(n-1) / 2场比赛,但是这里丢失了一场比赛的记录,现在让你通过n(n-1) /2 -1场仍然存在的比赛记录来判断丢失的那条比 ...

  2. 第5月第8天 jsonmodel

    1. @implementation JSONValueTransformer (CustomTransformer) //时间戳转NSDate - (NSDate *)NSDateFromNSStr ...

  3. 数链剖分(Housewife Wind )

     题目链接:https://vjudge.net/contest/279350#problem/B 题目大意:给你n,q,s.n指的是有n个点,q代表有q次询问,s代表的是起点.然后接下来会有n-1条 ...

  4. Git常见错误处理

      如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git  提示出错信息:fatal: remo ...

  5. 用于阻止缓冲区溢出攻击的 Linux 内核参数与 gcc 编译选项

    先来看看基于 Red Hat 与 Fedora 衍生版(例如 CentOS)系统用于阻止栈溢出攻击的内核参数,主要包含两项: kernel.exec-shield 可执行栈保护,字面含义比较“绕”, ...

  6. 『记录』Android参考资料

    欢迎留言推荐好的教程.资料.博客及作者等. 『记录』Android参考资料 1.前期环境 Android Studio使用Git Android Studio快捷键总结 Android Studio及 ...

  7. Tesseract-OCR 3.05 多过语言文字识别(运行程序+中英日韩语言包)

    最新版本 静态编译 tesseract 3.05.00dev leptonica-1.73 libgif 5.1.3 : libjpeg 8c : libpng 1.6.16 : libtiff 3. ...

  8. Linux磁盘分区UUID的获取及其UUID的作用

    注:UUID-Universally Unique IDentifiers全局唯一标识符 一.Linux磁盘分区UUID的获取方法 1.[san@localhost ~]$ ls -l /dev/di ...

  9. Jmeter之逻辑控制器(Logic Controller)【转】

    Jmeter之逻辑控制器(Logic Controller) 前言: 1. Jmeter官网对逻辑控制器的解释是:“Logic Controllers determine the order in w ...

  10. Transition学习笔记

    概述 Android 4.4.2 (API level 19)引入Transition框架,之后很多APP上都使用该框架做出很酷炫的效果,如 Google Play Newsstand app 还有g ...