[题解]codeforces 438d The Child and Sequence
来源:互联网 发布:mac安装exe软件 编辑:程序博客网 时间:2024/04/30 16:30
Description
题目大意:
给出一个长度为 n 的序列
1. 给出 k, x,将
2. 给出 l, r, x,将所有的
3. 给出 l, r,询问
数据范围:
Solution
这道题和Rikka With Phi很像,都是修改操作经过log次之后就不在有效(有效的取模每次至少使数减少一半),所以可以用类似的方法解,就是判断区间内的数是否全部相等。而且这道题是单点修改,用同样的方法可以完成支持区间修改。要注意的是,这道题由于存在单点修改,区间内的数不会越来越趋近于相等,所以我们还要加上特判,才能不会TLE:如果区间内的数都小于模数,那么直接退出。
但是这道题由于是单点修改,所以每次区间范围取模之后想要把数重新变大是比较麻烦的,所以这题其实暴力也可以过去,就是每次取模都递归到线段树的最底层。
代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;template<typename T>inline void read(T &x){ T f=1;char ch=getchar(); for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1; for(x=0;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0'; x*=f;}typedef long long LL;const int maxn=100010;struct Segment_Tree{ #define lc x<<1 #define rc x<<1|1 int L[maxn<<2],R[maxn<<2],mx[maxn<<2],mi[maxn<<2],same[maxn<<2]; LL sum[maxn<<2]; Segment_Tree(){memset(same,-1,sizeof same);} void update(int x){ mx[x]=max(mx[lc],mx[rc]); mi[x]=min(mi[lc],mi[rc]); sum[x]=sum[lc]+sum[rc]; } void Build(int x,int *a,int l,int r){ if((L[x]=l)==(R[x]=r))return sum[x]=mx[x]=mi[x]=a[l],void(); int mid=(l+r)>>1; Build(lc,a,l,mid);Build(rc,a,mid+1,r); update(x); } void pushsame(int x,int val){ same[x]=mx[x]=mi[x]=val; sum[x]=(LL)(R[x]-L[x]+1)*val; } void pushdown(int x){ if(same[x]==-1)return; pushsame(lc,same[x]); pushsame(rc,same[x]); same[x]=-1; } void Change(int x,int pos,int val){ if(L[x]==R[x])return pushsame(x,val),void(); pushdown(x); int mid=(L[x]+R[x])>>1; if(pos<=mid)Change(lc,pos,val); else Change(rc,pos,val); update(x); } void Modify(int x,int l,int r,int val){ if(R[x]<l||L[x]>r||mx[x]<val)return; if(L[x]>=l&&R[x]<=r&&mx[x]==mi[x])return pushsame(x,mx[x]%val),void(); pushdown(x); Modify(lc,l,r,val);Modify(rc,l,r,val); update(x); } LL Query(int x,int l,int r){ if(R[x]<l||L[x]>r)return 0; if(L[x]>=l&&R[x]<=r)return sum[x]; pushdown(x); return Query(lc,l,r)+Query(rc,l,r); }}tree;int n,m,a[maxn];int main(){ read(n);read(m); for(int i=1;i<=n;i++)read(a[i]); tree.Build(1,a,1,n); while(m--){ int opt,l,r,x; read(opt);read(l);read(r); if(opt==1)printf("%lld\n",tree.Query(1,l,r)); else if(opt==2)read(x),tree.Modify(1,l,r,x); else tree.Change(1,l,r); } return 0;}
阅读全文
0 0
- [题解]codeforces 438d The Child and Sequence
- CodeForces 438D The Child and Sequence
- 【Codeforces 438 D】The Child and Sequence
- CODEFORCES 438D The Child and Sequence <二叉树>
- Codeforce 438D-The Child and Sequence
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
- [均摊 平衡树 || 线段树] Codeforces 438D #250 (Div. 1) D. The Child and Sequence
- Codeforces Round #250 Div1D 438D The Child and Sequence 线段树+势能分析
- CodeForces 438D The Child and Sequence 数据结构+点更新(赋值)+区间求和
- codeforces 438D The Child and Sequence(线段树:单点更新+区间取模+区间和)
- [均摊复杂度线段树]Codeforces 438D. The Child and Sequence
- [均摊复杂度线段树] Codeforces #438D. The Child and Sequence
- CF(438D) The Child and Sequence(线段树)
- CF 438D The Child and Sequence [线段树]
- CF 438D The Child and Sequence(线段树取模)
- Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树暴力)
- Codeforces Round #250 D - The Child and Sequence/[TYVJ3838] DQS和序列(by 帝江&Darkfalmes)
- 设计模式学习笔记——装饰器模式
- 分布式消息队列RocketMQ与Kafka架构上的巨大差异之2 -- CommitLog与ConsumeQueue
- 196. Delete Duplicate Emails
- Y
- sprintf函数用法详解
- [题解]codeforces 438d The Child and Sequence
- Oracle 10g安装教程
- 评教管理系统
- Asp.net创建Datatable并赋值
- opencv 之运动物体检测(二)
- 杨氏矩阵查找的Java实现
- 分布式消息队列RocketMQ源码分析之3 -- Consumer负载均衡机制 -- Rebalance
- dubbo和zookeeper使用
- C++ explicit