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 ...
随机推荐
- Mongo第三个参数的用法
Mongo update的用法 Update( array $criteria , array $new_object [, array $options = array() ] ) 第一个参数是条件 ...
- JOBDU 题目1100:最短路径
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5786 解决:902 题目描述: N个城市,标号从0到N-1,M条道路,第K条道路(K从0开始)的长度为2^K,求编号为0的城市到其他城市的 ...
- AJAX(表单验证)/JSON之一
## 什么是Ajax AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). 1. 异步通信:浏览器利用独立的线程与服务器进行通信, ...
- Eloquent JavaScript #01# values
When action grows unprofitable, gather information; when information grows unprofitable, sleep. ...
- django 分页函数
实现类似: 上一页 1 ... 4 5 7 8 ... 89 下一页 的效果 def pageGenerate(fullList,pagenum,urltype,type,currpage): pag ...
- Golang字符串解析成数字
package main import ( "strconv" "fmt" ) func main() { // 使用ParseFloat解析浮点数,64是说明 ...
- Cannot resolve reference to bean 'sqlSessionFactory' while setting bean
今天下载公司以前的一个项目,要和现在的项目进行整合,下载一切顺利,但在开发机器上一跑,憋了. 有两种日志,两种报错:一个是cannot resolve reference to bean 'sqlSe ...
- 论文阅读笔记 Improved Word Representation Learning with Sememes
论文阅读笔记 Improved Word Representation Learning with Sememes 一句话概括本文工作 使用词汇资源--知网--来提升词嵌入的表征能力,并提出了三种基于 ...
- hog cython
pip安装cython之后,将下面代码写入hogtest2.pyx文件(我通过改文件后缀新建) import numpy as np from PIL import Image cimport num ...
- 【python38--面向对象继承】
一.继承 1.语法:class DerivedClassName(BaseClassName):子类继承父类 >>> class Parent: def hello(self): p ...