#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. linux /etc/shadow--passwd/pam.d/system-auth文件详解

     在linux操作系统中, /etc/passwd文件中的每个用户都有一个对应的记录行,记录着这个用户的一下基本属性.该文件对所有用户可读.   而/etc/shadow文件正如他的名字一样,他是pa ...

  2. GoldenGate Logdump基本使用

    Logdump是GoldenGate复制软件中附带的一个工具软件,在OGG的目录下可以找到.这个工具主要用于分析OGG生成的队列文件,查找记录.统计队列文件中的数据等. 在OGG安装目录下执行logd ...

  3. 面试必问之JVM篇

    前言 只有光头才能变强 JVM在准备面试的时候就有看了,一直没时间写笔记.现在到了一家公司实习,闲的时候就写写,刷刷JVM博客,刷刷电子书. 学习JVM的目的也很简单: 能够知道JVM是什么,为我们干 ...

  4. php 加密 解密 密码传输

    php 加密 解密 密码传输 <?php /* * * 使用按位异或运算 加密 * $str 明文 * $salt 盐 * */ public static function xor_encry ...

  5. 作为phper既然了解共享内存函数shmop的使用方法,那么就必须要了解一下信号量是什么,以及信号量使用的代码案例

    在单独的一个PHP进程中读写.创建.删除共享内存方面上你应该没有问题了.但是实际运行中不可能只是一个PHP进程在运行中.如果在多个进程的情况下你还是沿用单个进程的处理方法,你一定会碰到问题--著名的并 ...

  6. shell脚本一键安装jdk

    直接上shell #!/bin/bash #offline jdk install ipath="/usr/local" installpath=$(cd `dirname $0` ...

  7. ldap集成nexus

    nexus版本:2.14.4 添加nexus支持ldap认证: 管理员登录,点击 Administration --> Server -->Security Settings,将 OSS ...

  8. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  9. noip 2013 提高组 day1

    1.转圈游戏: 解析部分略,快速幂就可以过 Code: #include<iostream> #include<fstream> using namespace std; if ...

  10. Loader

    1.定义 可以把Loader当做一个占位符,即占有屏幕的某一个空间,当加载了组件之后,这个空间就能显示相应的图形了.所以可以给Loader设置anchor布局 2.加载组件 source:加载QML文 ...