CF438D The Child and Sequence
外国人的数据结构题真耿直
唯一有难度的操作就是区间取模,然而这个东西可以暴力弄一下,因为一个数$x$被取模不会超过$logn$次。
证明如下(假设$x Mod y$):
如果$y \leq \frac{x}{2}$那么$x$取模之后会小于$\frac{x}{2}$,而如果$y > \frac{x}{2}$时,$x$取模之后一定也会小于$\frac{x}{2}$
然后就暴力一个一个取过去就好了,还有一个算是剪枝的优化,我们可以顺便维护一下区间最大值,如果区间最大值都小于当前的模数的话,那么就直接$return$好了。
仍然不会算时间复杂度。
丢个模板。
Code:
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 1e5 + ; int n, qn;
ll a[N]; template <typename T>
inline void read(T &X) {
X = ;
char ch = ;
T op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline ll max(ll x, ll y) {
return x > y ? x : y;
} namespace SegT {
ll s[N << ], maxn[N << ]; #define lc p << 1
#define rc p << 1 | 1
#define mid ((l + r) >> 1) inline void up(int p) {
if(p) {
s[p] = s[lc] + s[rc];
maxn[p] = max(maxn[lc], maxn[rc]);
}
} void build(int p, int l, int r) {
if(l == r) {
s[p] = maxn[p] = a[l];
return;
} build(lc, l, mid);
build(rc, mid + , r);
up(p);
} void modify(int p, int l, int r, int x, ll v) {
if(x == l && x == r) {
s[p] = maxn[p] = v;
return;
} if(x <= mid) modify(lc, l, mid, x, v);
else modify(rc, mid + , r, x, v);
up(p);
} void doMod(int p, int l, int r, int x, int y, ll v) {
if(maxn[p] < v) return;
if(l == r) {
s[p] %= v, maxn[p] %= v;
return;
} if(x <= mid) doMod(lc, l, mid, x, y, v);
if(y > mid) doMod(rc, mid + , r, x, y, v);
up(p);
} ll query(int p, int l, int r, int x, int y) {
if(x <= l && y >= r) return s[p];
ll res = 0LL;
if(x <= mid) res += query(lc, l, mid, x, y);
if(y > mid) res += query(rc, mid + , r, x, y);
return res;
} } using namespace SegT; int main() {
read(n), read(qn);
for(int i = ; i <= n; i++) read(a[i]); build(, , n);
for(int op, x, y; qn--; ) {
read(op);
if(op == ) read(x), read(y), printf("%lld\n", query(, , n, x, y));
if(op == ) {
read(x), read(y);
ll v; read(v);
doMod(, , n, x, y, v);
}
if(op == ) {
read(x);
ll v; read(v);
modify(, , n, x, v);
}
} return ;
}
CF438D The Child and Sequence的更多相关文章
- [CF438D]The Child and Sequence【线段树】
题目大意 区间取模,区间求和,单点修改. 分析 其实算是一道蛮简单的水题. 首先线段树非常好解决后两个操作,重点在于如何解决区间取模的操作. 一开始想到的是暴力单点修改,但是复杂度就飙到了\(mnlo ...
- CF438D The Child and Sequence(线段树)
题目链接:CF原网 洛谷 题目大意:维护一个长度为 $n$ 的正整数序列 $a$,支持单点修改,区间取模,区间求和.共 $m$ 个操作. $1\le n,m\le 10^5$.其它数均为非负整数且 ...
- CF438D The Child and Sequence 线段树
给定数列,区间查询和,区间取模,单点修改. n,m小于10^5 ...当区间最值小于模数时,就直接返回就好啦~ #include<cstdio> #include<iostream& ...
- 「CF438D The Child and Sequence」
一道CF线段树好题. 前置芝士 线段树:一个很有用数据结构. 势能分析:用来证明复杂度,其实不会也没什么关系啦. 具体做法 不难发现,对于一个数膜一个大于它的数后,这个数至少减少一半,每个数最多只能被 ...
- 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 ...
随机推荐
- DataGridView绑定数据源的几种方式
使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...
- Python_单元测试工具nose
一.nose的API nose的API地址:http://pythontesting.net/framework/nose/nose-introduction/ 二.安装nose 先用easy_ins ...
- Effective C++ 条款10
令operator=返回一个reference to *this 将operator=返回一个reference是为了什么呢?答案很简单,就是为了实现连锁形式. 什么是连锁形式,如int x,y,z: ...
- FAT-fs (mmcblk0p1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
/******************************************************************************** * FAT-fs (mmcblk0p ...
- WC2019 I 君的商店
交互题 一个 01 序列,告诉你其中 1 有奇数个还是偶数个,每次可以给定两个集合 $A$,$B$,系统会告诉你 $A \leq B$ 或者 $B \leq A$ 求序列 交互次数要求 $5n + O ...
- CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)
Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great ...
- 怪盗基德的滑翔翼(还是最长x序列)
//怪盗基德的滑翔翼 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstri ...
- [推荐]InfoQ上的深入浅出Node.js的系列文章
InfoQ上的深入浅出Node.js的系列文章 详情如下链接:http://www.heiboard.com/?p=2081
- MongoDB优化之一:常见优化方法
常用性能优化方案 创建索引 限定返回结果数 只查询使用到的字段 采用capped collection 采用Server Side Code Execution 使用Hint,强制使用索引 Hint ...
- 2017-09-17 python 学习笔记
Mock 库 介绍: http://blog.csdn.net/chdhust/article/details/50663729 说明mock能做什么. 可以考虑在调试方法时使用 Mock 库