4927 线段树练习5

时间限制: 1 s

空间限制: 128000 KB

题目等级 : 黄金 Gold

题目描述 Description

有n个数和5种操作

add a b c:把区间[a,b]内的所有数都增加c

set a b c:把区间[a,b]内的所有数都设为c

sum a b:查询区间[a,b]的区间和

max a b:查询区间[a,b]的最大值

min a b:查询区间[a,b]的最小值

输入描述 Input Description

第一行两个整数n,m,第二行n个整数表示这n个数的初始值

接下来m行操作,同题目描述

输出描述 Output Description

对于所有的sum、max、min询问,一行输出一个答案

样例输入 Sample Input

10 6

3 9 2 8 1 7 5 0 4 6

add 4 9 4

set 2 6 2

add 3 8 2

sum 2 10

max 1 7

min 3 6

样例输出 Sample Output

49

11

4

数据范围及提示 Data Size & Hint

10%:1

  1. /*
  2. 分块.
  3. 这题改了一个下午.
  4. 直接呵呵了.
  5. 思路是挺简单的,但是实现起来有点鬼畜.
  6. 哎就不说啥了 码力太弱.
  7. 注意几个细节.
  8. 重构不完整的块时要注意区间和贡献的计算还有每个位置的值应该是啥.
  9. ★x,y在同一个块中不要忘了重构y到块末的贡献.
  10. 然后区分该区间是否被"set",重构的话把标记清掉.
  11. 感谢前人的分块solution code 给了我debug一个下午的可能hhh.
  12. */
  13. #include<cstdio>
  14. #include<cmath>
  15. #include<iostream>
  16. #define MAXN 200001
  17. #define MAXM 200001
  18. #define LL long long
  19. #define INF 1e18
  20. using namespace std;
  21. int n,m,q,belong[MAXN];
  22. LL ans,s[MAXN],sum[MAXM],tot,tag[MAXM],bj[MAXM],max1[MAXM],min1[MAXM];
  23. bool b[MAXM];
  24. LL read()
  25. {
  26. LL x=0,f=1;char ch=getchar();
  27. while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  28. while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
  29. return x*f;
  30. }
  31. void sloveadd(int x,int y,LL z)
  32. {
  33. for(int i=x;i<=min(belong[x]*m,y);i++)
  34. {
  35. if(b[belong[x]]) s[i]=bj[belong[x]]+tag[belong[x]]+z;
  36. else s[i]+=z+tag[belong[x]];
  37. sum[belong[x]]+=z;
  38. }
  39. if(belong[x]==belong[y])
  40. {
  41. for(int i=y+1;i<=belong[y]*m;i++)
  42. {
  43. if(b[belong[y]]) s[i]=bj[belong[y]]+tag[belong[y]];
  44. else s[i]+=tag[belong[y]];
  45. }
  46. }
  47. for(int i=(belong[x]-1)*m+1;i<=x-1;i++)
  48. {
  49. if(b[belong[x]]) s[i]=bj[belong[x]]+tag[belong[x]];
  50. else s[i]+=tag[belong[x]];
  51. }
  52. b[belong[x]]=false;tag[belong[x]]=0;
  53. max1[belong[x]]=-INF,min1[belong[x]]=INF;
  54. for(int i=(belong[x]-1)*m+1;i<=min(n,belong[x]*m);i++)
  55. {
  56. max1[belong[i]]=max(max1[belong[i]],s[i]+tag[belong[i]]);
  57. min1[belong[i]]=min(min1[belong[i]],s[i]+tag[belong[i]]);
  58. }
  59. for(int i=belong[x]+1;i<=belong[y]-1;i++)
  60. sum[i]+=z*m,tag[i]+=z,max1[i]+=z,min1[i]+=z;
  61. if(belong[x]!=belong[y])
  62. {
  63. for(int i=(belong[y]-1)*m+1;i<=y;i++)
  64. {
  65. if(b[belong[y]]) s[i]=bj[belong[y]]+tag[belong[y]]+z;
  66. else s[i]+=z+tag[belong[y]];
  67. sum[belong[y]]+=z;
  68. }
  69. for(int i=y+1;i<=min(n,belong[y]*m);i++)
  70. {
  71. if(b[belong[y]]) s[i]=bj[belong[y]]+tag[belong[y]];
  72. else s[i]+=tag[belong[y]];
  73. }
  74. max1[belong[y]]=-INF,min1[belong[y]]=INF;
  75. b[belong[y]]=false;tag[belong[y]]=0;
  76. max1[belong[y]]=-INF,min1[belong[y]]=INF;
  77. for(int i=(belong[y]-1)*m+1;i<=min(n,belong[y]*m);i++)
  78. {
  79. max1[belong[y]]=max(max1[belong[y]],s[i]+tag[belong[y]]);
  80. min1[belong[y]]=min(min1[belong[y]],s[i]+tag[belong[y]]);
  81. }
  82. }
  83. return ;
  84. }
  85. void slovechange(int x,int y,LL z)
  86. {
  87. for(int i=x;i<=min(belong[x]*m,y);i++)
  88. {
  89. if(!b[belong[x]]) sum[belong[x]]+=z-s[i]-tag[belong[x]];
  90. else sum[belong[x]]+=z-bj[belong[x]]-tag[belong[x]];
  91. s[i]=z;
  92. }
  93. if(belong[x]==belong[y])
  94. {
  95. for(int i=y+1;i<=belong[y]*m;i++)
  96. {
  97. if(b[belong[y]]) s[i]=bj[belong[y]]+tag[belong[y]];
  98. else s[i]+=tag[belong[y]];
  99. }
  100. }
  101. for(int i=(belong[x]-1)*m+1;i<=x-1;i++)
  102. {
  103. if(b[belong[x]]) s[i]=bj[belong[x]]+tag[belong[x]];
  104. else s[i]+=tag[belong[x]];
  105. }
  106. b[belong[x]]=false;tag[belong[x]]=0;
  107. max1[belong[x]]=-INF,min1[belong[x]]=INF;
  108. for(int i=(belong[x]-1)*m+1;i<=min(n,belong[x]*m);i++)
  109. {
  110. max1[belong[i]]=max(max1[belong[i]],s[i]+tag[belong[i]]);
  111. min1[belong[i]]=min(min1[belong[i]],s[i]+tag[belong[i]]);
  112. }
  113. for(int i=belong[x]+1;i<=belong[y]-1;i++)
  114. sum[i]=z*m,tag[i]=0,b[i]=true,max1[i]=z,min1[i]=z,bj[i]=z;
  115. if(belong[x]!=belong[y])
  116. {
  117. for(int i=(belong[y]-1)*m+1;i<=y;i++)
  118. {
  119. if(!b[belong[y]]) sum[belong[y]]+=z-s[i]-tag[belong[y]];
  120. else sum[belong[y]]+=z-bj[belong[y]]-tag[belong[y]];
  121. s[i]=z;
  122. }
  123. for(int i=y+1;i<=min(n,belong[y]*m);i++)
  124. {
  125. if(b[belong[y]]) s[i]=bj[belong[y]]+tag[belong[y]];
  126. else s[i]+=tag[belong[y]];
  127. }
  128. b[belong[y]]=false;tag[belong[y]]=0;
  129. max1[belong[y]]=-INF,min1[belong[y]]=INF;
  130. for(int i=(belong[y]-1)*m+1;i<=min(n,belong[y]*m);i++)
  131. {
  132. max1[belong[y]]=max(max1[belong[y]],s[i]+tag[belong[y]]);
  133. min1[belong[y]]=min(min1[belong[y]],s[i]+tag[belong[y]]);
  134. }
  135. }
  136. return ;
  137. }
  138. LL slovequerysum(int x,int y)
  139. {
  140. ans=0;
  141. for(int i=x;i<=min(belong[x]*m,y);i++)
  142. {
  143. if(!b[belong[x]]) ans+=s[i]+tag[belong[i]];
  144. else ans+=bj[belong[i]]+tag[belong[i]];
  145. }
  146. for(int i=belong[x]+1;i<=belong[y]-1;i++) ans+=sum[i];
  147. if(belong[x]!=belong[y])
  148. {
  149. for(int i=(belong[y]-1)*m+1;i<=y;i++)
  150. {
  151. if(!b[belong[y]]) ans+=s[i]+tag[belong[y]];
  152. else ans+=bj[belong[y]]+tag[belong[y]];
  153. }
  154. }
  155. return ans;
  156. }
  157. LL slovequerymax(int x,int y)
  158. {
  159. ans=-INF;
  160. for(int i=x;i<=min(belong[x]*m,y);i++)
  161. {
  162. if(!b[belong[x]]) ans=max(ans,s[i]+tag[belong[i]]);
  163. else ans=max(ans,bj[belong[i]]+tag[belong[i]]);
  164. }
  165. for(int i=belong[x]+1;i<=belong[y]-1;i++) ans=max(ans,max1[i]);
  166. if(belong[x]!=belong[y])
  167. {
  168. for(int i=(belong[y]-1)*m+1;i<=y;i++)
  169. {
  170. if(!b[belong[y]]) ans=max(ans,s[i]+tag[belong[y]]);
  171. else ans=max(ans,bj[belong[y]]+tag[belong[y]]);
  172. }
  173. }
  174. return ans;
  175. }
  176. LL slovequerymin(int x,int y)
  177. {
  178. ans=INF;
  179. for(int i=x;i<=min(belong[x]*m,y);i++)
  180. {
  181. if(!b[belong[x]]) ans=min(ans,s[i]+tag[belong[i]]);
  182. else ans=min(ans,bj[belong[i]]+tag[belong[i]]);
  183. }
  184. for(int i=belong[x]+1;i<=belong[y]-1;i++) ans=min(ans,min1[i]);
  185. if(belong[x]!=belong[y])
  186. {
  187. for(int i=(belong[y]-1)*m+1;i<=y;i++)
  188. {
  189. if(!b[belong[y]]) ans=min(ans,s[i]+tag[belong[y]]);
  190. else ans=min(ans,bj[belong[y]]+tag[belong[y]]);
  191. }
  192. }
  193. return ans;
  194. }
  195. int main()
  196. {
  197. int x,y,z;LL k;char ch[6];
  198. n=read();q=read();
  199. m=sqrt(n);
  200. for(int i=1;i<=n;i++) belong[i]=(i-1)/m+1;
  201. for(int i=1;i<=belong[n];i++) max1[i]=-INF,min1[i]=INF;
  202. for(int i=1;i<=n;i++)
  203. {
  204. s[i]=read(),sum[belong[i]]+=s[i];
  205. max1[belong[i]]=max(max1[belong[i]],s[i]);
  206. min1[belong[i]]=min(min1[belong[i]],s[i]);
  207. }
  208. while(q--)
  209. {
  210. scanf("%s",ch);x=read(),y=read();
  211. if(ch[0]=='a') k=read(),sloveadd(x,y,k);
  212. else if(ch[1]=='e') k=read(),slovechange(x,y,k);
  213. else if(ch[1]=='u') printf("%lld\n",slovequerysum(x,y));
  214. else if(ch[1]=='a') printf("%lld\n",slovequerymax(x,y));
  215. else if(ch[1]=='i') printf("%lld\n",slovequerymin(x,y));
  216. }
  217. return 0;
  218. }

Codevs 4927 线段树练习5(分块)的更多相关文章

  1. codevs 1082 线段树练习 3 --分块练习

    时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[ ...

  2. codevs 4927 线段树练习5

    赶在期末考试之前把这道傻逼题调了出来. #include<iostream> #include<cstdio> #include<cstring> #include ...

  3. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  4. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

  5. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  6. 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

  7. codevs 1080 线段树练习 CDQ分治

    codevs 1080 线段树练习 http://codevs.cn/problem/1080/  时间限制: 1 s  空间限制: 128000 KB   题目描述 Description 一行N个 ...

  8. CODEVS.5037.线段树练习4加强版(分块 区间k的倍数)

    题目链接 /* 如果用线段树,每个节点要再开k的空间,显然不行.但是分块可以(虽然空间依旧爆炸) 分块.用bloans[i][j]表示 第i块 模k为j 的有多少个 对于不是整块的,查询时应判断 A[ ...

  9. codevs 1080 线段树练习

    链接:http://codevs.cn/problem/1080/ 先用树状数组水一发,再用线段树水一发 树状数组代码:84ms #include<cstdio> #include< ...

随机推荐

  1. Android--DES加密

    Base64.java import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputS ...

  2. "一起来捉妖"怎么从瘸腿中组合到最合心意的妖灵

    背景: 最近两天活动,黑鬼白鬼合体觉醒秋容,陆无名,聂小倩,作为一个非土豪玩家,没有超高资质妖灵的我,想要在瘸腿妖灵中选取两个最佳选择,合体觉醒. 初选: 备选妖灵从5个维度录入数据,进行选择,分别为 ...

  3. [Done] Codeforces Round #562 (Div. 2) 题解

    A - Circle Metro 模拟几百步就可以了. B - Pairs 爆搜一下,时间复杂度大概是 $O(4 * n)$ Code: 56306723 C - Increasing by Modu ...

  4. DG环境恢复同步遇到报错ORA-00353ORA-00334以及ORA-00600[2619], [47745]

    问题说明 客户环境主库4节点RAC11.2.0.4,单实例DG环境,DG由于空间不足,导致同步中断,由于DG备库未应用的归档主库都再,本次恢复的方式,是开启dg mrp进程,自动同步追上主库. 以下遇 ...

  5. php 判断请求是否是json

    $object =file_get_contents("php://input"); $arr = is_json($object); if($arr){ var_dump($ar ...

  6. windows下mysql表名区分大小写

    windows下mysql默认是不区分大小写的,但是linux会区分大小写 如何让windows下mysql区分大小写呢? 修改 my.ini 里面的mysqld部分 #区分大小写 lower_cas ...

  7. Python遗传和进化算法框架(一)Geatpy快速入门

    https://blog.csdn.net/qq_33353186/article/details/82014986 Geatpy是一个高性能的Python遗传算法库以及开放式进化算法框架,由华南理工 ...

  8. elk docker-compose

    version: '3.1' services: elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4 c ...

  9. MySQL LAST_INSERT_ID()用法

    last_insert_id()函数是适用于id为自动生成的表 下面是插入表数据时last_insert_id()函数的两种用法: 表结构: 此表使用last_insert_id()函数的字段为par ...

  10. FLV 数据封装格式

    https://www.cnblogs.com/chyingp/p/flv-getting-started.html https://blog.csdn.net/ai2000ai/article/de ...