Codeforces 438D The Child and Sequence
题意:给定一个n个数的序列,完成以下3个操作:
1.给定区间求和
2.给定区间对x取模
3.单点修改
对一个数取模,这个数至少折半。于是我们记一个最大值max,如果x>max则不做处理。
- #include<stdio.h>
- #include<algorithm>
- using namespace std;
- #define MAXN 1000000+10
- typedef long long LL;
- struct tree{LL mx,sum;}tr[MAXN<<];
- int n,m;
- LL a[MAXN];
- void build(int k,int l,int r){
- if(l==r){
- tr[k].mx=tr[k].sum=a[l];
- return;
- }
- int mid=(l+r)>>;
- build(k<<,l,mid);
- build(k<<|,mid+,r);
- tr[k].mx=max(tr[k<<].mx,tr[k<<|].mx);
- tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
- }
- LL sum(int k,int l,int r,int L,int R){
- if(l>=L&&r<=R)return tr[k].sum;
- int mid=(l+r)>>;
- if(R<=mid)return sum(k<<,l,mid,L,R);
- else if(L>mid)return sum(k<<|,mid+,r,L,R);
- else return sum(k<<,l,mid,L,R)+sum(k<<|,mid+,r,L,R);
- tr[k].mx=max(tr[k<<].mx,tr[k<<|].mx);
- tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
- }
- void update(int k,int l,int r,int t,LL x){
- if(l==t&&r==t){
- tr[k].mx=tr[k].sum=x;
- return;
- }
- int mid=(l+r)>>;
- if(t<=mid)update(k<<,l,mid,t,x);
- if(t>mid)update(k<<|,mid+,r,t,x);
- tr[k].mx=max(tr[k<<].mx,tr[k<<|].mx);
- tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
- }
- void mod(int k,int l,int r,int L,int R,LL x){
- if(tr[k].mx<x)return;
- if(l==r){
- tr[k].mx%=x;
- tr[k].sum%=x;
- return;
- }
- int mid=(l+r)>>;
- if(R<=mid)mod(k<<,l,mid,L,R,x);
- else if(L>mid)mod(k<<|,mid+,r,L,R,x);
- else mod(k<<,l,mid,L,R,x),mod(k<<|,mid+,r,L,R,x);
- tr[k].mx=max(tr[k<<].mx,tr[k<<|].mx);
- tr[k].sum=tr[k<<].sum+tr[k<<|].sum;
- }
- int main(){
- //freopen("mod.in","r",stdin);
- //freopen("mod.out","w",stdout);
- scanf("%d%d",&n,&m);
- for(int i=;i<=n;i++)scanf("%I64d",&a[i]);
- build(,,n);
- while(m--){
- int opt;
- scanf("%d",&opt);
- if(opt==){
- int l,r;
- scanf("%d%d",&l,&r);
- printf("%I64d\n",sum(,,n,l,r));
- }
- if(opt==){
- int l,r;LL x;
- scanf("%d%d%I64d",&l,&r,&x);
- mod(,,n,l,r,x);
- }
- if(opt==){
- int k;LL y;
- scanf("%d%I64d",&k,&y);
- update(,,n,k,y);
- }
- }
- return ;
- }
Codeforces 438D The Child and Sequence的更多相关文章
- 题解——CodeForces 438D The Child and Sequence
题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...
- 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 ...
- 2018.07.23 codeforces 438D. The Child and Sequence(线段树)
传送门 线段树维护区间取模,单点修改,区间求和. 这题老套路了,对一个数来说,每次取模至少让它减少一半,这样每次单点修改对时间复杂度的贡献就是一个log" role="presen ...
- CodeForces 438D The Child and Sequence (线段树 暴力)
传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...
- 438D - The Child and Sequence
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- CF(438D) The Child and Sequence(线段树)
题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...
- Codeforces 438D (今日gg模拟第二题) | 线段树 考察时间复杂度的计算 -_-|||
Codeforces 438D The Child and Sequence 给出一个序列,进行如下三种操作: 区间求和 区间每个数模x 单点修改 如果没有第二个操作的话,就是一棵简单的线段树.那么如 ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- 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 ...
随机推荐
- 哈尔滨理工大学第六届程序设计团队 H-Permutation
/* 数学是硬伤......推了半小时推出来一个错误的公式 */ #include <iostream> #include <stdio.h> #include <alg ...
- 在ASP.NET Core中使用AOP来简化缓存操作
前言 关于缓存的使用,相信大家都是熟悉的不能再熟悉了,简单来说就是下面一句话. 优先从缓存中取数据,缓存中取不到再去数据库中取,取到了在扔进缓存中去. 然后我们就会看到项目中有类似这样的代码了. pu ...
- Android 开发笔记___图像视图__简单截屏
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- HTML学习笔记 w3sCss盒子模型(阴影)(div的一些使用)案例 第十节 (原创) 参考使用表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HTTPCLIENT 模拟登陆
第一步构建忽略https验证的httpclient public static CloseableHttpClient getHttpClient() throws Exception { SSLCo ...
- PyCharm汉化、破解教程
汉化 1.将 C:\Program Files (x86)\JetBrains\PyCharm 2017\lib(路径是你的安装路径)目录下的resources_en.jar文件复制出来之后删除,以备 ...
- 【Arduino】使用LCD1602和DHT11制作温湿度显示器
材料: 1.DHT11 2.LCD1602 3.LCD1602 转接板 4.Arduino UNO 5.Arduino 传感器扩展版 那个Arduino UNO 我当初挑类个便宜的山寨货买,结果发来和 ...
- Excel、Exchange 和 C# (摘要)
Excel.Exchange 和 C#Eric GunnersonMicrosoft Corporation 2003年4月21日 摘要:Eric Gunnerson 将向您介绍如何使用 Outloo ...
- [转载] Hadoop和Hive单机环境搭建
转载自http://blog.csdn.net/yfkiss/article/details/7715476和http://blog.csdn.net/yfkiss/article/details/7 ...
- ANDROID基础ACTIVITY篇之ACTIVITY的生命周期(二)
除了Activity的七大生命周期方法外外,还有两个相当重要的方法需要大家熟记那就是onSavelnstanceState()和onRestoreinstanceState(). 那么什么时候会调用这 ...