[ CodeForces 438 D ] The Child and Sequence
\(\\\)
\(Description\)
维护长为 \(N\) 的数列,\(M\)次操作,支持单点修改,区间取模,查询区间和。
- \(N,M\le 10^5\)
\(\\\)
\(Solution\)
线段树单点修改直接改,直接维护区间和就好。
关于取模,显然的优化是,当前节点代表区间最大值如果小于模数就停止递归。
事实上我们只需要这样做,甚至连区间取模的 tag 都不用。
因为一个数变为 \(1\) 至多需要 \(log\) 次取模,所以每个数至多被有效操作 \(log\) 次,然而修改是单点修改,所以并不会对区间暴力取模有太大的影响。
\(\\\)
\(Code\)
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100010
#define gc getchar
#define Rg register
#define mid ((l+r)>>1)
using namespace std;
typedef long long ll;
inline ll rd(){
ll x=0; bool f=0; char c=gc();
while(!isdigit(c)){if(c=='-')f=1;c=gc();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=gc();}
return f?-x:x;
}
ll n,m,a[N];
struct segment{
ll root,ptr;
inline ll newnode(){return ++ptr;}
struct node{ll ls,rs,sum,mx;}c[N<<2];
inline void pushup(ll rt){
c[rt].mx=max(c[c[rt].ls].mx,c[c[rt].rs].mx);
c[rt].sum=c[c[rt].ls].sum+c[c[rt].rs].sum;
}
void build(ll &rt,ll l,ll r){
rt=newnode();
if(l==r){
c[rt].mx=c[rt].sum=a[l];
return;
}
build(c[rt].ls,l,mid);
build(c[rt].rs,mid+1,r);
pushup(rt);
}
void updata1(ll rt,ll l,ll r,ll L,ll R,ll p){
if(r<L||l>R) return;
if(l==r){
c[rt].mx=c[rt].sum=c[rt].sum%p;
return;
}
if(c[rt].mx<p) return;
if(L<=mid) updata1(c[rt].ls,l,mid,L,R,p);
if(R>mid) updata1(c[rt].rs,mid+1,r,L,R,p);
pushup(rt);
}
void updata2(ll rt,ll l,ll r,ll p,ll x){
if(l==r){
c[rt].mx=c[rt].sum=x;
return;
}
if(p<=mid) updata2(c[rt].ls,l,mid,p,x);
else updata2(c[rt].rs,mid+1,r,p,x);
pushup(rt);
}
ll query(ll rt,ll l,ll r,ll L,ll R){
if(r<L||l>R) return 0;
if(l>=L&&r<=R) return c[rt].sum;
ll ans=0;
if(L<=mid) ans+=query(c[rt].ls,l,mid,L,R);
if(R>mid) ans+=query(c[rt].rs,mid+1,r,L,R);
return ans;
}
}tree;
int main(){
n=rd(); m=rd();
for(Rg ll i=1;i<=n;++i) a[i]=rd();
tree.build(tree.root,1,n);
for(Rg ll i=1,op,l,r,x;i<=m;++i){
op=rd();
if(op==1){
l=rd(); r=rd();
printf("%I64d\n",tree.query(tree.root,1,n,l,r));
}
else if(op==2){
l=rd(); r=rd(); x=rd();
tree.updata1(tree.root,1,n,l,r,x);
}
else{
l=rd(); x=rd();
tree.updata2(tree.root,1,n,l,x);
}
}
return 0;
}
[ CodeForces 438 D ] The Child and Sequence的更多相关文章
- 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 ...
- 【codeforces 438D】The Child and Sequence
[原题题面]传送门 [大致题意] 给定一个长度为n的非负整数序列a,你需要支持以下操作: 1:给定l,r,输出a[l]+a[l+1]+…+a[r]. 2:给定l,r,x,将a[l],a[l+1],…, ...
- [题解] Codeforces 438 E The Child and Binary Tree DP,多项式,生成函数
题目 首先令\(f_i\)表示权值和为\(i\)的二叉树数量,\(f_0=1\). 转移为:\(f_k=\sum_{i=0}^n \sum_{j=0}^{k-c_i}f_j f_{k-c_i-j}\) ...
- 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 438D The Child and Sequence
题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...
- 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 ...
- 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 ...
- AC日记——The Child and Sequence codeforces 250D
D - The Child and Sequence 思路: 因为有区间取模操作所以没法用标记下传: 我们发现,当一个数小于要取模的值时就可以放弃: 凭借这个来减少更新线段树的次数: 来,上代码: # ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his h ...
随机推荐
- Jsp+servlet 验证码案例
昨晚在csdn看到一位前辈写一个ajax+servlet+jsp验证.顿时心血来潮,在阅读前辈的代码下我亲手体验一下,做了一个验证码生成工具类.以供大家做个參考. 1:加入VeriyCodeUtils ...
- Android Toast小解
简单介绍:Toast英文含义是吐司,在Android中.它就像烘烤机里做好的吐司弹出来,并持续一小段时间后慢慢消失. Toast也是一个容器,能够包括各种View,并承载着它们显示. Android中 ...
- kvm 安装
一. 虚拟化 是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内运行而互相不影响,从而 ...
- MariaDB 数据库的备份
1> 备份单个数据库 mysqldump -uroot -plichao123 --database students1 > stundents.sql; 2>查看备份文件 3> ...
- Java千百问_03基本的语法(001)_局部变量、类变量、实例变量有什么差别
点击进入_很多其它_Java千百问 局部变量.类变量.实例变量有什么差别 在聊局部变量.类变量.实例变量有什么差别之前,我们须要了解一下Java变量. 1.Java变量是什么 在数学世界中,我们知道有 ...
- 实时人工智能:微软发布Project Brainwave预览版 现场可编程门阵列(Field Programmable Gate Array,简称FPGA) 硬件设计可以迅速演进
https://mp.weixin.qq.com/s/bAPiPURZd-YsbV5PbzwpQQ 编者按:随着各大公司对于数据计算的要求越来越高,实时AI成为了研究者们关注的重点.在美国西雅图举行的 ...
- python 时区
Python中的时区处理 http://tech.glowing.com/cn/dealing-with-timezone-in-python/ Python时区设置方法与pytz查询时区教程_py ...
- 转 Dos和linux格式转换(转)
错误提示: bad interpreter: No such file or directory: /bin/sh 错误分析: 因为操作系统是windows,在windows下编辑的脚本,所以有可能有 ...
- openstack cluster 封装
- 如何在BCGControlBar工程的工具栏里面新增下拉列表控件
通常情况下,工具栏里面都是一些按钮和图片,很少可以看到下拉列表控件,但是在某些应用场合,也需要用到下拉列表控件.今天在这里就简单讲解下如何在工具栏里添加下拉列表控件. 添加的过程也比较简单,在CM ...