D. The Child and Sequence
 

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:

  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[imod x for each i (l ≤ i ≤ r).
  3. 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 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 题意:
操作1:l,r 求l到r之间的区间和
操作2:l,r,c 将l到r之间的数分别取模x
操作3:l,r 讲a[l]改成r;
题解:
线段树的区间操作
对于去摸:我们设置一个区间段最大值,要取得模要是大于这个最大则之间退出
这就是优化
///
#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=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//************************************************
const int maxn=+;
int a[maxn],n,m,q;
struct ss{
int l,r;
ll v,sum,tag;
}tr[maxn*];
void build(int k,int s,int t)
{
tr[k].l=s;tr[k].r=t;
tr[k].v=;tr[k].tag=;tr[k].sum=;
if(s==t){
tr[k].sum=a[s];
tr[k].v=a[s];
return ;
}
int mid=(s+t)>>;
build(k<<,s,mid);
build(k<<|,mid+,t);
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
tr[k].v=max(tr[k<<].v,tr[k<<|].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)>>;
ll ret=;
if(t<=mid)ret= ask(k<<,s,t);
else if(s>mid)ret= ask(k<<|,s,t);
else {
ret=(ask(k<<,s,mid)+ask(k<<|,mid+,t));
} tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
tr[k].v=max(tr[k<<].v,tr[k<<|].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)>>;
if(t<=mid)modify(k<<,s,t,c);
else if(s>mid)modify(k<<|,s,t,c);
else {
modify(k<<,s,mid,c);modify(k<<|,mid+,t,c);
}
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
tr[k].v=max(tr[k<<].v,tr[k<<|].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)>>;
if(x<=mid)update(k<<,x,c);
else {
update(k<<|,x,c);
}
tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
tr[k].v=max(tr[k<<].v,tr[k<<|].v);
}
int main(){ n=read();m=read();
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}build(,,n);int x,y;ll c;
for(int i=;i<=m;i++){
scanf("%d",&q);
if(q==){
scanf("%d%d",&x,&y);
printf("%I64d\n",ask(,x,y));
}
else if(q==){
scanf("%d%d%I64d",&x,&y,&c);
modify(,x,y,c);
}
else {
scanf("%d%d",&x,&y);
update(,x,y);
} }
return ;
}

代码

Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模的更多相关文章

  1. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

    D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  2. Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)

    题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...

  3. Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树)

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  4. Codeforces Round #250 (Div. 1) D. The Child and Sequence

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  5. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  6. Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树

    C. Day at the Beach Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599/p ...

  7. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

  8. Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树

    题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  9. Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp

    D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...

随机推荐

  1. HDU_3591_(多重背包+完全背包)

    The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  2. Windows提高_1.3文件操作

    文件操作 不带句柄的文件操作 // 1. 拷贝文件,第三个参数为 FALSE 表示会覆盖 // CopyFile(L"D:\\1.txt", L"E:\\2.txt&qu ...

  3. RocketMQ学习笔记(16)----RocketMQ搭建双主双从(异步复制)集群

    1. 修改RocketMQ默认启动端口 由于只有两台机器,部署双主双从需要四个节点,所以只能修改rocketmq的默认启动端口,从官网下载rocketmq的source文件,解压后使用idea打开,全 ...

  4. #NOIP前数学知识总结

    我好菜啊…… 欧拉函数 欧拉函数φ(n),是小于n且和n互质的正整数(包括1)的个数. 性质: 1.对于质数n: φ(n)=n-1 2..对于n=pk φ(n)=(p-1)*pk-1 3.积性函数的性 ...

  5. 当From窗体中数据变化时,使用代码获取数据库中的数据然后加入combobox中并且从数据库中取得最后的结果

    private void FormLug_Load(object sender, EventArgs e) { FieldListLug.Clear();//字段清除 DI = double.Pars ...

  6. spring思想分析

    摘要: EveryBody in the world should learn how to program a computer...because it teaches you how to th ...

  7. docker 1-->docker machine 转载

    Docker Machine 是 Docker 官方编排(Orchestration)项目之一,负责在多种平台上快速安装 Docker 环境. Docker Machine 是一个工具,它允许你在虚拟 ...

  8. jquery 选中设置的值

    select设置值为xxx选中:如下所示 $("#questionClass").val("xxx");

  9. 重载与重写的区别----https://blog.csdn.net/zhu_apollo/article/details/1852542

    重载 overloading        1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型.重载是一个类中多态性的一种表现.        ...

  10. Tensorflow Eager execution and interface

    Lecture note 4: Eager execution and interface Eager execution Eager execution is (1) a NumPy-like li ...