CF438 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]、....、a[r]对x取模
3)给定k,y,将a[k]修改为y
n, m <= 100000,a[i], x, y <= 109
对于操作(1)(3)非常简单,线段树基本操作
问题是操作(2),显然的是我们不能对区间和取模,这样就很难受
但是我们可以想到,一个数若是比模数小,就不需要取模,而一个数w有效取模次数最多为log(w)
同时单个数被有效取模的一次只会花费O(logn)
因此每次修改至多使复杂度增加O(lognlogw)
这样我们对于区间l, r暴力对每个能取模的数取模即可
最后时间复杂度为O(mlognlogw)
#include<bits/stdc++.h>
#define ll long long
#define uint unsigned int
#define ull unsigned long long
using namespace std;
const int maxn = ;
struct shiki {
ll maxx, sum;
}tree[maxn << ];
int n, m;
ll a[maxn]; inline ll read() {
ll x = , y = ;
char ch = getchar();
while(!isdigit(ch)) {
if(ch == '-') y = -;
ch = getchar();
}
while(isdigit(ch)) {
x = (x << ) + (x << ) + ch - '';
ch = getchar();
}
return x * y;
} inline void maintain(int pos) {
int ls = pos << , rs = pos << | ;
tree[pos].maxx = max(tree[ls].maxx, tree[rs].maxx);
tree[pos].sum = tree[ls].sum + tree[rs].sum;
} void build(int pos, int l, int r) {
if(l == r) {
tree[pos].maxx = tree[pos].sum = a[l];
return;
}
int mid = l + r >> ;
build(pos << , l, mid);
build(pos << | , mid + , r);
maintain(pos);
} void get_mod(int pos, int L, int R, int l, int r, ll mod) {
if(l > R || r < L) return;
if(tree[pos].maxx < mod) return;
if(l == r) {
tree[pos].sum %= mod;
tree[pos].maxx %= mod;
return;
}
int mid = l + r >> ;
get_mod(pos << , L, R, l, mid, mod);
get_mod(pos << | , L, R, mid + , r, mod);
maintain(pos);
} void update(int pos, int aim, int l, int r, ll val) {
if(l == r && l == aim) {
tree[pos].maxx = tree[pos].sum = val;
return;
}
int mid = l + r >> ;
if(aim <= mid) update(pos << , aim, l, mid, val);
else update(pos << | , aim, mid + , r, val);
maintain(pos);
} ll query_sum(int pos, int L, int R, int l, int r) {
if(l > R || r < L) return ;
if(l >= L & r <= R) return tree[pos].sum;
int mid = l + r >> ;
return query_sum(pos << , L, R, l, mid) + query_sum(pos << | , L, R, mid + , r);
} int main() {
n = read(), m = read();
for(int i = ; i <= n; ++i) a[i] = read();
build(, , n);
for(int i = ; i <= m; ++i) {
int opt = read(), x = read(), y = read();
if(opt == ) printf("%I64d\n", query_sum(, x, y, , n));
if(opt == ) {
ll p = read();
get_mod(, x, y, , n, p);
}
if(opt == ) update(, x, , n, y);
}
return ;
}
CF438 The Child and Sequence的更多相关文章
- Codeforce 438D-The Child and Sequence 分类: Brush Mode 2014-10-06 20:20 102人阅读 评论(0) 收藏
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: 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 思路: 因为有区间取模操作所以没法用标记下传: 我们发现,当一个数小于要取模的值时就可以放弃: 凭借这个来减少更新线段树的次数: 来,上代码: # ...
- 438D - 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 At the children's day, the child came to Picks's house, and messed his h ...
- 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 ...
随机推荐
- Final类和Final方法
终止继承 Final类 当关键字final用来修饰类时,其含义是该类不能在派生子类.换句话说,任何其他类都不能继承用final修饰的类,即使该类的访问限制为public类型,也不能被继承:否则,将编译 ...
- selenium利用Excel进行参数化(简单示例)
上篇搭建好环境后,正真开始我的自动化之旅了.... 开始之前特别说明一下testNG的版本,不能直接使用Eclipse直接线上下载的版本,线上版本太高,不能兼容,运行程序会报以下错误,需要自行下载低一 ...
- CF540 B 贪心
坑在B题是常态,弱智的日常. 是找中位数不是平均值. 慌了,乱写了 出了一塌糊涂的ZZ代码 特记一下 /** @Date : 2017-08-27 17:25:11 * @FileName: B.cp ...
- linux系统df和du命令的区别
发现一台用户的电脑,df检查出来的/磁盘空间占用了16G,比用du查看得到的磁盘空间大的多,du查看/下所有程序目录加起来还不到5G.这是什么原因呢? 即便是有隐藏文件,查了也很小啊. 因为df和 ...
- python3学习笔记.2.基础
1.编码 默认编码是 utf-8 # -*- coding: utf-8 -*- 2.注释 单行注释 # 多行注释,用三个单引号或双引号 3.关键字 可在交互窗口查询. >>> i ...
- phpmywind目录结构
phpmywind目录结构了解 admin/ 后台管理目录 admin/editor/ 后台编辑器存放目录 admin/inc/ 后台公用文件引用目录 admin/plugin/ 后台插件存放目录 a ...
- 对Feign的请求url 重写
需求:对当前请求的 url 重新构建 debug feign 的执行可知,重写 LoadBalancerFeignClient 类中的 execute 方法即可控制当前请求的url 代码分析 当引入 ...
- caffe Python API 之BatchNormal
net.bn = caffe.layers.BatchNorm( net.conv1, batch_norm_param=dict( moving_average_fraction=0.90, #滑动 ...
- oracle客户端不需要配置tnsnames.ora文件直接连接服务器数据库
在以前的oracle使用过程中,想要在客户端连接到服务器时,都是在客户端中的tnsnames.ora文件配置如以下内容: adb = (DESCRIPTION = (ADDRESS_LIST = (A ...
- Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常
Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,出现请求异常时,会进入熔断处理,但是不会抛出异常信息. 经过以下配置,可以抛出异常: 将原有ErrorEncoder ...