2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树)
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?
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.
题意: 三种操作,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 (线段树)的更多相关文章
- 2016暑假多校联合---Windows 10
2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...
- 2016暑假多校联合---Substring(后缀数组)
2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...
- 2016暑假多校联合---To My Girlfriend
2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...
- 2016暑假多校联合---A Simple Chess
2016暑假多校联合---A Simple Chess Problem Description There is a n×m board, a chess want to go to the po ...
- 2016暑假多校联合---Another Meaning
2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...
- hdu 5828 Rikka with Sequence 线段树
Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...
- 2016暑假多校联合---Death Sequence(递推、前向星)
原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...
- 2016暑假多校联合---GCD
Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...
- 2016暑假多校联合---Counting Intersections
原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need t ...
随机推荐
- [Redis]Redis 概述及基本使用规范.
1 nosql的简介 1.1 nosql简介 随着互联网Web2.0网站的兴起,传统的关系数据库在应付Web2.0网站,特别是超大规模和高并发的SNS类型的Web2.0纯动态网站已经显得力不从心,暴露 ...
- [开发工具]Java开发常用的在线工具
注明: 本文转自http://www.hollischuang.com/archives/1459.作为一个Java开发人员,经常要和各种各样的工具打交道,除了我们常用的IDE工具以外,其实还有很多工 ...
- Atitti css3 新特性attilax总结
Atitti css3 新特性attilax总结 图片发光效果2 透明渐变效果2 文字描边2 背景拉伸2 CSS3 选择器(Selector)4 @Font-face 特性7 Word-wrap &a ...
- Java-set集合
package exception; import java.util.Map; import java.util.TreeMap; import java.util.HashMap; public ...
- 一次SSIS Package的调试经历
SSIS Package的调试有时是一个非常艰难的过程,由于SSIS 编译器给出的错误信息,可能并不完善,需要程序员根据错误信息抽丝拨茧,寻找错误的根源,进而解决问题. 第一部分:SSIS提供的调试工 ...
- oc连接signalr,各种填坑
在网上搜了signalr的oc客户端,基本上都指向同一个东西https://github.com/DyKnow/SignalR-ObjC 但是这个也有日子没更新了,用cocoapods安装下来是编译不 ...
- mac+apache+php+phpmyadmin集成php开发环境配置
刚开始才接触php才发现macos还是比较强大了,macbook不仅是时尚达品还很实用哦. --------------他山之石-------------------------- http://da ...
- Spring MVC 学习总结(四)——视图与综合示例
一.表单标签库 1.1.简介 从Spring2.0起就提供了一组全面的自动数据绑定标签来处理表单元素.生成的标签兼容HTML 4.01与XHTML 1.0.表单标签库中包含了可以用在JSP页面中渲染H ...
- ShineTime - 带有 CSS3 闪亮特效的缩略图相册
ShineTime 是一个效果非常精致的缩略图相册,鼠标悬停到缩略图的时候有很炫的闪光效果,基于 CSS3 实现,另外缩略图也会有立体移动的效果.特别适用于个人摄影作品,公司产品展示等用途,快来来围观 ...
- 十款让 Web 前端开发人员更轻松的实用工具
这篇文章介绍十款让 Web 前端开发人员生活更轻松的实用工具.每个 Web 开发人员都有自己的工具箱,这样工作中碰到的每个问题都有一个好的解决方案供选择. 对于每一项工作,开发人员需要特定的辅助工具, ...