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.pswp.cn/news/388576.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388576.shtml
英文地址,請注明出處:http://en.pswp.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;需要…