[ 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 ...
随机推荐
- Redis持久化方式--RDB和AOF
转载于:https://www.cnblogs.com/xingzc/p/5988080.html Redis提供了RDB持久化和AOF持久化 RDB机制的优势和略施 RDB持久化是指在指定的时间间隔 ...
- mysql导入大型sql文件时注意事项
原文:http://blog.csdn.net/k21325/article/details/70808563 大型sql文件,需要在my.ini(windows)或者my.cnf(Linux)中设置 ...
- 解决Vue打包后背景图片路径错误问题
1.原因解析 当用vue-cli自动构建项目后,有两种运行方法,分别是: npm run dev : 提供一个开发的环境,自动热更新,资源使用绝对路径,所以可以正常看到背景图片. npm run bu ...
- javaweb_page指令
jsp指令: 1.作用:jsp指令是为jsp引擎设计的.他们并不直接产生不论什么课件输出.而是告诉引擎怎样处理jsp页面中的其余部分 2.jsp指令包含:page指令.include指令,taglib ...
- 怎样在OTN站点高速找到asm包并下载 (Oracle RAC)
怎样在OTN站点高速找到asm包并下载 ***********************************************声明******************************* ...
- android学习笔记NO.5
Intent 能够理解为信使 由Intent来协助完毕android各个组件之间的通讯. 感觉基础薄弱就又复习了一遍基础知识,整理了一遍! gen 保存自己主动生成的R资源目录 gen->com ...
- 2016/1/12 第一题 输出 i 出现次数 第二题 用for循环和if条件句去除字符串中空格 第三题不用endwith 实现尾端字符查询
import java.util.Scanner; public class Number { private static Object i; /* *第一题 mingrikejijavabu中字符 ...
- b-is-in- (1267, "Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='") SELECT id FROM qqzoneshuoshuo WHERE words=
db-is-in- (1267, "Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci, ...
- CSP 201703-4 地铁修建【最小生成树+并查集】
问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市 ...
- ZOJ 3956 Course Selection System 背包DP
ZOJ3956 观察数据范围, c的值非常小 只有100 所以c的和也很有限 只有50000 是否可以从这里下手? 对于某一个c的和 我们一定希望h的和最大 才有可能是最终答案. 于是有了类似背包的d ...