1. 程式人生 > >438D The Child and Sequence【線段樹】

438D The Child and Sequence【線段樹】

Time limit 4000 ms Memory limit 262144 kB

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.

題目分析

此題關鍵在於取膜的一個性質 一個數xx被取膜到0的最多隻要logxlogx 顯然為了讓每次xx被取膜後的值都儘量大,每一次取膜的值都為x/2x/2 相當於每次除以2,所以最多取logxlogx次膜即可到0

於是我們可以用線段樹維護區間和的同時在維護一個區間最大值 每次區間修改進入左右區間前先檢查其區間最大值是否小於取膜值,若是則不進入修改這個區間 對於其他的直接修改到最低端

#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;

lt read()
{
    lt f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=100010;
int n,m;
lt a[maxn];
lt sum[maxn<<2],mx[maxn<<2];

void pushup(int p)
{
	sum[p]=sum[p<<1]+sum[p<<1|1];
	mx[p]=max(mx[p<<1],mx[p<<1|1]); 
}

void build(int s,int t,int p)
{
	if(s==t){ sum[p]=mx[p]=a[s]; return;}
	int mid=s+t>>1;
	build(s,mid,p<<1);build(mid+1,t,p<<1|1);
	pushup(p);
}

void update(int ll,int rr,int s,int t,int p,lt x)
{
	if(s==t){ sum[p]%=x; mx[p]%=x; return;}
	int mid=s+t>>1;
	if(ll<=mid&&mx[p<<1]>=x) update(ll,rr,s,mid,p<<1,x);
	if(rr>mid&&mx[p<<1|1]>=x) update(ll,rr,mid+1,t,p<<1|1,x);
	pushup(p);
}

lt qsum(int ll,int rr,int s,int t,int p)
{
	if(ll<=s&&t<=rr) return sum[p];
	int mid=s+t>>1;lt ans=0;
	if(ll<=mid) ans+=qsum(ll,rr,s,mid,p<<1);
	if(rr>mid) ans+=qsum(ll,rr,mid+1,t,p<<1|1);
	return ans;
}

void change(int u,int s,int t,int p,lt x)
{
	if(s==t){ sum[p]=mx[p]=x; return;}
	int mid=s+t>>1;
	if(u<=mid) change(u,s,mid,p<<1,x);
	else change(u,mid+1,t,p<<1|1,x);
	pushup(p);
}

int main()
{
    n=read();m=read();
    for(int i=1;i<=n;++i) a[i]=read();
    build(1,n,1);
    
    while(m--)
    {
    	lt opt=read(),ll=read(),rr=read();
    	if(opt==1) printf("%lld\n",qsum(ll,rr,1,n,1));
    	else if(opt==2) update(ll,rr,1,n,1,read());
    	else if(opt==3) change(ll,1,n,1,rr);
	}
	return 0;
}