线段树之区间更新The Child and Sequence
作者:互联网
Description
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 .
- Modulo operation l, r, x. Picks should perform assignment a[i] = a[i] mod x for each i(l ≤ i ≤ r).
- Set operation k, x. Picks should set the value of a[k] to x (in other words perform an assignment a[k] = x).
Can you help Picks to perform the whole sequence of operations?
Input
The first line of input contains two integer: n, m(1 ≤ n, m ≤ 105). The second line contains n integers, separated by space: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109) — initial value of array elements.
Each of the next m lines begins with a number type.
- If type = 1, there will be two integers more in the line: l, r (1 ≤ l ≤ r ≤ n), which correspond the operation 1.
- If type = 2, there will be three integers more in the line: l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 109), which correspond the operation 2.
- If type = 3, there will be two integers more in the line: k, x (1 ≤ k ≤ n; 1 ≤ x ≤ 109), which correspond the operation 3.
Output
For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.
Sample Input
Input5 5Output
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3
8Input
5
10 10Output
6 9 6 7 6 1 10 10 9 5
1 3 9
2 7 10 9
2 5 10 8
1 4 7
3 3 7
2 7 9 9
1 2 4
1 6 6
1 5 9
3 1 10
49
15
23
1
9
Hint
Consider the first testcase:
- At first, a = {1, 2, 3, 4, 5}.
- After operation 1, a = {1, 2, 3, 0, 1}.
- After operation 2, a = {1, 2, 5, 0, 1}.
- At operation 3, 2 + 5 + 0 + 1 = 8.
- After operation 4, a = {1, 2, 2, 0, 1}.
- At operation 5, 1 + 2 + 2 = 5.
#include<cstdio> #include<iostream> using namespace std; #define ll long long const int N=1e5+5; ll f[N<<2];//因为是存区间和,所以要开longlong ll lazy[N<<2];//存最值不用longlong int n,m; void pushup(int root){ int rt=root<<1; f[root]=f[rt]+f[rt+1]; lazy[root]=max(lazy[rt],lazy[rt+1]); } void build(int left,int right,int root){ if(left==right){ scanf("%lld",&f[root]); lazy[root]=f[root]; return ; } int mid=(left+right)>>1; int rt=root<<1; build(left,mid,rt); build(mid+1,right,rt+1); pushup(root); } void change(int x,int val,int left,int right,int root){//单点修改 if(left==right){ lazy[root]=f[root]=val; return ; } int mid=(left+right)>>1; int rt=root<<1; if(x<=mid){ change(x,val,left,mid,rt); }else{ change(x,val,mid+1,right,rt+1); } pushup(root); } void update(int x,int uleft,int uright,int left,int right,int root){//区间取模 if(lazy[root]<x) { return ; }//区间最值如果小于x,则不用继续递归 if(left==right){ lazy[root]=(f[root]%=x); return ; } int rt=root<<1; int mid=(left+right)>>1; if(uleft<=mid){ update(x,uleft,uright,left,mid,rt); } if(uright>mid){ update(x,uleft,uright,mid+1,right,rt+1); } pushup(root); } ll query(int qleft,int qright,int left,int right,int root){//区间求和 if(qleft<=left&&qright>=right){ return f[root]; } int mid=(left+right)/2; int rt=root<<1; ll ans=0; if(qleft<=mid){ ans+=query(qleft,qright,left,mid,rt); } if(qright>mid){ ans+=query(qleft,qright,mid+1,right,rt+1); } return ans; } int main(){ scanf("%d%d",&n,&m); build(1,n,1); long long ans=0; int op,l,r,x,val; while(m--){ scanf("%d",&op); if(op==1) { scanf("%d%d",&l,&r); ans=query(l,r,1,n,1); printf("%lld\n",ans); }else if(op==2) { scanf("%d%d%d",&l,&r,&x); update(x,l,r,1,n,1); } else { scanf("%d%d",&x,&val); change(x,val,1,n,1); } } return 0; }
标签:10,Sequence,int,线段,Picks,Child,operation,should,line 来源: https://www.cnblogs.com/killjoyskr/p/16492268.html