Rikka with Phi 线段树
There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:
1. "C A B C" Color the board from segment A to segment B with color C.
2. "P A B" Output the number of different colors painted between segment A and segment B (including).
In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.
Input
Output
Sample Input
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAXN = 1e7 + ;
const int N = 3e5 + ;
LL euler[MAXN];
void geteuler()
{
memset(euler, , sizeof(euler));
euler[] = ;
for (LL i = ; i < MAXN; i++)
{
if (!euler[i])
for (LL j = i; j < MAXN; j += i)
{
if (!euler[j]) euler[j] = j;
euler[j] = euler[j] / i * (i - );
}
}
}
struct node
{
int l, r;
LL sum, laz;
}T[N * + ];
LL a[N];
void pushup(int p)
{
T[p].sum = T[p * ].sum + T[p * + ].sum;
if (T[p * ].laz == T[p * + ].laz)
T[p].laz = T[p * ].laz;
else
T[p].laz = ;
}
void pushdown(int p)
{
if (T[p].laz)
{
T[p * ].laz = T[p * + ].laz = T[p].laz;
T[p * ].sum = T[p].laz * (T[p * ].r - T[p * ].l + );
T[p * + ].sum = T[p].laz * (T[p * + ].r - T[p * + ].l + );
}
}
void update1(int x, int l, int r)
{
if (T[x].laz&&T[x].l == l&&T[x].r == r)
{
T[x].laz = euler[T[x].laz];
T[x].sum = T[x].laz * (T[x].r - T[x].l + );
return;
}
pushdown(x);
int mid = (T[x].l + T[x].r) / ;
if (r <= mid)
update1(x * , l, r);
else if(l > mid)
update1(x * + , l , r);
else
{
update1(x * , l, mid);
update1(x * + , mid + , r);
}
pushup(x);
}
void update2(int x, int l, int r, LL val)
{
if (l == T[x].l&&r == T[x].r)
{
T[x].laz = val;
T[x].sum = (T[x].r - T[x].l + )*T[x].laz;
return;
}
pushdown(x);
int mid = (T[x].l + T[x].r) / ;
if (r <= mid)
update2(x * , l, r, val);
else if (l > mid)
update2(x * + , l, r, val);
else
{
update2(x * , l, mid, val);
update2(x * + , mid + , r, val);
}
pushup(x);
} void build(int x, int l, int r)
{
T[x].l = l, T[x].r = r;
T[x].laz = T[x].sum = ;
if (l == r)
{
T[x].laz = T[x].sum = a[l];
return;
}
int mid = (l + r) / ;
build(x * , l, mid);
build(x * + , mid + , r);
pushup(x);
} LL query(int x, int l, int r)
{
if (T[x].l == l&&T[x].r == r)
return T[x].sum;
int mid = (T[x].l + T[x].r) / ;
pushdown(x);
if (r <= mid)
return query(x * , l, r);
else if (l > mid)
return query(x * + , l, r);
else
return query(x * , l, mid) + query(x * + , mid + , r);
}
int t, n, m;
int main()
{
geteuler();
ios::sync_with_stdio();
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
scanf("%lld", &a[i]);
build(, , n);
int op, L, R;
LL tmp;
while (m--)
{
scanf("%d%d%d", &op, &L, &R);
if (op == )
{
update1(, L, R);
}
else if (op == )
{
scanf("%lld", &tmp);
update2(, L, R, tmp);
}
else if (op == )
{
printf("%lld\n", query(, L, R));
}
}
}
}
Count Color
修改节点的值,查询区间总和
这里laz就表示当前区间元素是否相同 pushdown 顺推
pushup
只有当左右两边都是整块而且左右边的颜色相等才能设置laz =
这里多了一个左右都是整块的条件是因为在欧拉的题目中laz>0就表示是整块了 Rikka with Phi
laz有两个含义:laz== 表示当前区间多个元素值不同
laz == x 表示当前区间元素的值都是x
修改节点的值,查询区间总和
pushdown
顺推即可,laz 相同, sum计算一下
pushup
当前sum = 子区间sum之和
当前laz = 子区间laz 相同? 子区间laz,否则为0 分为块状区域统一处理,当处理比当前块更小的块的时候,把之前积累的信息传递下去,递归处理 两个题的区别在于颜色的题目不需要Laz来表示当前值,当前值用color表示即可 PUSHDOWN
更新结点数据
PUSHUP
根据结点更新当前点的数据
Rikka with Phi 线段树的更多相关文章
- HDU5634 Rikka with Phi 线段树
// HDU5634 Rikka with Phi 线段树 // 思路:操作1的时候,判断一下当前区间是不是每个数都相等,在每个数相等的区间上操作.相当于lazy,不必更新到底. #include & ...
- HDU 5634 Rikka with Phi 线段树
题意:bc round 73 div1 D 中文题面 分析:注意到10^7之内的数最多phi O(log(n))次就会变成1, 因此可以考虑把一段相同的不为1的数缩成一个点,用平衡树来维护. 每次求p ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- hdu 5828 Rikka with Sequence 线段树
Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...
- HDU 6089 Rikka with Terrorist (线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6089 题解 这波强行维护搞得我很懵逼... 扫描线,只考虑每个点能走到左上方(不包括正上方,但包括正左 ...
- Rikka with Mista 线段树求交点个数
由于上下线段是不可能有交点的 可以先看左右线段树,按照y递增的顺序,对点进行排序. 升序构造,那么对于从某一点往下的射线,对于L,R进行区间覆盖,线段交点个数就是单点的被覆盖的次数. 降序构造,那么对 ...
- HDU5828 Rikka with Sequence 线段树
分析:这个题和bc round 73应该是差不多的题,当时是zimpha巨出的,那个是取phi,这个是开根 吐槽:赛场上写的时候直接维护数值相同的区间,然后1A,结果赛后糖教一组数据给hack了,仰慕 ...
- HDU 5828 Rikka with Sequence(线段树区间加开根求和)
Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...
- 牛客多校第十场 A Rikka with Lowbit 线段树
链接:https://www.nowcoder.com/acm/contest/148/A来源:牛客网 题目描述 Today, Rikka is going to learn how to use B ...
随机推荐
- WAMP配置虚拟目录
1.启动wamp所有服务,输入localhost或localhost:端口号确保wamp环境正常无误. 2.设置httpd.conf 2.1打开文件:单击wamp在电脑右下角的图标=>wamp= ...
- AJPFX关于this用法和注意事项
this:代表对象.就是所在函数所属对象的引用.哪个对象调用了this所在的函数,this就代表哪个对象,就是哪个对象的引用.开发时在定义功能时,如果该功能内部使用到了调用该功能的对象,这时就用thi ...
- BOW-js浏览器对象模型
- eclipse debug java 源码
当我们需要研究java SE的时候,debug 源码是个不错的选择,可以帮助我们清楚了解java 封装jar包的具体实现. 因为oracle 提供的源码jar包为了节省空间,所以没有将调试信息一起打包 ...
- Objective -C Object initialization 对象初始化
Objective -C Object initialization 对象初始化 1.1 Allocating Objects 分配对象 Allocation is the process by w ...
- servlet 生命周期 与 初始化
一. 生命周期 Servlet 通过调用 init () 方法进行初始化. Servlet 调用 service() 方法来处理客户端的请求. Servlet 通过调用 destroy() 方法终止( ...
- IE浏览器样式表限制
原文链接:http://caibaojian.com/ie-stylesheet.html 在开发头条上发现的IE浏览器样式限制,算是一个IE浏览器的一个bug吧.主要有4点限制:· IE9及以下单个 ...
- (转)淘淘商城系列——SSM框架整合之表现层整合
http://blog.csdn.net/yerenyuan_pku/article/details/72721120 上文我们一起学习了Service层的整合,本文将教大家如何整合表现层. 我们在t ...
- 处理不同jQuery版本的兼容性问题
众所周知,jquery版本很多,而且有些版本的冲突也非常明显,有一些网上流传的很实用的插件是用A版本写的,但是要实现另各功能又必須用B版本.所以实现版本之間的和平相处很重要. 1.这里介绍一个函数,可 ...
- js里的稀疏数组
今天在逛掘金网站的时候,在一篇文章里学到一个新名字,稀疏数组,特此记录一下. 稀疏数组就是包含从0开始的不连续索引的数组.也就是说数组中大部分的内容值都未被使用(或都为零). var arr = ne ...