题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828

给你n个数,三种操作。操作1是将l到r之间的数都加上x;操作2是将l到r之间的数都开方;操作3是求出l到r之间的和。

操作1和3就不说了,关键是开方操作。

一个一个开方,复杂度太高,无疑会T。所以我们来剪枝一下。

我们可以观察,这里一个数最多开方4,5次(loglogx次)就会到1,所以要是一段区间最大值为1的话,就不需要递归开方下去了。这是一个剪枝。

如果一段区间的数都是一样大小(最大值等于最小值),那么开方的话,值也会相同。所以只要一次开方就好了,而一次开方也相当于减去 x-sqrt(x)。这是剪枝二。

有了两个剪枝,原本是过了... 后来数据加强了,就T了,无奈...

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int N = 1e5 + ;
struct SegTree {
int l, r, mid;
LL sum, lazy, Max, Min;
}T[N << ]; void build(int p, int l, int r) {
int ls = p << , rs = (p << )|;
T[p].l = l, T[p].r = r, T[p].mid = (l + r) >> , T[p].lazy = ;
if(l == r) {
scanf("%lld", &T[p].sum);
T[p].Max = T[p].Min = T[p].sum;
return ;
}
build(ls, l, T[p].mid);
build(rs, T[p].mid + , r);
T[p].sum = (T[ls].sum + T[rs].sum);
T[p].Min = min(T[ls].Min, T[rs].Min);
T[p].Max = max(T[ls].Max, T[rs].Max);
} void update_add(int p, int l, int r, LL val) {
int ls = p << , rs = (p << )|;
if(T[p].l == l && T[p].r == r) {
T[p].Min += val;
T[p].Max += val;
T[p].sum += (r - l + ) * val;
T[p].lazy += val;
return ;
}
if(T[p].lazy) {
T[ls].lazy += T[p].lazy, T[rs].lazy += T[p].lazy;
T[ls].sum += (T[ls].r - T[ls].l + )*T[p].lazy;
T[rs].sum += (T[rs].r - T[rs].l + )*T[p].lazy;
T[ls].Max += T[p].lazy, T[ls].Min += T[p].lazy;
T[rs].Max += T[p].lazy, T[rs].Min += T[p].lazy;
T[p].lazy = ;
}
if(r <= T[p].mid) {
update_add(ls, l, r, val);
}
else if(l > T[p].mid) {
update_add(rs, l, r, val);
}
else {
update_add(ls, l, T[p].mid, val);
update_add(rs, T[p].mid + , r, val);
}
T[p].sum = (T[ls].sum + T[rs].sum);
T[p].Min = min(T[ls].Min, T[rs].Min);
T[p].Max = max(T[ls].Max, T[rs].Max);
} void update(int p, int l, int r) {
if(T[p].Max == ) //最大值为1 就不需要开方了
return ;
int ls = p << , rs = (p << )|;
if(T[p].l == l && T[p].r == r && T[p].Max == T[p].Min) {
LL temp = T[p].Max - (LL)sqrt(T[p].Max*1.0);
T[p].Max -= temp;
T[p].Min -= temp;
T[p].lazy -= temp;
T[p].sum -= (r - l + )*temp;
return ;
}
if(T[p].lazy) {
T[ls].lazy += T[p].lazy, T[rs].lazy += T[p].lazy;
T[ls].sum += (T[ls].r - T[ls].l + )*T[p].lazy;
T[rs].sum += (T[rs].r - T[rs].l + )*T[p].lazy;
T[ls].Max += T[p].lazy, T[ls].Min += T[p].lazy;
T[rs].Max += T[p].lazy, T[rs].Min += T[p].lazy;
T[p].lazy = ;
}
if(r <= T[p].mid) {
update(ls, l, r);
}
else if(l > T[p].mid) {
update(rs, l, r);
}
else {
update(ls, l, T[p].mid);
update(rs, T[p].mid + , r);
}
T[p].sum = (T[ls].sum + T[rs].sum);
T[p].Min = min(T[ls].Min, T[rs].Min);
T[p].Max = max(T[ls].Max, T[rs].Max);
} LL query(int p, int l, int r) {
int ls = p << , rs = (p << )|;
if(T[p].l == l && T[p].r == r) {
return T[p].sum;
}
if(T[p].lazy) {
T[ls].lazy += T[p].lazy, T[rs].lazy += T[p].lazy;
T[ls].sum += (T[ls].r - T[ls].l + )*T[p].lazy;
T[rs].sum += (T[rs].r - T[rs].l + )*T[p].lazy;
T[ls].Max += T[p].lazy, T[ls].Min += T[p].lazy;
T[rs].Max += T[p].lazy, T[rs].Min += T[p].lazy;
T[p].lazy = ;
}
if(r <= T[p].mid) {
return query(ls, l, r);
}
else if(l > T[p].mid) {
return query(rs, l, r);
}
else {
return query(ls, l, T[p].mid) + query(rs, T[p].mid + , r);
}
} int main()
{
int n, m, t, c, l, r;
LL val;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
build(, , n);
while(m--) {
scanf("%d %d %d", &c, &l, &r);
if(c == ) {
scanf("%lld", &val);
update_add(, l, r, val);
}
else if(c == ) {
update(, l, r);
}
else {
printf("%lld\n", query(, l, r));
}
}
}
return ;
}

HDU 5828 Rikka with Sequence (线段树+剪枝优化)的更多相关文章

  1. hdu 5828 Rikka with Sequence 线段树

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

  2. HDU 5828 Rikka with Sequence(线段树区间加开根求和)

    Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...

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

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  4. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

  5. HDU 5828 Rikka with Sequence (线段树)

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

  6. HDU 5828 Rikka with Sequence(线段树 开根号)

    Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  7. 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区间上的所以数变成 ...

  8. HDU 6089 Rikka with Terrorist (线段树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6089 题解 这波强行维护搞得我很懵逼... 扫描线,只考虑每个点能走到左上方(不包括正上方,但包括正左 ...

  9. HDU 5634 Rikka with Phi 线段树

    题意:bc round 73 div1 D 中文题面 分析:注意到10^7之内的数最多phi O(log(n))次就会变成1, 因此可以考虑把一段相同的不为1的数缩成一个点,用平衡树来维护. 每次求p ...

随机推荐

  1. HDU 3068 (Manacher) 最长回文

    求一个字符串的最长子串,Manacher算法是一种O(n)的算法,很给力! s2[0] = '$',是避免在循环中对数组越界的检查. 老大的代码: http://www.cnblogs.com/Big ...

  2. swift2.0 Cannot assign a value of type '[CFString]' to a value of type '[String]'

    Cannot assign a value of type '[CFString]' to a value of type '[String]' 代码示例如下: picker.mediaTypes = ...

  3. clearfix 清除浮动的问题

    今天看一篇博文,发现其实有很多方法实现清除浮动,各有利弊 采用伪类:after进行后续空制的高度位零的伪类层清除 采用CSS overflow:auto的方式撑高 采用CSS overflow:hid ...

  4. 【英语】Bingo口语笔记(23) - 万圣节系列

    jack-o-lantern 杰克灯(南瓜灯) spooky 幽灵般的

  5. 利用ICSharpCode.SharpZipLib.Zip进行文件压缩

    官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...

  6. JBPM4之decision节点:2、好学生|坏学生|超级学生

    JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流程图的插件 JBPM4入门——3.JBPM4开发环境的搭建 JBPM4入门—— ...

  7. 数据绑定表达式(上):.NET发现之旅(一)

    数据绑定表达式(上):.NET发现之旅(一) 2009-06-30 10:29:06 来源:网络转载 作者:佚名 共有评论(0)条 浏览次数:859 作为.NET平台软件开发者,我们频繁与各种各样的数 ...

  8. AngularJS自定义指令(Directives)在IE8下的一个坑

    在项目中,由于要兼容到IE8,我使用1.2.8版本的angularJS.这个版本是支持自定义指令的.我打算使用自定义指令将顶部的header从其他页面分离.也就是实现在需要header的页面只用在&l ...

  9. Windows下Cygwin中使用NCView

    1. 使用cygwin的setup.exe安装 NetCDF, HDF5, Curl, libXaw, libICE, udunits, libexpat 和 libpng: 在选择库界面搜索:&qu ...

  10. 在Jenkins中使用Git Plugin访问Https代码库失败的问题

    最近需要在Jenkins上配置一个Job,SCM源是http://git.opendaylight.org/gerrit/p/integration.git 于是使用Jenkins的Git Plugi ...