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 ...
随机推荐
- 数组中累加和为k的最大子数组的长度
package com.hzins.suanfa; import java.util.HashMap; public class demo { /** * 数组中累加和为k的最大子数组的长度 * @p ...
- Linux U盘 启动盘
/****************************************************************************** * Linux U盘 启动盘 * 说明: ...
- PNG24在ie6下的完美解决方法!(DD_belatedPNG)
原网址:http://www.zjgsq.com/1629.html 之前写过一篇<js+css滤镜设置解决PNG24在IE6下显示问题> 解决方法不是很完美,使用起来也比较麻烦. DD_ ...
- POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)
题目链接 扩展CRT模板题,原理及证明见传送门(引用) #include<cstdio> #include<algorithm> using namespace std; ty ...
- ENTRYPOINT 与 CMD
在Dockerfile中 ENTRYPOINT 只有最后一条生效,如果写了10条,前边九条都不生效 ENTRYPOINT 的定义为运行一个Docker容器像运行一个程序一样,就是一个执行的命令 两种写 ...
- 基于spring及zookeeper的dubbo工程搭建
一.生产者搭建 新建一个maven工程,勾选Create a simple project Packaging方式选择jar包的方式. 修改pom.xml文件: <project xmlns=& ...
- angular.run 妙用
**1.浏览器判断**在angular做微信应用的时候,有时候我们也想把相同一份代码运行在非微信的浏览器上,这时候我们可以在angular的run上写点东西实现~例如asw.run函数里执行定义一个$ ...
- vue.js初学(一) vue的入门编程
1:首先引入vue.js 2:点击事件 <div id='clickVue'> <p>{{message}}</p> <button v-on:click=& ...
- 蓝桥杯 基础练习 BASIC-25 回形取数
基础练习 回形取数 时间限制:1.0s 内存限制:512.0MB 问题描述 回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转90度.一开始位于矩阵左上角,方向向下. 输入格式 ...
- HAproxy+Mycat
haproxy+mycat搭建haproxy server 10.0.1.134mycat server 10.0.1.134,10.0.1.135mysql master 10.0.1.134mys ...