hdu 5828 Rikka with Sequence 线段树
Rikka with Sequence
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5828
Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
Yuta has an array A with n numbers. Then he makes m operations on it.
There are three type of operations:
1 l r x : For each i in [l,r], change A[i] to A[i]+x
2 l r : For each i in [l,r], change A[i] to ⌊A−−√[i]⌋
3 l r : Yuta wants Rikka to sum up A[i] for all i in [l,r]
It is too difficult for Rikka. Can you help her?
Input
The first line contains a number t(1<=t<=100), the number of the testcases. And there are no more than 5 testcases with n>1000.
For each testcase, the first line contains two numbers n,m(1<=n,m<=100000). The second line contains n numbers A[1]~A[n]. Then m lines follow, each line describe an operation.
It is guaranteed that 1<=A[i],x<=100000.
Output
For each operation of type 3, print a lines contains one number -- the answer of the query.
Sample Input
1
5 5
1 2 3 4 5
1 3 5 2
2 1 4
3 2 4
2 3 5
3 1 5
Sample Output
5
6
Hint
题意
给你n个数,m个操作
一共有三类操作
区间开根号
区间加
区间求和
题解:
线段树,区间开根号最多十几次就会变成1,我们把相同值的数合并在一起去更新就好了
区间加,就正常做,和普通的线段树一样去做就好了。
区间求和也正常做……
其实就比普通的线段树多一个lazy,表示左右是否相同。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+7;
long long n;
int a[maxn];
int to[2000005];
typedef long long ll;
typedef long long SgTreeDataType;
const int BUF=40000000;
char Buf[BUF],*buf=Buf;
const int OUT=20000000;
char Out[OUT],*ou=Out;int Outn[30],Outcnt;
inline void write(int x){
if(!x)*ou++=48;
else{
for(Outcnt=0;x;x/=10)Outn[++Outcnt]=x%10+48;
while(Outcnt)*ou++=Outn[Outcnt--];
}
}
inline void writell(ll x){
if(!x)*ou++=48;
else{
for(Outcnt=0;x;x/=10)Outn[++Outcnt]=x%10+48;
while(Outcnt)*ou++=Outn[Outcnt--];
}
}
inline void writechar(char x){*ou++=x;}
inline void writeln(){*ou++='\n';}
inline void read(int&a){for(a=0;*buf<48;buf++);while(*buf>47)a=a*10+*buf++-48;}
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline int TransForm( long long x ){
if( x <= 2000000 ) return to[x];
return sqrt( x );
}
struct Sgtree{
struct treenode{
int l , r ;
long long f , lzy , sum ;
void Update( long long x ){
lzy += x;
sum += (r - l + 1) * x;
if( ~f ) f += x;
}
void SetUp( long long x ){
f = x;
sum = x * ( r - l + 1 );
}
void SqrtUp(){
f = TransForm( f );
sum = f * ( r - l + 1 );
}
}tree[maxn << 2];
void Push_Up( int o ){
tree[o].sum = tree[o << 1].sum + tree[o << 1 | 1].sum;
if( tree[o << 1].f == tree[o << 1 | 1].f ) tree[o].f = tree[o << 1].f;
else tree[o].f = -1;
}
void ReleaseLabel( int o ){
if( tree[o].lzy ){
tree[o << 1].Update( tree[o].lzy );
tree[o << 1 | 1].Update( tree[o].lzy );
tree[o].lzy = 0;
}
if( ~tree[o].f ){
tree[o << 1].SetUp( tree[o].f );
tree[o << 1 | 1].SetUp( tree[o].f );
}
}
void Build( int l , int r , int o ){
tree[o].l = l , tree[o].r = r , tree[o].lzy = tree[o].sum = 0;
if( r > l ){
int mid = l + r >> 1;
Build( l , mid , o << 1 );
Build( mid + 1 , r , o << 1 | 1 );
Push_Up( o );
}else tree[o].f = tree[o].sum = a[l];
}
void Add( int ql , int qr , int v , int o ){
int l = tree[o].l , r = tree[o].r;
if( ql <= l && r <= qr ) tree[o].Update( v );
else{
int mid = l + r >> 1;
ReleaseLabel( o );
if( ql <= mid ) Add( ql , qr , v , o << 1 );
if( qr > mid ) Add( ql , qr , v , o << 1 | 1 );
Push_Up( o );
}
}
void DFS( int o ){
if( ~tree[o].f ){
tree[o].SqrtUp( );
}else{
ReleaseLabel( o );
DFS( o << 1 );
DFS( o << 1 | 1 );
Push_Up( o );
}
}
void OpeSqrt( int ql , int qr , int o ){
int l = tree[o].l , r = tree[o].r;
if( ql <= l && r <= qr ) DFS( o );
else{
int mid = l + r >> 1;
ReleaseLabel( o );
if( ql <= mid ) OpeSqrt( ql , qr , o << 1 );
if( qr > mid ) OpeSqrt( ql , qr , o << 1 | 1 );
Push_Up( o );
}
}
long long Ask( int ql , int qr , int o ){
int l = tree[o].l , r = tree[o].r;
if( ql <= l && r <= qr ) return tree[o].sum;
else{
int mid = l + r >> 1;
ReleaseLabel( o );
long long Result = 0;
if( ql <= mid ) Result += Ask( ql , qr , o << 1 );
if( qr > mid ) Result += Ask( ql , qr , o << 1 | 1 );
Push_Up( o );
return Result;
}
}
}Sgtree;
int main()
{
fread(Buf,1,BUF,stdin);
for(int i=0;i<=2000000;i++)
to[i]=sqrt(i);
int t;
read(t);
while(t--){
int n,m;
read(n);read(m);
for(int i=1;i<=n;i++) read(a[i]);
Sgtree.Build( 1 , n , 1 );
while(m--)
{
int op,x,y,z;
read(op),read(x),read(y);
if(op==2) Sgtree.OpeSqrt( x , y , 1 );
if(op==1){
read(z);
Sgtree.Add( x , y , z , 1 );
}
if(op==3)writell(Sgtree.Ask(x,y,1)),writeln();
}
}
fwrite(Out,1,ou-Out,stdout);
return 0;
}
hdu 5828 Rikka with Sequence 线段树的更多相关文章
- HDU 5828 Rikka with Sequence (线段树+剪枝优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828 给你n个数,三种操作.操作1是将l到r之间的数都加上x:操作2是将l到r之间的数都开方:操作3是 ...
- HDU 5828 Rikka with Sequence(线段树区间加开根求和)
Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence
// 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...
- HDU 5828 Rikka with Sequence (线段树)
Rikka with Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...
- HDU 5828 Rikka with Sequence(线段树 开根号)
Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- hdu 4893 Wow! Such Sequence!(线段树)
题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...
- HDU 6089 Rikka with Terrorist (线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6089 题解 这波强行维护搞得我很懵逼... 扫描线,只考虑每个点能走到左上方(不包括正上方,但包括正左 ...
- HDU 5634 Rikka with Phi 线段树
题意:bc round 73 div1 D 中文题面 分析:注意到10^7之内的数最多phi O(log(n))次就会变成1, 因此可以考虑把一段相同的不为1的数缩成一个点,用平衡树来维护. 每次求p ...
随机推荐
- 那些年的 网络通信之 UDP 数据报包传输---
下面是 一个多线程,基于 UDP 用户数据报包 协议 的 控制台聊天小程序 import java.io.*; import java.net.*; class Send implements Run ...
- Javascript中与Scroll有关的方法
这块确实太乱了,被兼容搞的简直快要晕死,默默地总结下... 与scroll相关的方法 4个window对象下:scrollX.scrollY.scrollTo.scroll(作用和scrollTo一样 ...
- 20155338 2016-2017-2 《Java程序设计》第7周学习总结
20155338 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 本周学习了第十二章和第十三章的内容,我重点学习了第十三章时间与日期的相关内容. 时间的度量: ...
- shiro登录成功之后跳转原路径
通过 WebUtils.getSavedRequest(request) 来获取shiro保存在session登录之前的url 1:java Controller代码 @PostMapping(&qu ...
- python所有基础
下面就不一一列举了,所有的资料都和GitHub对接,到时候我有更新就直接拖到GitHub上面了.入门的小伙伴们可以进来看看,估计后面还会有很多项目,待更新.
- decimal模块
简介 decimal意思为十进制,这个模块提供了十进制浮点运算支持. 常用方法 1.可以传递给Decimal整型或者字符串参数,但不能是浮点数据,因为浮点数据本身就不准确. 2.要从浮点数据转换为De ...
- pip2和pip3冲突问题解决方法
python使用pip安装模块时报错:unable to create process using ' '的解决方法: 参考:http://qoogle.cn/?id=39 1.删除C:\Python ...
- Space Replacement
Write a method to replace all spaces in a string with %20. The string is given in a characters array ...
- Ubuntu+Nginx+uWSGI+Flask应用
Ubuntu 18.04,Nginx 1.14.0,uWSGI 2.0.17.1,Flask 1.0.2,Python 3.6.5, 多日未更新博客,就是在忙着把自己的Flask应用在Ubuntu上运 ...
- 解决C/C++语言中全局变量重复定义的问题
前言 今天,在整理自己的代码的时候,考虑到我写的代码从一至终都是在一个cpp文件里面.于是,想把自己的代码中的各个模块分离开来,以便更好地阅读和管理. 遇到的问题 我的做法是: 宏定义.结构体定义.函 ...