Codeforces Round #250 (Div. 1) D. The child and Sequence segment tree interval summation + point modification + interval modulus

Source: Internet
Author: User
Tags modulus

D. the Child and Sequence

At the children's, the child came to picks's house, and messed he 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 being one of the following:

    1. Print Operation l, R. Picks should write down the value of.
    2. Modulo Operation l, R, x. Picks should perform assignment a[i] = a[i] mod x For each i (lir).
    3. Set Operation k, x. Picks should set the value of a[K] to x (in and words perform an assignment c29>a[k] = x).

Can picks to perform the whole sequence of operations?

Input

The first line of input contains the 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'll be is the integers more in the line: l, R (1≤ lr< /c7>≤ n), which correspond the operation 1.
    • If Type = 2, there'll is three integers more in the line: l, R, x ( 1 ≤ lrn; 1 ≤ x ≤109), which correspond the Operation 2.
    • If Type = 3, there'll be is the 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 the answer may exceed the 32-bit integer.

Sample Test (s) input
5 5
1 2 3) 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3
Output
8
5


Test instructions
Operation 1:l,r the interval between L and R and
Action 2:l,r,c the number from L to R is modulo x respectively
Operation 3:l,r talk a[l] change to R;
Exercises
Interval operation of Segment tree
For the go-to-touch: We set a maximum interval segment, to get the modulus if greater than the maximum between the exit
This is the optimization
///1085422276#include <bits/stdc++.h>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#defineMeminf (a) memset (A,127,sizeof (a));#defineINF 1000000007#defineMoD 1000000007inline ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){        if(ch=='-') f=-1; ch=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar (); }returnx*F;}//************************************************Const intmaxn=100000+5;inta[maxn],n,m,q;structss{intL,r; ll V,sum,tag;} TR[MAXN*5];voidBuildintKintSintt) {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 ; }    intMid= (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 (intKintSintt) {    if(tr[k].l==s&&tr[k].r==t) {        returntr[k].sum; }    intMid= (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); returnret;}voidModifyintKintSintt,ll c) {    if(TR[K].V&LT;C)return ; if(tr[k].l==TR[K].R) {Tr[k].sum%=C; TR[K].V%=C; return ; }    intMid= (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);}voidUpdateintKintXintc) {    if(tr[k].l==x&&tr[k].r==x) {Tr[k].sum=C; TR[K].V=C; return; }    intMid= (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);}intMain () {n=read (); m=read ();  for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); }build (1,1, n);intX,y;ll C;  for(intI=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;}
Code

Codeforces Round #250 (Div. 1) D. The child and Sequence segment tree interval summation + point modification + interval modulus

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.