codeforces CF438D The Child and Sequence 线段树

$ \Rightarrow $ 戳我进CF原题

D. The Child and Sequence

time limit per test: 4 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

 

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 $ \sum_{i=l}^r a[i] $ .
$ 2. $ Modulo operation $ l, r, x $ . Picks should perform assignment $ a[i] = a[i] \quad mod \quad 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 ≤ 10^5) $ .
The second line contains n integers, separated by space: $ a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 10^9) $ — initial value of array elements.
 
Each of the next $ m $ lines begins with a number $ type (type \in (1,2,3) ) $ .
 

  • 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 ≤ 10^9) $ , which correspond the operation 2.
  • If $ type = 3 $ , there will be two integers more in the line: $ k, x (1 ≤ k ≤ n; 1 ≤ x ≤ 10^9) $ , 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

input1

 5 51 2 3 4 52 3 5 43 3 51 2 52 1 3 31 1 3

output1

 85

input2

 10 106 9 6 7 6 1 10 10 9 51 3 92 7 10 92 5 10 81 4 73 3 72 7 9 91 2 41 6 61 5 93 1 10

output2

 49152319

 

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

题目大意

  • 给出一个序列,进行如下三种操作:

  • 区间求和

  • 区间每个数模 $ x $

  • 单点修改

  • $ n,m \le 100000 $
     

思路

  • 如果没有第二个操作的话,就是一棵简单的线段树。那么如何处理这个第二个操作呢?

  • 对于一个数 $ a $ ,如果模数 $ x>a $ ,则这次取模是没有意义的,直接跳过;
    如果 $ x>a/2 $ 则取模结果小于 $ a/2 $ ;如果 $ x<a/2 $ ,取模结果小于 $ x $,则也小于 $ a/2 $

  • 所以对于一个数,最多只会做 $ log_a $ 次取模操作。这是可以接受的!

  • 对于一个区间,维护最大值,如果模数 $ x> $ 最大值,直接跳过即可。否则继续往下像单点修改一样。
     

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define int long long
#define N 100005
int n,m,sum[N<<2],smx[N<<2];
void build(int o,int l,int r){if(l==r){scanf("%lld",&sum[o]);smx[o]=sum[o];return;}int mid=l+r>>1;build(o<<1,l,mid); build(o<<1|1,mid+1,r);sum[o]=sum[o<<1]+sum[o<<1|1];smx[o]=max(smx[o<<1],smx[o<<1|1]);
}
void updata(int o,int l,int r,int pos,int val){if(l==r){sum[o]=smx[o]=val;return;}int mid=l+r>>1;if(pos<=mid) updata(o<<1,l,mid,pos,val);else updata(o<<1|1,mid+1,r,pos,val);sum[o]=sum[o<<1]+sum[o<<1|1];smx[o]=max(smx[o<<1],smx[o<<1|1]);
}
void modtify(int o,int l,int r,int L,int R,int k){if(smx[o]<k) return;if(l==r){sum[o]%=k;smx[o]=sum[o];return;}int mid=l+r>>1;if(L>mid) modtify(o<<1|1,mid+1,r,L,R,k);else if(R<=mid) modtify(o<<1,l,mid,L,R,k);else{modtify(o<<1,l,mid,L,R,k);modtify(o<<1|1,mid+1,r,L,R,k);}sum[o]=sum[o<<1]+sum[o<<1|1];smx[o]=max(smx[o<<1],smx[o<<1|1]);
}
int query(int o,int l,int r,int L,int R){if(L<=l&&r<=R) return sum[o];int mid=l+r>>1;if(L>mid) return query(o<<1|1,mid+1,r,L,R);else if(R<=mid) return query(o<<1,l,mid,L,R);else return query(o<<1,l,mid,L,R)+query(o<<1|1,mid+1,r,L,R);
}
signed main(){scanf("%lld %lld",&n,&m);build(1,1,n);while(m--){int opt,x,y;scanf("%lld %lld %lld",&opt,&x,&y);if(opt==1) printf("%lld\n",query(1,1,n,x,y));else if(opt==2){int k;scanf("%lld",&k);modtify(1,1,n,x,y,k);} else updata(1,1,n,x,y);}return 0;
}
/*
#         42611024
When      2018-09-07 14:02:33 
Who       PotremZ
Problem   D - The Child and Sequence
Lang      GNU C++11
Verdict   Accepted
Time      826 ms
Memory    6300 KB
*/

转载于:https://www.cnblogs.com/PotremZ/p/CF438D.html

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

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

相关文章

大型网站架构演变

今天我们来谈谈一个网站一般是如何一步步来构建起系统架构的&#xff0c;虽然我们希望网站一开始就能有一个很好的架构&#xff0c;但马克思告诉我们事物是在发展中不断前进的&#xff0c;网站架构也是随着业务的扩大、用户的需求不断完善的&#xff0c;下面是一个网站架构逐步…

linux的磁盘磁头瓷片作用,Linux 磁盘管理

硬盘物理结构以下三张图片都是磁盘的实物图&#xff0c;一个磁盘是由多块堆放的瓷片组成的&#xff0c;所以磁头的结构也是堆叠的&#xff0c;他要对每一块瓷片进行读取&#xff0c;磁头是可以在不同磁道(在瓷片的表现为不同直径的同心圆&#xff0c;磁道间是有间隔的)之间移动…

多层插件开发框架

先来几张效果图&#xff1a; 1.基于DATASNAP构建的中间件&#xff0c;中间件已经经过实际项目的检验&#xff0c;单台中间件可支持几千客户端&#xff0c;中间件可集群 2.中间件支持同时连接ACCESS\SQL SERVER\MYSQL\ORACLE。。。多种数据库系统 3.中间件同时支持TCP/IP,HTTP&a…

unity3d 可视化编程_R编程系列:R中的3D可视化

unity3d 可视化编程In the last blog, we have learned how to create “Dynamic Maps Using ggplot2“. In this article, we will explore more into the 3D visualization in R programming language by using the plot3d package.在上一个博客中&#xff0c;我们学习了如何…

linux无法设置变量,linux – crontab在作业之前无法设置变量

我的crontab看起来像&#xff1a;rootslack13x64:~# crontab -l -u dnd# some variablesSHELL/bin/bashPATH/bin:/usr/bin:/usr/local/bin:/home/dnd/binMAILTOroot# Actual jobs40 20 * * * /home/dnd/cron_jobs/some_job.sh55 23 * * Fri /home/dnd/cron_jobs/other_job.py作…

详谈P(查准率),R(查全率),F1值

怎么来的&#xff1f; 我们平时用的精度accuracy&#xff0c;也就是整体的正确率 acc predict_right_num / predict_num 这个虽然常用&#xff0c;但不能满足所有任务的需求。比如&#xff0c;因为香蕉太多了&#xff0c;也不能拨开人工的一个一个的看它的好坏(我爱吃啊&#…

网站系统分布式架构

写这篇文章之前&#xff0c;需要有些论点和论据&#xff0c;以表明网络系统在极端情况下的情况&#xff0c;先来看看世界上排名靠前的网站。 1、 FaceBook 2、 Google 从这两个站可以看出&#xff0c;当下比较极限的日均访问量在2~3亿&#xff0c;PV值…

linux文件系统学习,linux文件系统之tmpfs学习

关于文件系统&#xff0c;我们在下面的博文中已有做简单的介绍&#xff0c;外链网址已屏蔽本篇博文我们学习的是文件系统中的tmpfs。tmpfs是一种伪文件系统&#xff0c;它是从DRAM中创建出来的&#xff0c;相比于磁盘而言&#xff0c;其具有更高的访问效率。如何创建一个tmpfs&…

python 数据科学 包_什么时候应该使用哪个Python数据科学软件包?

python 数据科学 包Python is the most popular language for data science. Unfortunately, it can be tricky to know which of the many data science libraries to use when. ☹️Python是数据科学中最流行的语言。 不幸的是&#xff0c;要知道何时使用许多数据科学库中的哪…

Go语言开发环境配置

http://blog.csdn.net/hil2000/article/details/41261267/ 一.我为什么要学习go语言 当今已经是移动和云计算时代&#xff0c;Go出现在了工业向云计算转型的时刻&#xff0c;简单、高效、内 置并发原语和现代的标准库让Go语言尤其适合云端软件开发&#xff08;毕竟它就是为此而…

微软研发致胜策略

第一章奠定基础 1&#xff0e;千万不要把程序设计师的时间浪费在改善产品以外的工作上。 2&#xff0e;保护程序设计师不受任何阻碍和干扰。 3&#xff0e;永远记得自己真正的目标&#xff0c;然后让团队用最有将效又最愉快的方法把它完成。 4&#xff0e;理清详细的项目目…

熊猫tv新功能介绍_您应该知道的4种熊猫绘图功能

熊猫tv新功能介绍Pandas is a powerful package for data scientists. There are many reasons we use Pandas, e.g. Data wrangling, Data cleaning, and Data manipulation. Although, there is a method that rarely talks about regarding Pandas package and that is the …

CPP_封装_继承_多态

类的三方法&#xff1a;封装&#xff0c;继承&#xff0c;多态。封装&#xff1a;使用一整套方法去创建一个新的类型&#xff0c;这叫类的封装。继承&#xff1a;从一个现有的类型基础上&#xff0c;稍作改动&#xff0c;得到一个新的类型的方法&#xff0c;叫类的继承。多态&a…

win与linux渊源,微软与Linux从对立走向合作,WSL是如何诞生的

原标题&#xff1a;微软与Linux从对立走向合作&#xff0c;WSL是如何诞生的正文Windows Subsystem for Linux(WSL)的开发&#xff0c;让微软从Linux的对立面走向合作&#xff0c;并且不断加大对开源社区的支持力度。而作为微软历史上的重要转折点&#xff0c;外界对WSL技术在Pr…

文件编辑器 vi

1、关于文本编辑器&#xff1b; 文本编辑器有很多&#xff0c;比如图形模式的gedit、kwrite、OpenOffice ... ... &#xff0c;文本模式下的编辑器有vi、vim&#xff08;vi的增强版本&#xff09;和nano ... ... vi和vim是我们在Linux中最常用的编辑器。我们有必要介绍一下vi&a…

MFC80.DLL复制到程序目录中,也有的说复制到安装目录中

在用VS2005学习C调试程序的时候&#xff0c;按F5键&#xff0c;总提示这个问题&#xff0c; 不晓得什么原因&#xff0c;网上有的说找到MFC80.DLL复制到程序目录中&#xff0c;也有的说复制到安装目录中&#xff0c;可结果很失望&#xff0c;也有的VS2005安装有问题&#xff0…

vs显示堆栈数据分析_什么是“数据分析堆栈”?

vs显示堆栈数据分析A poor craftsman blames his tools. But if all you have is a hammer, everything looks like a nail.一个可怜的工匠责怪他的工具。 但是&#xff0c;如果您只有一把锤子&#xff0c;那么一切看起来都像钉子。 It’s common for web developers or databa…

服务器

服务器主流品牌&#xff1a;华为、浪潮、戴尔、惠普华为服务器&#xff1a;华为FusionServer RH2288 V3 华为FusionServer RH5885 V3 浪潮服务器&#xff1a; 浪潮英信NP3020M4 浪潮英信NF5280M4 戴尔服务器&#xff1a; 戴尔PowerEdge R730 机架式服务器 戴尔PowerEdge R740 机…

树莓派 zero linux,树莓派 zero基本调试

回家之前就从网上购买了一堆设备&#xff0c;回去也不能闲着&#xff0c;可以利用家里相对齐全的准备安装调试。结果人还没回来&#xff0c;东西先到了。购买的核心装备是树莓派zero w&#xff0c;虽然已经知道它比家族大哥树莓派小不少&#xff0c;但拿到手里还是惊奇它的小巧…

error C2440 “static_cast” 无法从“void (__thiscall CPppView )(void)”转换为“LRESULT (__thiscall

error C2440 “static_cast” 无法从“void (__thiscall CPppView )(void)”转换为“LRESULT (__thiscall CWnd )(WPARAM,LPARAM)” 不能转换void (_thiscall CMainFrame::*)(void)to LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)开发平台由VC6.0升级至VS2005&#xff0c;需要…