HDU 5634 Rikka with Phi 线段树
题意:bc round 73 div1 D 中文题面
分析:注意到10^7之内的数最多phi O(log(n))次就会变成1,
因此可以考虑把一段相同的不为1的数缩成一个点,用平衡树来维护。
每次求phi的时候就在平衡树上取出这个区间然后暴力求phi,如果一段数变成了1,
就在平衡树里面删掉它,最后统计答案的时候只要把区间中被删去的1加回答案即可,
时间复杂度O((n + m)logn)
注:平衡树,写起来麻烦(然后其实我也不会写)
但是题解当中说把一段相同的数缩成一个点,就很好
所以用线段树,节点维护区间和以及(当这个区间元素都相同时)维护这个元素
然后操作2和操作3就是普通的线段树应用,区间更新以及区间求和
然后操作1,由于我维护了一整段相同元素的区间,所以更新时,
只要按照区间更新,区间在更新范围内,且节点所管辖区间的元素全部相同的时候,直接修改节点
区间更新就好了,这样的话,时间复杂度不是很高
由于单个元素的范围是1e7,所以先筛一遍欧拉函数
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e7;
const int maxn=3e5+;
const LL mod = 1e9+;
int phi[N+],n,m,T;
int o[maxn<<],mark[maxn<<];
LL sum[maxn<<];
void pushup(int rt)
{
sum[rt]=sum[rt*]+sum[rt*+];
if(o[rt*]==o[rt*+]&&o[rt*])
o[rt]=o[rt*];
else o[rt]=;
}
void pushdown(int rt,int l,int r)
{
if(mark[rt])
{
int mid=(l+r)>>;
sum[rt*]=1ll*(LL)(mid-l+)*(LL)(mark[rt]);
sum[rt*+]=1ll*(LL)(r-mid)*(LL)(mark[rt]);
o[rt*]=o[rt*+]=mark[rt];
mark[rt*]=mark[rt*+]=mark[rt];
mark[rt]=;
}
}
void build(int rt,int l,int r)
{
if(l==r)
{
scanf("%d",&o[rt]);
sum[rt]=o[rt];
return ;
}
int mid=(l+r)>>;
build(rt*,l,mid);
build(rt*+,mid+,r);
pushup(rt);
}
void op1(int rt,int l,int r,int x,int y)
{
if(x<=l&&r<=y&&o[rt])
{
int tmp=phi[o[rt]];
o[rt]=mark[rt]=tmp;
sum[rt]=1ll*(LL)(r-l+)*(LL)(tmp);
return;
}
pushdown(rt,l,r);
int mid=(l+r)>>;
if(x<=mid)op1(rt*,l,mid,x,y);
if(y>mid)op1(rt*+,mid+,r,x,y);
pushup(rt);
}
int t;
void op2(int rt,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
o[rt]=mark[rt]=t;
sum[rt]=1ll*(LL)(r-l+)*(LL)(t);
return;
}
pushdown(rt,l,r);
int mid=(l+r)>>;
if(x<=mid)op2(rt*,l,mid,x,y);
if(y>mid)op2(rt*+,mid+,r,x,y);
pushup(rt);
}
LL op3(int rt,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
return sum[rt];
pushdown(rt,l,r);
int mid=(l+r)>>;
LL ans=;
if(x<=mid)ans+=op3(rt*,l,mid,x,y);
if(y>mid)ans+=op3(rt*+,mid+,r,x,y);
return ans;
}
int main()
{
phi[]=;
for(int i=; i<=N; ++i)
{
if(!phi[i])
{
for(int j=i; j<=N; j+=i)
{
if(!phi[j])
phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
}
scanf("%d",&T);
while(T--)
{
memset(o,,sizeof(o));
memset(mark,,sizeof(mark));
scanf("%d%d",&n,&m);
build(,,n);
while(m--)
{
int c,l,r;
scanf("%d%d%d",&c,&l,&r);
if(c==)scanf("%d",&t);
if(c==)op1(,,n,l,r);
else if(c==)op2(,,n,l,r);
else printf("%I64d\n",op3(,,n,l,r));
}
}
return ;
}
HDU 5634 Rikka with Phi 线段树的更多相关文章
- HDU5634 Rikka with Phi 线段树
// HDU5634 Rikka with Phi 线段树 // 思路:操作1的时候,判断一下当前区间是不是每个数都相等,在每个数相等的区间上操作.相当于lazy,不必更新到底. #include & ...
- HDU 5634 Rikka with Phi
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5634 ------------------------------------------------ ...
- 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 Phi 线段树
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...
- HDU 5828 Rikka with Sequence (线段树+剪枝优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828 给你n个数,三种操作.操作1是将l到r之间的数都加上x:操作2是将l到r之间的数都开方:操作3是 ...
- HDU 5828 Rikka with Sequence(线段树区间加开根求和)
Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
随机推荐
- 【BZOJ 1202】 [HNOI2005]狡猾的商人
Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...
- Java注解处理器(转)
Java中的注解(Annotation)是一个很神奇的东西,特别现在有很多Android库都是使用注解的方式来实现的.一直想详细了解一下其中的原理.很有幸阅读到一篇详细解释编写注解处理器的文章.本文的 ...
- resin access.log format配置详解
The access log formatting variables follow the Apache variables: %b result content length %D tim ...
- 异常:HRESULT: 0x80070057 (E_INVALIDARG) 的处理
碰到这个异常的原因很偶然: 现象:Solution在ReBuild过程中断电了,来电恢复了,重析编译整个Solution不报错,但在浏览页面时始终无法正常浏览,而在design的视图中,每个aspx的 ...
- React Native Android配置部署踩坑日记
万事开头难 作为一只进入ECMAScript世界不久的菜鸟,已经被React Native的名气惊到了,开源一周数万星勾起了我浓烈的兴趣.新年新气象,来个HellWorld压压惊吧^_^(故意少打个' ...
- uva 567
Floyd 算法 就输入麻烦点 #include <iostream> #include <cstring> #include <cstdlib> #inclu ...
- uva 10608
简单并查集 水水..... #include <cstdio> #include <cstring> #define maxn 30005 int fa[maxn],ans[ ...
- tomcat 禁止某些文件(夹)的访问
tomcat 禁止某些文件(夹)的访问 <!-- 不允许访问的文件以及文件夹 --> <security-constraint> <display-name>Tom ...
- SVN中update to revision与revert to revision的区别
假设我们有许多个版本,版本号分别是1-10 如果我们在7这里选择revert to this version那么7之后的8,9,10的操作都会被消除 如果在7选择revert changes from ...
- 统计 p-value 含义
p-value是一种概率:在原假设为真的前提下,出现该样本或比该样本更极端的结果的概率之和. 例子: 我们假设 H0:出现正面的概率是1/2 扔硬币20次出现了14次正面.该样本的单边p-value计 ...