2016暑假多校联合---Rikka with Sequence (线段树)

Problem 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
 
Author
学军中学
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5831 5830 5829 5827 5826 
 

题意: 三种操作,1、区间上加上一个数;
                       2、区间上所有数开根号向下取整;
                       3、区间求和;

思路: 对于记录区间的最大值和最小值,如果相等的话,那么只需要对一个数开根号,算出开根号前后的差值,这样区间开根号就变成了区间减去一个数了; 
         由于是开根,所以存在两个数刚开始差为1,加上某数再开根依旧是差1,这样维护相同数区间的就没用了
         比如(2,3) +6-->(8,9)开根-->(2,3)如果全是这样的操作,即使维护相同的数,每次开根的复杂度都是O(N),不T才怪
         这样只需要维护区间最大值最小值,当差1的时候,看看是否开根后还是差1,如果还是差1,那么对区间开根号相当于整个区间减去同一个数,
         这样就可以变开根为减了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int BufferSize=<<;
char buffer[BufferSize],*head,*tail;
inline char Getchar()
{
if(head==tail)
{
int l=fread(buffer,,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read()
{
int x=,f=;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-;
for(;isdigit(c);c=Getchar()) x=x*+c-'';
return x*f;
}
///----------------------------------------------------------------------
const int N=1e5+;
LL sum[N<<],lz[N<<],mx[N<<],mn[N<<]; void up(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
mx[rt]=max(mx[rt<<],mx[rt<<|]);
mn[rt]=min(mn[rt<<],mn[rt<<|]);
} void build(int rt,int l,int r)
{
lz[rt]=;
if(l==r){sum[rt]=read();mn[rt]=mx[rt]=sum[rt];return;}
int mid=l+r>>;
build(rt<<,l,mid);build(rt<<|,mid+,r);
up(rt);
} void down(int rt,int l,int r)
{
if(lz[rt]!=)
{
int mid=l+r>>;
lz[rt<<]+=lz[rt];
lz[rt<<|]+=lz[rt];
mn[rt<<]+=lz[rt];
mx[rt<<]+=lz[rt];
mx[rt<<|]+=lz[rt];
mn[rt<<|]+=lz[rt];
sum[rt<<]+=lz[rt]*(mid-l+);
sum[rt<<|]+=lz[rt]*(r-mid);
lz[rt]=;
}
} int x,y,t,T,n,m; void kaigen(int rt,int l,int r)
{
if(x<=l&&r<=y)
{
if(mx[rt]==mn[rt])
{
lz[rt]-=mx[rt];
mx[rt]=sqrt(mx[rt]);
mn[rt]=mx[rt];
lz[rt]+=mx[rt];
sum[rt]=mx[rt]*(r-l+);
return;
}
else if(mx[rt]==mn[rt]+)
{
LL x1=sqrt(mx[rt]);
LL x2=sqrt(mn[rt]);
if(x1==x2+)
{
lz[rt]-=(mx[rt]-x1);
sum[rt]-=(mx[rt]-x1)*(r-l+);
mx[rt]=x1;mn[rt]=x2;
return;
}
}
}
int mid=l+r>>;down(rt,l,r);
if(x<=mid)kaigen(rt<<,l,mid);
if(y>mid)kaigen(rt<<|,mid+,r);
up(rt);
} void add(int rt,int l,int r)
{
if(x<=l&&r<=y)
{
lz[rt]+=t;
sum[rt]+=(long long)(r-l+)*t;
mx[rt]+=t;mn[rt]+=t;
return ;
}
int mid=l+r>>;down(rt,l,r);
if(x<=mid)add(rt<<,l,mid);
if(y>mid)add(rt<<|,mid+,r);
up(rt);
} LL get(int rt,int l,int r)
{
if(x<=l&&r<=y)return sum[rt];
int mid=l+r>>;down(rt,l,r);
LL ret=;
if(x<=mid)ret+=get(rt<<,l,mid);
if(y>mid)ret+=get(rt<<|,mid+,r);
return ret;
} int main()
{
T=read();
while(T--)
{
n=read();m=read();
build(,,n);
while(m--)
{
int op;
op=read();x=read();y=read();
if(op==)
{
t=read();
add(,,n);
}
else if(op==)kaigen(,,n);
else printf("%I64d\n",get(,,n));
}
}
return ;
}
 

2016暑假多校联合---Rikka with Sequence (线段树)的更多相关文章

  1. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

  2. 2016暑假多校联合---Substring(后缀数组)

    2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...

  3. 2016暑假多校联合---To My Girlfriend

    2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...

  4. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

  5. 2016暑假多校联合---Another Meaning

    2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...

  6. hdu 5828 Rikka with Sequence 线段树

    Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

  7. 2016暑假多校联合---Death Sequence(递推、前向星)

    原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...

  8. 2016暑假多校联合---GCD

    Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...

  9. 2016暑假多校联合---Counting Intersections

    原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need t ...

随机推荐

  1. js 数组

    js中的数组类似与java中的容器 类型可以不同.长度可变 一.数组的声明 var arr1=new Array();//数组的声明一     var arr2=[1,2,3,true,new Dat ...

  2. 每天一个linux命令(14):head 命令

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  3. AngularJS $http配置为form data 提交

    AngularJS $http配置为form data 提交 $scope.formData = {}; $http({ method: 'POST', url: '/user/', // pass ...

  4. CSS滚动条

    × 目录 [1]条件 [2]默认 [3]尺寸[4]兼容[5]自定义 前面的话 滚动条在网页中经常见到,却并没有受到足够的重视.只有当因为滚动条的问题需要处理兼容性时,才进行调试操作.本文将就滚动条的常 ...

  5. Android开发之时间日期1

     对于手机的时间日期设置估计大家一定都不陌生吧,今天做了一个关于时间日期设置的小例子,其中遇到一个问题,求指导,如何使设置的时间日期和手机系统同步?还望高手指点一二. 先不说这个了,分享一下我的小例子 ...

  6. AIX下如何根据端口号查找相应的进程

    1. $ netstat -Aan |grep 8080 f1000e0002321bb8 tcp 0 0 *.8080 *.* LISTEN 2. $ rmsock f1000e0002321bb8 ...

  7. AngularJS in Action读书笔记3——走近Services

    试着想想这些问题:如果一个controller只关心自己所控制的view页面,那么对于整个application来说,你如何调用想要的function:如果controller从来都不会和其他cont ...

  8. Windows Azure Virtual Network (6) 设置Azure Virtual Machine固定公网IP (Virtual IP Address, VIP) (1)

    <Windows Azure Platform 系列文章目录> 注意:本文介绍的是Global Azure (http://www.windowsazure.com),如果你使用的是由世纪 ...

  9. iOS开发的一些奇巧淫技

    TableView不显示没内容的Cell怎么办? 类似这种,我不想让下面那些空的显示. 很简单. self.tableView.tableFooterView = [[UIView alloc] in ...

  10. MySQL5.7(5.6)GTID环境下恢复从库思(qi)路(yin)方(ji)法(qiao)

      要讨论如何恢复从库,我们得先来了解如下一些概念: GTID_EXECUTED:它是一组包含已经记录在二进制日志文件中的事务集合 GTID_PURGED:它是一组包含已经从二进制日志删除掉的事务集合 ...