uoj #228. 基础数据结构练习题 线段树
#228. 基础数据结构练习题
sylvia 是一个热爱学习的女孩子,今天她想要学习数据结构技巧。
在看了一些博客学了一些姿势后,她想要找一些数据结构题来练练手。于是她的好朋友九条可怜酱给她出了一道题。
给出一个长度为 nn 的数列 AA,接下来有 mm 次操作,操作有三种:
- 对于所有的 i∈[l,r]i∈[l,r],将 AiAi 变成 Ai+xAi+x。
- 对于所有的 i∈[l,r]i∈[l,r],将 AiAi 变成 ⌊Ai−−√⌋⌊Ai⌋。
- 对于所有的 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
- 5 5
- 1 2 3 4 5
- 1 3 5 2
- 2 1 4
- 3 2 4
- 2 3 5
- 3 1 5
output
- 5
- 6
inline大法好;
读入优化大法好。
判断区间最小值跟最大值相差1或者0即可;
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<string>
- #include<queue>
- #include<algorithm>
- #include<stack>
- #include<cstring>
- #include<vector>
- #include<list>
- #include<set>
- #include<map>
- using namespace std;
- #define ll long long
- #define pi (4*atan(1.0))
- #define eps 1e-7
- #define bug(x) cout<<"bug"<<x<<endl;
- const int N=1e5+,M=1e6+,inf=;
- const ll INF=1e18+,mod=;
- inline ll scan()
- {
- ll res = , ch ;
- while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
- {
- if( ch == EOF ) return 1LL << ;
- }
- res = ch - '' ;
- while( ( ch = getchar() ) >= '' && ch <= '' )
- res = res * + ( ch - '' ) ;
- return res ;
- }
- /// 数组大小
- int n;
- ll sum[N<<],minn[N<<],maxx[N<<],lazy[N<<],cov[N<<];
- inline void pushup(int pos)
- {
- minn[pos]=min(minn[pos<<],minn[pos<<|]);
- maxx[pos]=max(maxx[pos<<|],maxx[pos<<]);
- sum[pos]=sum[pos<<]+sum[pos<<|];
- }
- inline void pushdown(int pos,int l,int r)
- {
- if(cov[pos])
- {
- int mid=(l+r)>>;
- cov[pos<<]=cov[pos];
- cov[pos<<|]=cov[pos];
- maxx[pos<<]=cov[pos];
- maxx[pos<<|]=cov[pos];
- minn[pos<<]=cov[pos];
- minn[pos<<|]=cov[pos];
- sum[pos<<]=cov[pos]*(mid-l+);
- sum[pos<<|]=cov[pos]*(r-mid);
- lazy[pos<<]=;
- lazy[pos<<|]=;
- cov[pos]=;
- }
- if(lazy[pos])
- {
- int mid=(l+r)>>;
- lazy[pos<<]+=lazy[pos];
- lazy[pos<<|]+=lazy[pos];
- maxx[pos<<]+=lazy[pos];
- maxx[pos<<|]+=lazy[pos];
- minn[pos<<]+=lazy[pos];
- minn[pos<<|]+=lazy[pos];
- sum[pos<<]+=lazy[pos]*(mid-l+);
- sum[pos<<|]+=lazy[pos]*(r-mid);
- lazy[pos]=;
- }
- }
- inline void build(int l,int r,int pos)
- {
- lazy[pos]=;
- cov[pos]=;
- if(l==r)
- {
- sum[pos]=scan();
- minn[pos]=maxx[pos]=sum[pos];
- return;
- }
- int mid=(l+r)>>;
- build(l,mid,pos<<);
- build(mid+,r,pos<<|);
- pushup(pos);
- }
- inline void update(int L,int R,ll c,int l,int r,int pos)
- {
- if(L<=l&&r<=R)
- {
- lazy[pos]+=c;
- minn[pos]+=c;
- maxx[pos]+=c;
- sum[pos]+=c*(r-l+);
- return;
- }
- pushdown(pos,l,r);
- int mid=(l+r)>>;
- if(L<=mid)update(L,R,c,l,mid,pos<<);
- if(R>mid)update(L,R,c,mid+,r,pos<<|);
- pushup(pos);
- }
- inline void update1(int L,int R,ll c,int l,int r,int pos)
- {
- if(L<=l&&r<=R)
- {
- cov[pos]=c;
- minn[pos]=c;
- maxx[pos]=c;
- sum[pos]=c*(r-l+);
- lazy[pos]=;
- return;
- }
- pushdown(pos,l,r);
- int mid=(l+r)>>;
- if(L<=mid)update1(L,R,c,l,mid,pos<<);
- if(R>mid)update1(L,R,c,mid+,r,pos<<|);
- pushup(pos);
- }
- inline void update2(int L,int R,int l,int r,int pos)
- {
- if(l==L&&R==r&&maxx[pos]==minn[pos])
- {
- ll x=(ll)floor(sqrt(maxx[pos]));
- update1(L,R,x,,n,);
- return;
- }
- if(l==L&&R==r&&maxx[pos]==minn[pos]+)
- {
- ll x=(ll)floor(sqrt(maxx[pos]));
- ll y=(ll)floor(sqrt(minn[pos]));
- if(x==y)update1(L,R,x,,n,);
- else update(L,R,x-maxx[pos],,n,);
- return;
- }
- pushdown(pos,l,r);
- int mid=(l+r)>>;
- if(R<=mid)update2(L,R,l,mid,pos<<);
- else if(L>mid)update2(L,R,mid+,r,pos<<|);
- else
- {
- update2(L,mid,l,mid,pos<<);
- update2(mid+,R,mid+,r,pos<<|);
- }
- pushup(pos);
- }
- inline ll query(int L,int R,int l,int r,int pos)
- {
- if(L<=l&&r<=R)return sum[pos];
- pushdown(pos,l,r);
- int mid=(l+r)>>;
- ll ans=;
- if(L<=mid)ans+=query(L,R,l,mid,pos<<);
- if(R>mid)ans+=query(L,R,mid+,r,pos<<|);
- return ans;
- }
- int main()
- {
- int m;
- n=scan();
- m=scan();
- build(,n,);
- while(m--)
- {
- int t,l,r;
- t=scan();
- l=scan();
- r=scan();
- if(t==)
- {
- ll x;
- x=scan();
- update(l,r,x,,n,);
- }
- else if(t==) update2(l,r,,n,);
- else printf("%lld\n",query(l,r,,n,));
- }
- return ;
- }
uoj #228. 基础数据结构练习题 线段树的更多相关文章
- uoj#228. 基础数据结构练习题(线段树区间开方)
题目链接:http://uoj.ac/problem/228 代码:(先开个坑在这个地方) #include<bits/stdc++.h> using namespace std; ; l ...
- UOJ #228. 基础数据结构练习题 线段树 + 均摊分析 + 神题
题目链接 一个数被开方 #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",st ...
- 【线段树】uoj#228. 基础数据结构练习题
get到了标记永久化 sylvia 是一个热爱学习的女孩子,今天她想要学习数据结构技巧. 在看了一些博客学了一些姿势后,她想要找一些数据结构题来练练手.于是她的好朋友九条可怜酱给她出了一道题. 给出一 ...
- 【UOJ#228】基础数据结构练习题 线段树
#228. 基础数据结构练习题 题目链接:http://uoj.ac/problem/228 Solution 这题由于有区间+操作,所以和花神还是不一样的. 花神那道题,我们可以考虑每个数最多开根几 ...
- uoj#228 基础数据结构练习题
题面:http://uoj.ac/problem/228 正解:线段树. 我们可以发现,开根号时一个区间中的数总是趋近相等.判断一个区间的数是否相等,只要判断最大值和最小值是否相等就行了.如果这个区间 ...
- 【uoj#228】基础数据结构练习题 线段树+均摊分析
题目描述 给出一个长度为 $n$ 的序列,支持 $m$ 次操作,操作有三种:区间加.区间开根.区间求和. $n,m,a_i\le 100000$ . 题解 线段树+均摊分析 对于原来的两个数 $a$ ...
- uoj#228. 基础数据结构练习题(线段树)
传送门 只有区间加区间开方我都会--然而加在一起我就gg了-- 然后这题的做法就是对于区间加直接打标记,对于区间开方,如果这个区间的最大值等于最小值就直接区间覆盖(据ljh_2000大佬说这个区间覆盖 ...
- UOJ #228 - 基础数据结构练习题(势能线段树+复杂度分析)
题面传送门 神仙题. 乍一看和经典题 花神游历各国有一点像,只不过多了一个区间加操作.不过多了这个区间加操作就无法再像花神游历各国那样暴力开根直到最小值为 \(1\) 为止的做法了,稍微感性理解一下即 ...
- [UOJ228] 基础数据结构练习题 - 线段树
考虑到一个数开根号 \(loglog\) 次后就会变成1,设某个Node的势能为 \(loglog(maxv-minv)\) ,那么一次根号操作会使得势能下降 \(1\) ,一次加操作最多增加 \(l ...
随机推荐
- read 命令详解
read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 语法选项 -p read –p “提示语句”,则屏幕就会输出提示语句,等待输入,并将输入存储在REPLY中 -n ...
- 【react懒加载组件】--react-lazyload
组件安装: npm install react-lazyload --save-dev 组件使用: //引入 import LazyLoad from 'react-lazyload'; //rend ...
- org.springframework.dao.DuplicateKeyException
org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [insert into account v ...
- Tencent QQ现在就是一个十八层地狱下面的大恶魔-删除右键里的"通过QQ发送到"
都是流氓软件, 有人推荐装什么管家什么助手来清除, 那就是请走一个流氓又引进另外一个流氓. 下面的注册表项直接手工删除 32位系统: windows Registry Editor Version 5 ...
- script 跳出小窗口
sss
- win7改装 CentOS7,装完后开机,没有引导
本来系统是win7,安装centos是用U盘启动安装方式安装成功后,发现win7的系统引导不见了.之前用的是centos6.4安装后依然保留win7引导的,到centos7就不行了 解决方法1.使用r ...
- 为什么不应该使用ZooKeeper做服务发现
[编者的话]本文作者通过ZooKeeper与Eureka作为Service发现服务(注:WebServices体系中的UDDI就是个发现服务)的优劣对比,分享了Knewton在云计算平台部署服务的经验 ...
- 通过shell查找访问日志中访问量最大的ip
日志格式: /Sep/::: +] /Sep/::: +] /Sep/::: +] - /Sep/::: +] - /Sep/::: +] /Sep/::: +] - /Sep/::: +] /Sep ...
- vue2.0子组件修改父组件props数据的值
从vue1.0升级至2.0之后 prop的.sync被去除 因此直接在子组件修改父组件的值是会报错的如下: 目的是为了阻止子组件影响父组件的数据那么在vue2.0之后 如何在子组件修改父组件props ...
- vue学习【第七篇】:Vue之导入Bootstrap
Vue引入bootstrap主要有两种方法 方法一:在main.js中引入 此方法导入的bootstrap中对于html,body的一些预设置的css样式可能无效 引入jQuery 在当前项目的目录下 ...