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 ...
随机推荐
- 10.29训练赛第一场B题
题目大意:有n个队伍之间比赛,每两个队伍之间都有一场比赛,因此一共有n(n-1) / 2场比赛,但是这里丢失了一场比赛的记录,现在让你通过n(n-1) /2 -1场仍然存在的比赛记录来判断丢失的那条比 ...
- 第5月第8天 jsonmodel
1. @implementation JSONValueTransformer (CustomTransformer) //时间戳转NSDate - (NSDate *)NSDateFromNSStr ...
- 数链剖分(Housewife Wind )
题目链接:https://vjudge.net/contest/279350#problem/B 题目大意:给你n,q,s.n指的是有n个点,q代表有q次询问,s代表的是起点.然后接下来会有n-1条 ...
- Git常见错误处理
如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 提示出错信息:fatal: remo ...
- 用于阻止缓冲区溢出攻击的 Linux 内核参数与 gcc 编译选项
先来看看基于 Red Hat 与 Fedora 衍生版(例如 CentOS)系统用于阻止栈溢出攻击的内核参数,主要包含两项: kernel.exec-shield 可执行栈保护,字面含义比较“绕”, ...
- 『记录』Android参考资料
欢迎留言推荐好的教程.资料.博客及作者等. 『记录』Android参考资料 1.前期环境 Android Studio使用Git Android Studio快捷键总结 Android Studio及 ...
- Tesseract-OCR 3.05 多过语言文字识别(运行程序+中英日韩语言包)
最新版本 静态编译 tesseract 3.05.00dev leptonica-1.73 libgif 5.1.3 : libjpeg 8c : libpng 1.6.16 : libtiff 3. ...
- Linux磁盘分区UUID的获取及其UUID的作用
注:UUID-Universally Unique IDentifiers全局唯一标识符 一.Linux磁盘分区UUID的获取方法 1.[san@localhost ~]$ ls -l /dev/di ...
- Jmeter之逻辑控制器(Logic Controller)【转】
Jmeter之逻辑控制器(Logic Controller) 前言: 1. Jmeter官网对逻辑控制器的解释是:“Logic Controllers determine the order in w ...
- Transition学习笔记
概述 Android 4.4.2 (API level 19)引入Transition框架,之后很多APP上都使用该框架做出很酷炫的效果,如 Google Play Newsstand app 还有g ...