[ 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 ...
随机推荐
- Ubuntu 16.04安装JMeter测试工具
JMeter是Java的测试工具,由Apache开发. 同样,JMeter是跨平台的. 下载: http://jmeter.apache.org/download_jmeter.cgi 安装: 7z ...
- Map根据value排序ASC DESC
原文:http://blog.csdn.net/k21325/article/details/53259180 需求有点刁钻,写关键词组合匹配标题的时候,遇到关键词像这样 XXX XXX 1222 X ...
- AIM Tech R3 div2 E Centroid
思路很明显了,假设是点x,则看它的子树中是否有大于n/2的,如果有,则在该子树中剪去它可以剪的且小于n/2的,接到点x上. 则统计出在以x点为根的子树中,它的各子树可以剪去的且小于n/2的最大子子树. ...
- 改變iTunes備份路徑
*** 在任意分區建立文件夾用來移動iTunes原有備份 *** 例如:move"c:\user\vhd\appdata\roaming\apple computer"全部到&qu ...
- 扩展GCD 中国剩余定理(CRT) 乘法逆元模版
extend_gcd: 已知 a,b (a>=0,b>=0) 求一组解 (x,y) 使得 (x,y)满足 gcd(a,b) = ax+by 以下代码中d = gcd(a,b).顺便求出gc ...
- Cocos2d-x学习笔记(12)(CCControlSwitch开关、CCControlSlider滑动条、CCControlButtonbutton)
CCEditBox.CCControlSwitch.CCControlSlider.CCControlColourPicker.CCControlButton等都是Cocos2d-x 2.x的最新UI ...
- 模式识别之ocr---文字识别Tesseract-OCR 进行文字识别 VS2010
近日做铸件文字识别的项目,需要识别铸件上的字符和数字,找到开源的识别库Tesseract,下面简单记录下怎么使用. 首先在项目主页http://code.google.com/p/tesseract- ...
- [翻译角]Learn From George, Not Pinocchio(ESLPOD)
以下转自www.eslpod.com,翻译为本人添加.其余版权均归原网站所有. ESLPOD是一个英语学习网站,我最初知道这个网站,是因为“奶爸”<把你的英语用起来>一书的推荐. ESLP ...
- 为什么要在css文件里定义 ul{margin:0;padding:0;}这个选择器?
为什么要在css文件里定义 ul{margin:0;padding:0;}这个选择器? ul标签在FF中默认是有padding值的,而在IE中仅仅有margin默认有值.请看下面不同浏览中对paddi ...
- Spring 之AOP 面向切面编程
AOP相关术语: Joinpoint (连接点):所谓连接点是指那些被拦截到的点,在spring中,这些点指的是方法,因为spring 只支持方法类型的连接点. Pointcut(切入点):所谓切入点 ...