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. 锋利的JQuery —— 事件和动画

    大图猛戳

  2. salesforce 零基础学习(二十三)数据记录导出至excel(自定义报表导出)

    我们都知道,报表有个功能为导出excel,但是有的时候客户需求往往标准的报表达不到,比如导出excel,其中本月修改的数据字段标红,如下图所示. 这就需要我们去写VF来实现此功能. 需求:将数据表记录 ...

  3. iOS-数据持久化基础-沙盒机制

    沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...

  4. jQuery学习-打字游戏

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  5. DOM_05之DOM、BOM常用对象

    1.HTML DOM常用对象之Table:①创建:createTHead():createTBody():createTFoot():②删除:deleteTHead():deleteTFoot():③ ...

  6. DOM_04之常用对象及BOM

    1.添加:①var a=document.createElement("a"):②设置关键属性:③将元素添加到DOM树:a.parent.appendChild(a):b.pare ...

  7. SQL server 临时表

    创建临时表,#代表局部临时表,##代表全局临时表.局部临时表和全局临时表的具体含义是什么呢? 举例说明一下比较清晰些,先来看下局部临时表,[新建查询],在里面输入如下文本: 运行后,我们在此文件执行输 ...

  8. Enerprise Solution Main 启动方法源代码

    .NET 系统以Main方法作为应用程序的启动入口点,Enterprise Solution的启动程序源代码如下: [STAThread] static void Main() { string MA ...

  9. Yii的学习(2)--数据访问对象 (DAO)

    摘自Yii官网:http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.dao Yii提供了强大的数据库编程支持.Yii数据访问对象(DAO) ...

  10. MYSQL的深入学习--优化步骤

    MySql优化的一般步骤 1.通过show status 命令了解各种sql的执行效率 SHOW STATUS提供msyql服务器的状态信息 一般情况下,我们只需要了解以”Com”开头的指令 show ...