[均摊复杂度线段树]Codeforces 438D. The Child and Sequence
2017-09-25 17:04
483 查看
首先一个数模一个小于它的数,肯定会变成原来的一半
那么用均摊复杂度线段树的套路,记一下区间最大值,区间取模时,如果区间最大值小于模数,那么不会改变数列,直接退出,否则暴力递归。
每个点最多变化log次,复杂度应该就是 O(nlog2n)
那么用均摊复杂度线段树的套路,记一下区间最大值,区间取模时,如果区间最大值小于模数,那么不会改变数列,直接退出,否则暴力递归。
每个点最多变化log次,复杂度应该就是 O(nlog2n)
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int N=100010; int n,m,a ; ll sum[N<<2],mx[N<<2]; inline void Up(int g){ sum[g]=sum[g<<1]+sum[g<<1|1]; mx[g]=max(mx[g<<1],mx[g<<1|1]); } void Build(int g,int l,int r){ if(l==r) return sum[g]=mx[g]=a[l],void(); int mid=l+r>>1; Build(g<<1,l,mid); Build(g<<1|1,mid+1,r); Up(g); } ll Query(int g,int L,int R,int l,int r){ if(l==L && r==R) return sum[g]; int mid=L+R>>1; if(r<=mid) return Query(g<<1,L,mid,l,r); else if(l>mid) return Query(g<<1|1,mid+1,R,l,r); else return Query(g<<1,L,mid,l,mid)+Query(g<<1|1,mid+1,R,mid+1,r); } void Modify(int g,int L,int R,int l,int r,int x){ if(mx[g]<x) return ; if(L==R) return sum[g]%=x,mx[g]%=x,void(); int mid=L+R>>1; if(r<=mid) Modify(g<<1,L,mid,l,r,x); else if(l>mid) Modify(g<<1|1,mid+1,R,l,r,x); else Modify(g<<1,L,mid,l,mid,x),Modify(g<<1|1,mid+1,R,mid+1,r,x); Up(g); } void Changes(int g,int l,int r,int x,int y){ if(l==r) return sum[g]=mx[g]=y,void(); int mid=l+r>>1; if(x<=mid) Changes(g<<1,l,mid,x,y); else Changes(g<<1|1,mid+1,r,x,y); Up(g); } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); Build(1,1,n); for(int i=1;i<=m;i++){ int opt,l,r,x,k; scanf("%d",&opt); if(opt==1){ scanf("%d%d",&l,&r); printf("%lld\n",Query(1,1,n,l,r)); } else if(opt==2){ scanf("%d%d%d",&l,&r,&x); Modify(1,1,n,l,r,x); } else{ scanf("%d%d",&k,&x); Changes(1,1,n,k,x); } //for(int j=1;j<=n;j++) printf("%lld ",Query(1,1,n,j,j)); putchar('\n'); } return 0; }
相关文章推荐
- [均摊 平衡树 || 线段树] Codeforces 438D #250 (Div. 1) D. The Child and Sequence
- codeforces 438D. The Child and Sequence(线段树)
- 【Codeforces 438 D】The Child and Sequence
- CF(438D) The Child and Sequence(线段树)
- CF(438D) The Child and Sequence(线段树)
- Codefroces 438D. The Child and Sequence(线段树)
- codeforces438 D The Child and Sequence
- Codeforce 438D-The Child and Sequence
- CodeForces 438 E.The Child and Binary Tree(生成函数+FFT)
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树
- Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)
- The Child and Sequence(线段树)
- CodeForces 438 C.The Child and Polygon(dp+计算几何)
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
- 线段树【CF620E】The Child and Sequence
- 有趣的线段树小集合 Codeforces Round #250 (Div. 1) D. The Child and Sequence
- 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 线段树 区间求和+点修改+区间取模
- H - The Child and Toy-CodeForces 437C-水题