【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 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 moperations. 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.

题目大意:

给一个序列
支持3种操作
1 u v 对于所有i u<=i<=v,输出a[i]的和
2 u v t 对于所有i u<=i<=v a[i]=a[i]%t
3 u v 表示a[u]=v(将v赋值给a[u])
n,q<=1e5 a[i],t,v<=1e9

解题报告:

    这题需要维护最大值的,不能直接判断是否sum=0了就返回。因为我操作可能给的模数很大,如果我给1e5次这个大模数操作,那就n^2logn了,肯定炸,但是用判断最大值的的方法就可以取消一大部分操作使复杂度降回来。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
struct TREE {int l,r;ll sum;ll maxx;
} tree[MAX<<2];
ll a[MAX];
int n,m;
void pushup(int cur) {tree[cur].sum = tree[cur*2].sum + tree[cur*2+1].sum;tree[cur].maxx = max(tree[cur*2].maxx,tree[cur*2+1].maxx);
}
void build(int l,int r,int cur) {tree[cur].l = l,tree[cur].r = r;if(l == r) {tree[cur].sum = tree[cur].maxx = a[r];return ;}int m = (l+r)>>1;build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur);
}
void update(int pl,int pr,ll val,int cur) {if(tree[cur].maxx < val) return ;if(tree[cur].l == tree[cur].r) {tree[cur].sum %=val;tree[cur].maxx %= val;return ;}if(pl <= tree[cur*2].r) update(pl,pr,val,cur*2);if(pr >= tree[cur*2+1].l) update(pl,pr,val,cur*2+1);pushup(cur);
}
void update2(int tar,ll val,int cur) {if(tree[cur].l == tree[cur].r) {tree[cur].sum = tree[cur].maxx = val;return ;}if(tar <= tree[cur*2].r) update2(tar,val,cur*2);if(tar >= tree[cur*2+1].l) update2(tar,val,cur*2+1);pushup(cur);
}
ll qSum(int pl,int pr,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) return tree[cur].sum;ll res = 0;if(pl <= tree[cur*2].r) res += qSum(pl,pr,cur*2);if(pr >= tree[cur*2+1].l) res += qSum(pl,pr,cur*2+1); return res;
} 
int main()
{cin>>n>>m;for(int i = 1; i<=n; i++) cin>>a[i];build(1,n,1);while(m--) {int op,u,v;ll t;scanf("%d",&op);if(op == 1) {scanf("%d%d",&u,&v);printf("%lld\n",qSum(u,v,1));}if(op == 2) {scanf("%d%d%lld",&u,&v,&t);if(u>v) swap(u,v);update(u,v,t,1);}if(op == 3) {scanf("%d%lld",&u,&t);update2(u,t,1);}}return 0 ;
}
/*
16:25 - 16:38
*/

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440533.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 自定义xml_6.1 如何在spring中自定义xml标签

dubbo自定义了很多xml标签&#xff0c;例如&#xff0c;那么这些自定义标签是怎么与spring结合起来的呢&#xff1f;我们先看一个简单的例子。一 编写模型类1 packagecom.hulk.testdubbo.model;23 public classHero {4 privateString name;5 private intage;67 publicString ge…

java 模块设计模式_Java9模块化学习笔记二之模块设计模式

模块设计的原则:1、防止出现编译时循环依赖(主要是编译器不支持)&#xff0c;但运行时是允许循环依赖的&#xff0c;比如GUI应用2、明确模块的边界几种模块设计:API模块&#xff0c;聚合模块(比如java.base)可选依赖两种方式:1、可选的编译时依赖(类似于maven的provided scope)…

java 手写签名_Android 自定义View手写签名并保存图片

GIF压缩有问题&#xff0c;运行很顺滑&#xff01;&#xff01;&#xff01;1.自定义View——支持设置画笔颜色&#xff0c;画笔宽度&#xff0c;画板颜色&#xff0c;清除画板&#xff0c;检查是否有签名&#xff0c;保存画板图片(复制粘贴可直接使用)/*** Created by YyyyQ o…

【2019第十届蓝桥杯省赛C/C++B组题解】(非官方题解)

A。 数数题。 答案&#xff1a;490 B。 26进制模拟。 答案&#xff1a;BYQ C。 类似fib数列求值&#xff0c;递推一下就好。 答案&#xff1a;4659 D。 注意两个坑点&#xff0c;一个是正整数&#xff0c;所以枚举要从1开始。第二个坑点是互不相同的&#xff0c;为了达到这…

java对象模型 指令_深入理解多线程(二)—— Java的对象模型

上一篇文章中简单介绍过synchronized关键字的方式&#xff0c;其中&#xff0c;同步代码块使用monitorenter和monitorexit两个指令实现&#xff0c;同步方法使用ACC_SYNCHRONIZED标记符实现。后面几篇文章会从JVM源码的角度更加深入&#xff0c;层层剥开synchronized的面纱。在…

java 学生课程成绩_Java课设--学生成绩管理系统一

写在前面这个项目是Java课程的课设&#xff0c;一共花了5天的时间去完成它&#xff0c;在这期间感谢一些博主的帮助&#xff0c;让我了解到了一些新的技术知识&#xff0c;所以打算写这一系列博客来介绍一整个课设项目&#xff0c;也为了帮助之后的人&#xff0c;如有错误&…

java调用cplex案例_【CPLEX教程03】java调用cplex求解一个TSP问题模型

前面我们已经搭建好cplex的java环境了&#xff0c;相信大家已经跃跃欲试&#xff0c;想动手写几个模型了。今天就来拿一个TSP的问题模型来给大家演示一下吧~CPLEX系列教程可以关注我们的公众号哦&#xff01;获取更多精彩消息&#xff01;01 TSP寤烘ā关于TSP建模&#xff0c;就…

java 自动启动监听_Spring Boot 启动事件和监听器,太强大了!

大家都知道&#xff0c;在 Spring 框架中事件和监听无处不在&#xff0c;打通了 Spring 框架的任督二脉&#xff0c;事件和监听也是 Spring 框架必学的核心知识之一。一般来说&#xff0c;我们很少会使用到应用程序事件&#xff0c;但我们也不要忘了它们的存在&#xff0c;比如…

java day_Java_Day7(上)

Java learning_Day7(上)内容常用类枚举类型常用类String 类java.lang.String 类代表不可变的字符序列。String 类的常见构造方法&#xff1a;String(String original)创建一个 String 对象为 original 的拷贝。String(char[] value)用一个字符数组创建一个 String 对象。String…

java关键字 valotile_Java内存模型-jsr133规范介绍,java中volatile关键字的含义

最近在看《深入理解Java虚拟机&#xff1a;JVM高级特性与最佳实践》讲到了线程相关的细节知识&#xff0c;里面讲述了关于java内存模型&#xff0c;也就是jsr 133定义的规范。系统的看了jsr 133规范的前面几个章节的内容&#xff0c;觉得受益匪浅。废话不说&#xff0c;简要的介…

Java重载和重写6_深入理解Java中的重写和重载

深入理解Java中的重写和重载重载(Overloading)和重写(Overriding)是Java中两个比较重要的概念。但是对于新手来说也比较容易混淆。本文通过两个简单的例子说明了他们之间的区别。定义重载简单说&#xff0c;就是函数或者方法有同样的名称&#xff0c;但是参数列表不相同的情形&…

python 协程池gevent.pool_进程池\线程池,协程,gevent

目录1. 进程池与线程池2. 协程3. gevent4. 单线程下实现并发的套接字通信首先写一个基于多线程的套接字服务端:from socket import *from threading import Threaddef comunicate(conn):while True: # 通信循环try:data conn.recv(1024)if len(data) 0: breakconn.send(data.…

java poi 3.13_Java 读取Excl文件 (poi-3.13)

最近做项目遇到了读取Excel数据到数据库做数据的初始化。于是找一个。发现(poi-3.13)可以解决问题。可以解析两种格式(xlsx和xls)以下是实现的步骤1.下载poi3.13包&#xff0c;地址(http://poi.apache.org/download.html#POI-3.13)2.学习APi。接下来是还是demo来说明问题吧&…

【CodeChef - CLIQUED 】Bear and Clique Distances(建图,缩点技巧,思维)

题干&#xff1a; 解题报告&#xff1a; 主要就是在于怎么处理那个前K个点&#xff1a;组成一个团。换句话说&#xff0c;缩成一个点。先直接当成每个点多了k条边来处理&#xff0c;T了。想想也是啊&#xff0c;要是K1e5&#xff0c;那就是1e10条边了。。刚开始尝试了半天缩点&…

【HDU - 5649】DZY Loves Sorting(线段树,区间更新区间查询,思维,01缩数变换,线段树分割)

题干&#xff1a; DZY has a sequence a[1..n]a[1..n]. It is a permutation of integers 1∼n1∼n. Now he wants to perform two types of operations: 0lr0lr: Sort a[l..r]a[l..r] in increasing order. 1lr1lr: Sort a[l..r]a[l..r] in decreasing order. After doin…

php错误403_phpstudy访问文件报错403/Forbidden解决办法

使用phpstudy访问WWW目录下的文件时&#xff0c;浏览器提示Forbidden错误&#xff0c;没有访问权限。我在网上搜索了喝多资料以及本人亲自尝试过后&#xff0c;总结了一下两种方法。方法一&#xff1a;打开phpStudy&#xff0c;点击按键“其他选项菜单”>找到phpStudy配置&g…

【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp)

题干&#xff1a; Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelfs dimensions to be as small as possible. The thickness of the i-th book is ti and its pages width is equal to wi. The thickness of each book is eith…

mac php开发集成环境,MAC OS X下php集成开发环境mamp

之前苦于mac上搭建本地服务器之艰辛&#xff0c;找寻好久都没找到一款类似windows上集成的本地服务器环境&#xff0c;诸如phpstudy&#xff0c;xampp,appserv,虽说xampp也有mac版&#xff0c;但不知为何不是Apache启动不了&#xff0c;这里小编为大家分享了MAC OS X 下php集成…

php获取手机目录,php如何获取手机型号

手机App中判断平台&#xff0c;可以根据$_SERVER[HTTP_USER_AGENT]中的内容来判断浏览器类型或手机平台。(推荐学习&#xff1a;PHP编程从入门到精通)iPhone UA&#xff1a;Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, l…

【CodeForces - 920E】Connected Components? (dsu,补图连通块,STLset+map,bfs 或bitset)

题干&#xff1a; You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices…