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
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(11000.
For each testcase, the first line contains two numbers n,m(1

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

Source


2016 Multi-University Training Contest 8


##题意:

对一个数组进行若干操作:
1. 将区间内的值都加x.
2. 将区间内的值都开平方.
3. 求区间内数的和.


##题解:

容易想到用线段树来维护,关键是如何处理操作二. 直接对每个数开平方肯定会超时.
考虑到开平方操作的衰减速度很快,一个数最多经过6次开平方操作就会到1.
那么随着操作的进行,区间内的数会趋于相同,恰好利用这个点来作剪枝.
对于树中的每个结点维护一个equal, 表示当前结点的子节点是否相等. (若相等就等于子节点的值,否则为-1).
当更新到某区间时,若区间内的值都相同,则只更新到这里即可,下面的结点利用pushdown来更新.

赛后数据被加强了,上述思路在HDU上已经AC不了了. sad....


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n;

LL num[maxn];

struct Tree

{

int left,right;

LL lazy,sum,equl;

}tree[maxn<<2];

void build(int i,int left,int right)

{

tree[i].left=left;

tree[i].right=right;

tree[i].lazy=0;

if(left==right){
tree[i].sum = num[left];
tree[i].equl = num[left];
return ;
} int mid=mid(left,right); build(i<<1,left,mid);
build(i<<1|1,mid+1,right); tree[i].sum = tree[i<<1].sum + tree[i<<1|1].sum;
tree[i].equl = tree[i<<1].equl==tree[i<<1|1].equl ? tree[i<<1].equl : -1;

}

void pushdown(int i)

{

if(tree[i].equl != -1) {

tree[i<<1].equl = tree[i].equl;

tree[i<<1|1].equl = tree[i].equl;

tree[i<<1].sum = (tree[i<<1].right-tree[i<<1].left+1)tree[i].equl;

tree[i<<1|1].sum = (tree[i<<1|1].right-tree[i<<1|1].left+1)
tree[i].equl;

tree[i].lazy = 0;

tree[i<<1].lazy = 0;

tree[i<<1|1].lazy = 0;

}

if(tree[i].lazy) {

tree[i<<1].lazy += tree[i].lazy;

tree[i<<1|1].lazy += tree[i].lazy;

tree[i<<1].sum += (tree[i<<1].right-tree[i<<1].left+1)tree[i].lazy;

tree[i<<1|1].sum += (tree[i<<1|1].right-tree[i<<1|1].left+1)
tree[i].lazy;

if(tree[i<<1].equl != -1) {

tree[i<<1].equl += tree[i].lazy;

tree[i<<1].lazy = 0;

}

if(tree[i<<1|1].equl != -1) {

tree[i<<1|1].equl += tree[i].lazy;

tree[i<<1|1].lazy = 0;

}

tree[i].lazy = 0;

}

}

void update(int i,int left,int right,LL d)

{

if(tree[i].leftleft&&tree[i].rightright)

{

tree[i].sum += (right-left+1)*d;

if(tree[i].equl == -1) tree[i].lazy += d;

else tree[i].equl += d;

return ;

}

pushdown(i);

int mid=mid(tree[i].left,tree[i].right);

if(right<=mid) update(i<<1,left,right,d);
else if(left>mid) update(i<<1|1,left,right,d);
else {
update(i<<1,left,mid,d);
update(i<<1|1,mid+1,right,d);
} tree[i].sum = tree[i<<1].sum + tree[i<<1|1].sum;
tree[i].equl = tree[i<<1].equl==tree[i<<1|1].equl ? tree[i<<1].equl : -1;

}

void update_sqrt(int i,int left,int right)

{

if(tree[i].leftleft&&tree[i].rightright && tree[i].equl!=-1)

{

tree[i].equl = (LL)sqrt(tree[i].equl);

tree[i].sum = tree[i].equl * (tree[i].right-tree[i].left+1);

tree[i].lazy = 0;

return ;

}

pushdown(i);

int mid=mid(tree[i].left,tree[i].right);

if(right<=mid) update_sqrt(i<<1,left,right);
else if(left>mid) update_sqrt(i<<1|1,left,right);
else {
update_sqrt(i<<1,left,mid);
update_sqrt(i<<1|1,mid+1,right);
} tree[i].sum = tree[i<<1].sum + tree[i<<1|1].sum;
tree[i].equl = tree[i<<1].equl==tree[i<<1|1].equl ? tree[i<<1].equl : -1;

}

LL query(int i,int left,int right)

{

if(tree[i].leftleft&&tree[i].rightright)

return tree[i].sum;

pushdown(i);

int mid=mid(tree[i].left,tree[i].right);

if(right<=mid) return query(i<<1,left,right);
else if(left>mid) return query(i<<1|1,left,right);
else return query(i<<1,left,mid)+query(i<<1|1,mid+1,right);

}

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
while(t--)
{
int m;
scanf("%d %d", &n,&m);
for(int i=1; i<=n; i++)
scanf("%lld", &num[i]);
build(1, 1, n); while(m--) {
int op, l, r;
scanf("%d %d %d", &op,&l,&r);
if(op == 1) {
LL x; scanf("%lld", &x);
update(1, l, r, x);
}
else if(op == 2) {
update_sqrt(1, l, r);
}
else if(op == 3) {
printf("%lld\n", query(1, l, r));
}
}
} return 0;

}

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 (线段树+剪枝优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828 给你n个数,三种操作.操作1是将l到r之间的数都加上x:操作2是将l到r之间的数都开方:操作3是 ...

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

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

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

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

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

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

  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. class_create()

    #define class_create(owner, name)               \({                                              \   ...

  2. R语言日期时间函数

    Sys.Date( ) returns today's date. date() returns the current date and time.# print today's datetoday ...

  3. 1320. Graph Decomposition

    1320 简单并查集 #include <iostream> #include<cstdio> #include<cstring> #include<algo ...

  4. POJ 2947 Widget Factory (高斯消元 判多解 无解 和解集 模7情况)

    题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是 ...

  5. ViewPager介绍和使用说明

    1   ViewPager实现的功能 和实际运行的效果图示意 ViewPager类提供了多界面切换的新效果.新效果有如下特征: [1] 当前显示一组界面中的其中一个界面. [2] 当用户通过左右滑动界 ...

  6. html之marquee详解[转]

    该标签不是HTML3.2的一部分,并且只支持MSIE3以后内核,所以如果你使用非IE内核浏览器(如:Netscape)可能无法看到下面一些很有意思的效果该标签是个容器标签语法: <marquee ...

  7. python模拟http请求

    下文主要讲述如何利用python自带的库模拟http请求,为以后利用python做API测试做准备. 只讲述模拟http的过程,具体到自己用的时候,要以自己的应用为准做出适当的调整. #!coding ...

  8. 纯CSS3带动画效果导航菜单

    随着互联网的发展,网页能表现的东西越来越多.由最开始单纯的文字和链接构成的网页,到后来的表格布局,再到div+css模式,现在发展到了html+css3.网页能表达的东西越来越多,css3兴起已经很多 ...

  9. HDU 5296 Annoying problem (LCA,变形)

    题意: 给一棵n个节点的树,再给q个操作,初始集合S为空,每个操作要在一个集合S中删除或增加某些点,输出每次操作后:要使得集合中任意两点互可达所耗最小需要多少权值.(记住只能利用原来给的树边.给的树边 ...

  10. Struts2中date标签乱码问题解决

    1.出现的问题如下图 八月份以前没有问题,但从九月份开始就会出现乱码问题 2.开始解决 (1)在使用标签的JSP中加入: <%@taglib prefix="s" uri=& ...