#228. 基础数据结构练习题

统计

sylvia 是一个热爱学习的女孩子,今天她想要学习数据结构技巧。

在看了一些博客学了一些姿势后,她想要找一些数据结构题来练练手。于是她的好朋友九条可怜酱给她出了一道题。

给出一个长度为 nn 的数列 AA,接下来有 mm 次操作,操作有三种:

  1. 对于所有的 i∈[l,r]i∈[l,r],将 AiAi 变成 Ai+xAi+x。
  2. 对于所有的 i∈[l,r]i∈[l,r],将 AiAi 变成 ⌊Ai−−√⌋⌊Ai⌋。
  3. 对于所有的 i∈[l,r]i∈[l,r],询问 AiAi 的和。

作为一个不怎么熟练的初学者,sylvia 想了好久都没做出来。而可怜酱又外出旅游去了,一时间联系不上。于是她决定向你寻求帮助:你能帮她解决这个问题吗。

输入格式

第一行两个数:n,mn,m。

接下来一行 nn 个数 AiAi。

接下来 mm 行中,第 ii 行第一个数 titi 表示操作类型:

若 ti=1ti=1,则接下来三个整数 li,ri,xili,ri,xi,表示操作一。

若 ti=2ti=2,则接下来三个整数 li,rili,ri,表示操作二。

若 ti=3ti=3,则接下来三个整数 li,rili,ri,表示操作三。

输出格式

对于每个询问操作,输出一行表示答案。

样例一

input

  1. 5 5
  2. 1 2 3 4 5
  3. 1 3 5 2
  4. 2 1 4
  5. 3 2 4
  6. 2 3 5
  7. 3 1 5

output

  1. 5
  2. 6

inline大法好;

读入优化大法好。

判断区间最小值跟最大值相差1或者0即可;

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<string>
  6. #include<queue>
  7. #include<algorithm>
  8. #include<stack>
  9. #include<cstring>
  10. #include<vector>
  11. #include<list>
  12. #include<set>
  13. #include<map>
  14. using namespace std;
  15. #define ll long long
  16. #define pi (4*atan(1.0))
  17. #define eps 1e-7
  18. #define bug(x) cout<<"bug"<<x<<endl;
  19. const int N=1e5+,M=1e6+,inf=;
  20. const ll INF=1e18+,mod=;
  21. inline ll scan()
  22. {
  23. ll res = , ch ;
  24. while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
  25. {
  26. if( ch == EOF ) return 1LL << ;
  27. }
  28. res = ch - '' ;
  29. while( ( ch = getchar() ) >= '' && ch <= '' )
  30. res = res * + ( ch - '' ) ;
  31. return res ;
  32. }
  33. /// 数组大小
  34. int n;
  35. ll sum[N<<],minn[N<<],maxx[N<<],lazy[N<<],cov[N<<];
  36. inline void pushup(int pos)
  37. {
  38. minn[pos]=min(minn[pos<<],minn[pos<<|]);
  39. maxx[pos]=max(maxx[pos<<|],maxx[pos<<]);
  40. sum[pos]=sum[pos<<]+sum[pos<<|];
  41. }
  42. inline void pushdown(int pos,int l,int r)
  43. {
  44. if(cov[pos])
  45. {
  46. int mid=(l+r)>>;
  47. cov[pos<<]=cov[pos];
  48. cov[pos<<|]=cov[pos];
  49. maxx[pos<<]=cov[pos];
  50. maxx[pos<<|]=cov[pos];
  51. minn[pos<<]=cov[pos];
  52. minn[pos<<|]=cov[pos];
  53. sum[pos<<]=cov[pos]*(mid-l+);
  54. sum[pos<<|]=cov[pos]*(r-mid);
  55. lazy[pos<<]=;
  56. lazy[pos<<|]=;
  57. cov[pos]=;
  58. }
  59. if(lazy[pos])
  60. {
  61. int mid=(l+r)>>;
  62. lazy[pos<<]+=lazy[pos];
  63. lazy[pos<<|]+=lazy[pos];
  64. maxx[pos<<]+=lazy[pos];
  65. maxx[pos<<|]+=lazy[pos];
  66. minn[pos<<]+=lazy[pos];
  67. minn[pos<<|]+=lazy[pos];
  68. sum[pos<<]+=lazy[pos]*(mid-l+);
  69. sum[pos<<|]+=lazy[pos]*(r-mid);
  70. lazy[pos]=;
  71. }
  72. }
  73. inline void build(int l,int r,int pos)
  74. {
  75. lazy[pos]=;
  76. cov[pos]=;
  77. if(l==r)
  78. {
  79. sum[pos]=scan();
  80. minn[pos]=maxx[pos]=sum[pos];
  81. return;
  82. }
  83. int mid=(l+r)>>;
  84. build(l,mid,pos<<);
  85. build(mid+,r,pos<<|);
  86. pushup(pos);
  87. }
  88. inline void update(int L,int R,ll c,int l,int r,int pos)
  89. {
  90. if(L<=l&&r<=R)
  91. {
  92. lazy[pos]+=c;
  93. minn[pos]+=c;
  94. maxx[pos]+=c;
  95. sum[pos]+=c*(r-l+);
  96. return;
  97. }
  98. pushdown(pos,l,r);
  99. int mid=(l+r)>>;
  100. if(L<=mid)update(L,R,c,l,mid,pos<<);
  101. if(R>mid)update(L,R,c,mid+,r,pos<<|);
  102. pushup(pos);
  103. }
  104. inline void update1(int L,int R,ll c,int l,int r,int pos)
  105. {
  106. if(L<=l&&r<=R)
  107. {
  108. cov[pos]=c;
  109. minn[pos]=c;
  110. maxx[pos]=c;
  111. sum[pos]=c*(r-l+);
  112. lazy[pos]=;
  113. return;
  114. }
  115. pushdown(pos,l,r);
  116. int mid=(l+r)>>;
  117. if(L<=mid)update1(L,R,c,l,mid,pos<<);
  118. if(R>mid)update1(L,R,c,mid+,r,pos<<|);
  119. pushup(pos);
  120. }
  121. inline void update2(int L,int R,int l,int r,int pos)
  122. {
  123. if(l==L&&R==r&&maxx[pos]==minn[pos])
  124. {
  125. ll x=(ll)floor(sqrt(maxx[pos]));
  126. update1(L,R,x,,n,);
  127. return;
  128. }
  129. if(l==L&&R==r&&maxx[pos]==minn[pos]+)
  130. {
  131. ll x=(ll)floor(sqrt(maxx[pos]));
  132. ll y=(ll)floor(sqrt(minn[pos]));
  133. if(x==y)update1(L,R,x,,n,);
  134. else update(L,R,x-maxx[pos],,n,);
  135. return;
  136. }
  137. pushdown(pos,l,r);
  138. int mid=(l+r)>>;
  139. if(R<=mid)update2(L,R,l,mid,pos<<);
  140. else if(L>mid)update2(L,R,mid+,r,pos<<|);
  141. else
  142. {
  143. update2(L,mid,l,mid,pos<<);
  144. update2(mid+,R,mid+,r,pos<<|);
  145. }
  146. pushup(pos);
  147. }
  148. inline ll query(int L,int R,int l,int r,int pos)
  149. {
  150. if(L<=l&&r<=R)return sum[pos];
  151. pushdown(pos,l,r);
  152. int mid=(l+r)>>;
  153. ll ans=;
  154. if(L<=mid)ans+=query(L,R,l,mid,pos<<);
  155. if(R>mid)ans+=query(L,R,mid+,r,pos<<|);
  156. return ans;
  157. }
  158. int main()
  159. {
  160. int m;
  161. n=scan();
  162. m=scan();
  163. build(,n,);
  164. while(m--)
  165. {
  166. int t,l,r;
  167. t=scan();
  168. l=scan();
  169. r=scan();
  170. if(t==)
  171. {
  172. ll x;
  173. x=scan();
  174. update(l,r,x,,n,);
  175. }
  176. else if(t==) update2(l,r,,n,);
  177. else printf("%lld\n",query(l,r,,n,));
  178. }
  179. return ;
  180. }

uoj #228. 基础数据结构练习题 线段树的更多相关文章

  1. uoj#228. 基础数据结构练习题(线段树区间开方)

    题目链接:http://uoj.ac/problem/228 代码:(先开个坑在这个地方) #include<bits/stdc++.h> using namespace std; ; l ...

  2. UOJ #228. 基础数据结构练习题 线段树 + 均摊分析 + 神题

    题目链接 一个数被开方 #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",st ...

  3. 【线段树】uoj#228. 基础数据结构练习题

    get到了标记永久化 sylvia 是一个热爱学习的女孩子,今天她想要学习数据结构技巧. 在看了一些博客学了一些姿势后,她想要找一些数据结构题来练练手.于是她的好朋友九条可怜酱给她出了一道题. 给出一 ...

  4. 【UOJ#228】基础数据结构练习题 线段树

    #228. 基础数据结构练习题 题目链接:http://uoj.ac/problem/228 Solution 这题由于有区间+操作,所以和花神还是不一样的. 花神那道题,我们可以考虑每个数最多开根几 ...

  5. uoj#228 基础数据结构练习题

    题面:http://uoj.ac/problem/228 正解:线段树. 我们可以发现,开根号时一个区间中的数总是趋近相等.判断一个区间的数是否相等,只要判断最大值和最小值是否相等就行了.如果这个区间 ...

  6. 【uoj#228】基础数据结构练习题 线段树+均摊分析

    题目描述 给出一个长度为 $n$ 的序列,支持 $m$ 次操作,操作有三种:区间加.区间开根.区间求和. $n,m,a_i\le 100000$ . 题解 线段树+均摊分析 对于原来的两个数 $a$ ...

  7. uoj#228. 基础数据结构练习题(线段树)

    传送门 只有区间加区间开方我都会--然而加在一起我就gg了-- 然后这题的做法就是对于区间加直接打标记,对于区间开方,如果这个区间的最大值等于最小值就直接区间覆盖(据ljh_2000大佬说这个区间覆盖 ...

  8. UOJ #228 - 基础数据结构练习题(势能线段树+复杂度分析)

    题面传送门 神仙题. 乍一看和经典题 花神游历各国有一点像,只不过多了一个区间加操作.不过多了这个区间加操作就无法再像花神游历各国那样暴力开根直到最小值为 \(1\) 为止的做法了,稍微感性理解一下即 ...

  9. [UOJ228] 基础数据结构练习题 - 线段树

    考虑到一个数开根号 \(loglog\) 次后就会变成1,设某个Node的势能为 \(loglog(maxv-minv)\) ,那么一次根号操作会使得势能下降 \(1\) ,一次加操作最多增加 \(l ...

随机推荐

  1. read 命令详解

    read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 语法选项 -p read –p “提示语句”,则屏幕就会输出提示语句,等待输入,并将输入存储在REPLY中 -n ...

  2. 【react懒加载组件】--react-lazyload

    组件安装: npm install react-lazyload --save-dev 组件使用: //引入 import LazyLoad from 'react-lazyload'; //rend ...

  3. org.springframework.dao.DuplicateKeyException

    org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [insert into account v ...

  4. Tencent QQ现在就是一个十八层地狱下面的大恶魔-删除右键里的"通过QQ发送到"

    都是流氓软件, 有人推荐装什么管家什么助手来清除, 那就是请走一个流氓又引进另外一个流氓. 下面的注册表项直接手工删除 32位系统: windows Registry Editor Version 5 ...

  5. script 跳出小窗口

    sss  

  6. win7改装 CentOS7,装完后开机,没有引导

    本来系统是win7,安装centos是用U盘启动安装方式安装成功后,发现win7的系统引导不见了.之前用的是centos6.4安装后依然保留win7引导的,到centos7就不行了 解决方法1.使用r ...

  7. 为什么不应该使用ZooKeeper做服务发现

    [编者的话]本文作者通过ZooKeeper与Eureka作为Service发现服务(注:WebServices体系中的UDDI就是个发现服务)的优劣对比,分享了Knewton在云计算平台部署服务的经验 ...

  8. 通过shell查找访问日志中访问量最大的ip

    日志格式: /Sep/::: +] /Sep/::: +] /Sep/::: +] - /Sep/::: +] - /Sep/::: +] /Sep/::: +] - /Sep/::: +] /Sep ...

  9. vue2.0子组件修改父组件props数据的值

    从vue1.0升级至2.0之后 prop的.sync被去除 因此直接在子组件修改父组件的值是会报错的如下: 目的是为了阻止子组件影响父组件的数据那么在vue2.0之后 如何在子组件修改父组件props ...

  10. vue学习【第七篇】:Vue之导入Bootstrap

    Vue引入bootstrap主要有两种方法 方法一:在main.js中引入 此方法导入的bootstrap中对于html,body的一些预设置的css样式可能无效 引入jQuery 在当前项目的目录下 ...