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 ...
随机推荐
- svn代码回滚命令 svn up -r
第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn revert [-R] something 其中so ...
- CH5E02 [IOI1999]花店橱窗[暴力dp]
众所周知,这个人太菜了,所以她又来切水题了. 显然设计状态表示第$i$朵花放第$j$瓶中的最大价值.然后瞎转移一波是n三方的,加个前缀max变成n方就水过去了. 当然这题可以搜索剪枝的. 虐lyd书上 ...
- 微信小程序 request域名配置好之后,还是提示报错配置的域名不在request合法域名中
自己尝试着用nodejs搭个后台服务的时候,用了端口号,然后在小程序中使用的时候,报错说配置的域名不在request合法域名中 明明已经配置好了的啊,看着报错信息.仔细对比了一下两个url请求地址,发 ...
- 服务器上传大小限制 --- 来自 FastAdmin 项目开发的引发的问题 (TODO)
服务器上传大小限制 --- 来自 FastAdmin 项目开发的引发的问题 服务器上传有几个地方修改. FastAdmin 的配置. php.ini 的配置. NGINX 的配置.
- ConcurrentHashMap的扩容机制(jdk1.8)
ConcurrentHashMap相关的文章网上有很多,而关于ConcurrentHashMap扩容机制是很关键的点,尤其是在并发的情况下实现数组的扩容的问题经常会碰到,看到这篇写的具有代表性,详细讲 ...
- HTML(超文本标记语言)
学习地址:https://developer.mozilla.org/zh-CN/docs/Web/Html
- C# 获取计算机的硬件信息
/// <summary> /// 获得CPU编号 /// </summary> /// <returns></returns> public stri ...
- ORA-28547:connection to server failed, probable Oracle Net admin error错误,解决方法
当用navicat连接oralce数据库时报ORA-28547错误时,直接懵逼了,上网查了资料说是navicat自带的oci.dll文件的版本和服务器端的oralce数据库的版本不一致造成的. 修改O ...
- 遍历js的obj中所有属性得key
var obj = $("#jstree_default").jstree("get_checked"); for (var a in obj) { alert ...
- FLASH和EEROM使用【转】
最近在看代码的时候,遇到了一个使用FLASH模拟EEPROM的情况,看到这个我当时是一脸蒙蔽啊,对于一个有时候连FLASH和EEPROM都分不清的人来说,怎么可能读懂用FLASH来模拟EEPROM呢? ...