#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

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. 基础数据结构练习题 线段树的更多相关文章

  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. python shutil模块简单介绍

    python shutil模块简单介绍 简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. shutil 模块方法: copy(src, ...

  2. Django框架----视图(views)

    Django的Views(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误 ...

  3. 基于Theano的深度学习框架keras及配合SVM训练模型

    https://blog.csdn.net/a819825294/article/details/51334397 1.介绍 Keras是基于Theano的一个深度学习框架,它的设计参考了Torch, ...

  4. right spindle supply short to gnd-- compact version

    hardware guy found that the R1004 lead to this error, but this error should not be checked, because ...

  5. django表单的api

    django表单的api,参考文档:https://yiyibooks.cn/xx/Django_1.11.6/ref/forms/api.html 绑定与未绑定形式: Form要么是绑定的,要么是未 ...

  6. 管理mycat命令详解

    mycat监听两个端口,分别为8066和9066:mycat服务默认的数据端口是8066,而9066端口则是mycat管理端口,用于管理mycat的整个集群状态.监听的端口可以在server.xml配 ...

  7. P2617 Dynamic Rankings(树状数组套主席树)

    P2617 Dynamic Rankings 单点修改,区间查询第k大 当然是无脑树套树了~ 树状数组套主席树就好辣 #include<iostream> #include<cstd ...

  8. Vue 旅游网首页开发1-工具安装及码云使用

    Vue 旅游网首页开发-工具安装及码云使用 环境安装 安装 node.js node.js 官网:https://nodejs.org/en/ 注册码云,创建私密仓库存储项目 码云:https://g ...

  9. opencv学习之路(2)、读取视频,读取摄像头

    一.介绍 视频读取本质上就是读取图像,因为视频是由一帧一帧图像组成的.1秒24帧基本就能流畅的读取视频了. ①读取视频有两种方法: A. VideoCapture cap; cap.open(“1.a ...

  10. 集合框架-Map集合

    * Map集合和Collection集合的区别? * Map集合存储元素是成对出现的,Map集合的键是唯一的,值是可重复的.可以把这个理解为:夫妻对 * Collection集合存储元素是单独出现的, ...