Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
2015-10-31 00:42
453 查看
[b]D. The Child and Sequence[/b]
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.
Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1], a[2], ..., a[n]. Then he should perform a sequence of m operations. An operation can be one of the following:
Print operation l, r. Picks should write down the value of
代码
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.
Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1], a[2], ..., a[n]. Then he should perform a sequence of m operations. An operation can be one of the following:
Print operation l, r. Picks should write down the value of
///1085422276 #include<bits/stdc++.h> using namespace std ; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,127,sizeof(a)); #define inf 1000000007 #define mod 1000000007 inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0';ch=getchar(); }return x*f; } //************************************************ const int maxn=100000+5; int a[maxn],n,m,q; struct ss{ int l,r; ll v,sum,tag; }tr[maxn*5]; void build(int k,int s,int t) { tr[k].l=s;tr[k].r=t; tr[k].v=0;tr[k].tag=0;tr[k].sum=0; if(s==t){ tr[k].sum=a[s]; tr[k].v=a[s]; return ; } int mid=(s+t)>>1; build(k<<1,s,mid); build(k<<1|1,mid+1,t); tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum; tr[k].v=max(tr[k<<1].v,tr[k<<1|1].v); } ll ask(int k,int s,int t) { if(tr[k].l==s&&tr[k].r==t){ return tr[k].sum; } int mid=(tr[k].l+tr[k].r)>>1; ll ret=0; if(t<=mid)ret= ask(k<<1,s,t); else if(s>mid)ret= ask(k<<1|1,s,t); else { ret=(ask(k<<1,s,mid)+ask(k<<1|1,mid+1,t)); } tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum; tr[k].v=max(tr[k<<1].v,tr[k<<1|1].v); return ret; } void modify(int k,int s,int t,ll c) { if(tr[k].v<c)return ; if(tr[k].l==tr[k].r){ tr[k].sum%=c; tr[k].v%=c; return ; } int mid=(tr[k].l+tr[k].r)>>1; if(t<=mid)modify(k<<1,s,t,c); else if(s>mid)modify(k<<1|1,s,t,c); else { modify(k<<1,s,mid,c);modify(k<<1|1,mid+1,t,c); } tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum; tr[k].v=max(tr[k<<1].v,tr[k<<1|1].v); } void update(int k,int x,int c) { if(tr[k].l==x&&tr[k].r==x){ tr[k].sum=c; tr[k].v=c; return; } int mid=(tr[k].l+tr[k].r)>>1; if(x<=mid)update(k<<1,x,c); else { update(k<<1|1,x,c); } tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum; tr[k].v=max(tr[k<<1].v,tr[k<<1|1].v); } int main(){ n=read();m=read(); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); }build(1,1,n);int x,y;ll c; for(int i=1;i<=m;i++){ scanf("%d",&q); if(q==1){ scanf("%d%d",&x,&y); printf("%I64d\n",ask(1,x,y)); } else if(q==2){ scanf("%d%d%I64d",&x,&y,&c); modify(1,x,y,c); } else { scanf("%d%d",&x,&y); update(1,x,y); } } return 0; }
代码
相关文章推荐
- IOS开发笔记30-UITableView(1)
- 报错-Unknown class in Interface Builder file
- UIScrollView语法用法样例
- 贪心-- HDU - 4296 Buildings
- UITextField垂直居中对齐
- leetcode笔记:Unique Paths II
- 硬盘分区4 BIOS与UEFI
- UITableViewCell 的单元格重用机制
- UIGestureRecognizer手势
- UITabBarControloler 的简单使用
- UIAlterController 的使用
- UI中自定制cell--3
- UI中自定制cell--2
- QtDesigner:Calculator Builder Example
- UI中自定制cell --1
- storm 开发系列三 Clojue编写程序读取kafka数据并写入到hdfs
- UIPressPhase API
- UINavigationController导航视图器
- IOS开发笔记29-UIScrollView
- UGUI(2)摄像机渲染纹理