D. The Child and Sequence

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/438/problem/D

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:

  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 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

Sample Output

8
5

HINT

题意

给你n个数,三个操作

1.输出[l,r]的和

2.将[l,r]中的数,对v取摸

3.把a[x]变成v

题解:

线段树

区间定值和区间和很简单

区间取摸的话,需要维护一个区间最大值,如果这个区间的最大值小于要取摸的数,那么就直接break就好了

代码

#include<iostream>
#include<stdio.h>
using namespace std;
#define maxn 100005
struct node
{
int l,r;
long long mx,sum;
}a[maxn*];
int d[maxn];
void build(int x,int l,int r)
{
a[x].l = l,a[x].r = r;
if(l==r)
{
a[x].mx = a[x].sum = d[l];
return;
}
int mid = (l+r)/;
build(x<<,l,mid);
build(x<<|,mid+,r);
a[x].mx = max(a[x<<].mx , a[x<<|].mx);
a[x].sum = a[x<<|].sum + a[x<<].sum;
}
void change(int x,int pos,long long val)
{
int l = a[x].l,r = a[x].r;
if(l==r)
{
a[x].mx = a[x].sum = val;
return;
}
int mid = (l+r)/;
if(pos<=mid)
change(x<<,pos,val);
else
change(x<<|,pos,val);
a[x].mx = max(a[x<<].mx,a[x<<|].mx);
a[x].sum = a[x<<].sum + a[x<<|].sum;
}
void mod(int x,int l,int r,long long val)
{
int L = a[x].l,R = a[x].r;
if(a[x].mx<val)return;
if(L==R)
{
a[x].sum%=val;
a[x].mx%=val;
return;
}
int mid = (L+R)/;
if(r<=mid)
mod(x<<,l,r,val);
else if(l>mid)
mod(x<<|,l,r,val);
else mod(x<<,l,mid,val),mod(x<<|,mid+,r,val);
a[x].sum = a[x<<].sum + a[x<<|].sum;
a[x].mx = max(a[x<<].mx,a[x<<|].mx);
}
long long get(int x,int l,int r)
{
int L = a[x].l,R = a[x].r;
if(L>=l&&R<=r)
{
return a[x].sum;
}
int mid = (L+R)/;
long long sum1 = ,sum2 = ;
if(r<=mid)
sum1 = get(x<<,l,r);
else if(l>mid)
sum2 = get(x<<|,l,r);
else sum1 = get(x<<,l,mid),sum2 = get(x<<|,mid+,r);
return sum1 + sum2;
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
scanf("%d",&d[i]);
build(,,n);
while(q--)
{
int op;
scanf("%d",&op);
if(op==)
{
int x,y;scanf("%d%d",&x,&y);
printf("%lld\n",get(,x,y));
}
if(op==)
{
int x,y,z;scanf("%d%d%d",&x,&y,&z);
mod(,x,y,z);
}
if(op==)
{
int x,y;scanf("%d%d",&x,&y);
change(,x,y);
}
}
}

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   At the children's day, the child came to Picks's house, and messed his h ...

  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. Delphi DecodeDate和EncodeDate 拆分和聚合时间函数的用法

    SysUtilsprocedure DecodeData(Date: TDateTime; var Year, Month, Day: Word);DecodeDate打断TdateTime成为年月日 ...

  2. [Everyday Mathematics]20150204

    设 $k_0>0$, $\phi:[k_0,\infty)\to[0,\infty)$ 是有界递减函数, 并且 $$\bex \phi(k)\leq \frac{A}{(k-h)^\al}\ph ...

  3. CXF之六 自定义拦截器

    CXF已经内置了一些拦截器,这些拦截器大部分默认添加到拦截器链中,有些拦截器也可以手动添加,如手动添加CXF提供的日志拦截器.也可以自定义拦截器,CXF中实现自定义拦截器很简单,只要继承Abstrac ...

  4. android电池信息简介

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  5. C++实现网格水印之调试笔记(三)—— 初有结果

    错误: error C2338: THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD 这种错误 ...

  6. html 5新特性 --用SVG绘制的微信logo

    一个简单的SVG绘制图片的小案例. 效果图: 代码如下: <style> * { ; ; } body { background-color: #d5d3d4; } .container ...

  7. npm + webpack +react

    踏上征途 在开始之前,你需要把你的 Node.js 和 NPM 都更新到最新的版本.访问 nodejs.org 查看安装详情.我们将会使用 NPM 安装一些工具. 开始使用 Webpack 非常简单, ...

  8. 1.2……初识Android开发

    Android体系结构 Dalvik VM(Android下的java虚拟机)与传统的JVM的区别 传统JVM 基于堆栈的架构 编写.java文件--->编译为.class文件--->打包 ...

  9. bzoj 1391 [Ceoi2008]order(最小割)

    [题意] 有n个有偿工作选做,m个机器,完成一个工作需要若干个工序,完成每个工序需要一个机器,对于一个机器,在不同的工序有不同的租费,但买下来的费用只有一个.问最大获益. [思路] 对于工作和机器建点 ...

  10. c++ 概念及学习/c++ concept&learning(三)

    这一篇继续说说程序设计中的基本语句:控制块 一 if类控制语句 if if else if  , else if ,else if(条件语句){如果条件为真,要做的一些事情}  if(条件语句) {如 ...