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.

Examples

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
Input
10 10
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
Output
49
15
23
1
9

Note

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.

题意:给出数组,有三种操作,分别是区间求和,区间取模 ,单点修改。

思路:一个点被取模,那么其大小减半,所以一个数最多被操作log次,这样的话就不难想到势能线段树。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
int Mx[maxn<<]; ll sum[maxn<<];
void build(int Now,int L,int R)
{
if(L==R){
scanf("%d",&Mx[Now]);
sum[Now]=Mx[Now]; return ;
}
int Mid=(L+R)>>;
build(Now<<,L,Mid); build(Now<<|,Mid+,R);
Mx[Now]=max(Mx[Now<<],Mx[Now<<|]);
sum[Now]=sum[Now<<]+sum[Now<<|];
}
ll query(int Now,int L,int R,int l,int r){
if(l<=L&&r>=R) return sum[Now];
int Mid=(L+R)>>; ll res=;
if(l<=Mid) res+=query(Now<<,L,Mid,l,r);
if(r>Mid) res+=query(Now<<|,Mid+,R,l,r);
return res;
}
void change(int Now,int L,int R,int pos,int val)
{
if(L==R){
Mx[Now]=val; sum[Now]=val; return ;
}
int Mid=(L+R)>>;
if(pos<=Mid) change(Now<<,L,Mid,pos,val);
else change(Now<<|,Mid+,R,pos,val);
Mx[Now]=max(Mx[Now<<],Mx[Now<<|]);
sum[Now]=sum[Now<<]+sum[Now<<|];
}
void modp(int Now,int L,int R,int l,int r,int P)
{
if(Mx[Now]<P) return ;
if(L==R) {
Mx[Now]%=P; sum[Now]=Mx[Now]; return ;
}
int Mid=(L+R)>>;
if(l<=Mid) modp(Now<<,L,Mid,l,r,P);
if(r>Mid) modp(Now<<|,Mid+,R,l,r,P);
Mx[Now]=max(Mx[Now<<],Mx[Now<<|]);
sum[Now]=sum[Now<<]+sum[Now<<|];
}
int main()
{
int N,M,opt,L,R,P;
scanf("%d%d",&N,&M);
build(,,N);
while(M--){
scanf("%d",&opt);
if(opt==) {
scanf("%d%d",&L,&R);
printf("%I64d\n",query(,,N,L,R));
}
else if(opt==){
scanf("%d%d%d",&L,&R,&P);
modp(,,N,L,R,P);
}
else {
scanf("%d%d",&L,&R);
change(,,N,L,R);
}
}
return ;
}

CodeForces - 438D: The Child and Sequence(势能线段树)的更多相关文章

  1. 2018.07.23 codeforces 438D. The Child and Sequence(线段树)

    传送门 线段树维护区间取模,单点修改,区间求和. 这题老套路了,对一个数来说,每次取模至少让它减少一半,这样每次单点修改对时间复杂度的贡献就是一个log" role="presen ...

  2. Codeforces 438D (今日gg模拟第二题) | 线段树 考察时间复杂度的计算 -_-|||

    Codeforces 438D The Child and Sequence 给出一个序列,进行如下三种操作: 区间求和 区间每个数模x 单点修改 如果没有第二个操作的话,就是一棵简单的线段树.那么如 ...

  3. Codeforces 438D 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 ...

  4. CodeForces 438D The Child and Sequence (线段树 暴力)

    传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...

  5. 题解——CodeForces 438D The Child and Sequence

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

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

  7. codeforces 284 C. Cows and Sequence(线段树)

    题目链接:http://codeforces.com/contest/284/problem/C 题意:就是给出3个操作 1)是将前i 个数加x 2)在数组最后添加一个数x 3)删除数组最后的那个数 ...

  8. [CF438D]The Child and Sequence【线段树】

    题目大意 区间取模,区间求和,单点修改. 分析 其实算是一道蛮简单的水题. 首先线段树非常好解决后两个操作,重点在于如何解决区间取模的操作. 一开始想到的是暴力单点修改,但是复杂度就飙到了\(mnlo ...

  9. CF438D The Child and Sequence(线段树)

    题目链接:CF原网  洛谷 题目大意:维护一个长度为 $n$ 的正整数序列 $a$,支持单点修改,区间取模,区间求和.共 $m$ 个操作. $1\le n,m\le 10^5$.其它数均为非负整数且 ...

  10. 【CF438D】The Child and Sequence(线段树)

    点此看题面 大致题意: 给你一个序列,让你支持区间求和.区间取模.单点修改操作. 区间取模 区间求和和单点修改显然都很好维护吧,难的主要是区间取模. 取模标记无法叠加,因此似乎只能暴力搞? 实际上,我 ...

随机推荐

  1. Android开发问题:ActivityNotFoundException: Unable to find explicit activity class

    http://blog.csdn.net/debuglog/article/details/7236013 原因:AndroidManifest.xml未添加对应Activity配置. 解决办法:在A ...

  2. rabbitmq 命令行工具 执行失败.

          修改cookie成一样       资料: http://zhiku8.com/rabbitmq-authentication-failed-rejected-by-the-remote- ...

  3. Yii2 高级模板 多域名管理问题

    现在在网站中有这种情况,比如有一个 http://frontend.com/tv 需要根据判断用户的 User Agent ,如果用户是手机浏览器的话,则跳转到 http://mobile.com/t ...

  4. ZOJ 3960 What Kind of Friends Are You? 【状态标记】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3960 题意 首先给出 一系列名字 需要辨别的名字,然后给出Q个问 ...

  5. PAT 天梯赛 L1-020. 帅到没朋友 【STL】

    题目链接 https://www.patest.cn/contests/gplt/L1-020 思路 对于每个 K >= 2 的朋友圈,里面的所有 ID 都用 MAP 标记一下 对于每个 K = ...

  6. 请求json和xml数据时的方式

    当请求xml数据时,直接通过NSMutableData接收后解析, NSURL *url = [NSURL URLWithString:PATH]; _receiveData = [[NSMutabl ...

  7. Python学习进程(2)Python环境的搭建

        本节主要介绍在windows和Linux平台上如何搭建Python编程环境.     (1)查看Python版本: windows: C:\Users\JMSun>python 'pyt ...

  8. third application :Directions widget

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. 计算机网络概述---OSI参考模型

    应用层:所有能产生网络流量的程序,例如:qq等,txt记事本没有产生流量,所以不属于应用层: 表示层:在传输之前对应用层的数据进行加工或处理,例如:加密.压缩.传视频时二进制,传文档时ASCII码 等 ...

  10. Linux 基础一---操作系统&常用命令

    UNIX是一个计算机操作系统,一个用来协调.管理和控制计算机硬件和软件资源的控制程序. 1.UNIX操作系统的特点:多用户和多任务: a) 多用户表示在同一时刻可以有多个用户同时使用UNIX操作系统而 ...