codeforces438 D The Child and Sequence
2018-01-19 11:09
579 查看
题目
一句话题意:支持区间取模,单点修改,区间求和的数据结构。
后两个操作都是可以线段树操作的,但是第一个貌似就特别麻烦了。
貌似不可做的样子呀,但是,我们可以发现一个性质
如果x>y那么xmody≤x2的,这样的话,我们可以维护一下区间最大值,如果小于模数,就直接return,不然暴力做就好了。
复杂度可以用势能分析得出是对的,我也不太会,就不多说了
一句话题意:支持区间取模,单点修改,区间求和的数据结构。
后两个操作都是可以线段树操作的,但是第一个貌似就特别麻烦了。
貌似不可做的样子呀,但是,我们可以发现一个性质
如果x>y那么xmody≤x2的,这样的话,我们可以维护一下区间最大值,如果小于模数,就直接return,不然暴力做就好了。
复杂度可以用势能分析得出是对的,我也不太会,就不多说了
//区间取模、单点修改、区间求和 #include<bits/stdc++.h> #define N 100000 using namespace std; int a[N+5],n,m,op,x,y,l,r; struct Tree{ long long sum; int mx,l,r; };Tree T[4*N+5]; inline char nc() { static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; } inline int read() { int x=0,b=1; char c=nc(); for(;!(c<='9'&&c>='0');c=nc())if(c=='-')b=-1; for(;c<='9'&&c>='0';c=nc())x=x*10+c-'0'; return x*b; } inline void pushup(int rt) { T[rt].sum=T[rt*2].sum+T[rt*2+1].sum; T[rt].mx=max(T[rt*2].mx,T[rt*2+1].mx); } inline void build(int rt,int l,int r) { T[rt].l=l,T[rt].r=r; if(l==r) { T[rt].sum=a[l]; T[rt].mx=a[l]; return; } int mid=(l+r)/2; build(rt*2,l,mid),build(rt*2+1,mid+1,r); pushup(rt); } inline void modify(int rt,int pos,int x) { if(T[rt].l==pos&&T[rt].r==pos) { T[rt].sum=x; T[rt].mx=x; return; } int mid=(T[rt].l+T[rt].r)/2; if(pos<=mid)modify(rt*2,pos,x); else modify(rt*2+1,pos,x); pushup(rt); } inline long long query(int rt,int l,int r) { if(T[rt].l==l&&T[rt].r==r)return T[rt].sum; int mid=(T[rt].l+T[rt].r)/2; if(r<=mid)return query(rt*2,l,r); else if(l>mid)return query(rt*2+1,l,r); else return query(rt*2,l,mid)+query(rt*2+1,mid+1,r); } inline void mod(int rt,int l,int r,int p) { if(T[rt].mx<p)return; if(T[rt].l==T[rt].r) { T[rt].sum=T[rt].sum%p; T[rt].mx=T[rt].mx%p; return; } int mid=(T[rt].l+T[rt].r)/2; if(r<=mid)mod(rt*2,l,r,p); else if(l>mid)mod(rt*2+1,l,r,p); else mod(rt*2,l,mid,p),mod(rt*2+1,mid+1,r,p); pushup(rt); } int main() { // freopen("in.txt","r",stdin); n=read(),m=read(); for(int i=1;i<=n;i++)a[i]=read(); build(1,1,n); for(int i=1;i<=m;i++) { op=read(); if(op==1) { l=read(),r=read(); printf("%I64d\n",query(1,l,r)); } if(op==2) { l=read(),r=read(),x=read(); mod(1,l,r,x); } if(op==3) { x=read(),y=read(); modify(1,x,y); } //for(int j=1;j<=n;j++)cout<<query(1,j,j)<<" "; //cout<<endl; } return 0; }
相关文章推荐
- codeforces 438D. The Child and Sequence(线段树)
- 【Codeforces 438 D】The Child and Sequence
- [均摊复杂度线段树]Codeforces 438D. The Child and Sequence
- Codefroces 438D. The Child and Sequence(线段树)
- CodeForces 438 E.The Child and Binary Tree(生成函数+FFT)
- CodeForces 438 C.The Child and Polygon(dp+计算几何)
- Codeforce 438D-The Child and Sequence
- [均摊 平衡树 || 线段树] Codeforces 438D #250 (Div. 1) D. The Child and Sequence
- 438D - The Child and Sequence
- CF(438D) The Child and Sequence(线段树)
- CF(438D) The Child and Sequence(线段树)
- CF438D(The Child and Sequence-线段树mod x)
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
- Codeforces 250 E. The Child and Binary Tree [多项式开根 生成函数]
- 线段树【CF620E】The Child and Sequence
- 有趣的线段树小集合 Codeforces Round #250 (Div. 1) D. The Child and Sequence
- The Child and Sequence
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
- Codeforces-743B-Chloe and the sequence(二进制思维题)
- codeforces 743 B Chloe and the sequence(递归)