[CF438D]The Child and Sequence【线段树】
题目大意
区间取模,区间求和,单点修改。
分析
其实算是一道蛮简单的水题。
首先线段树非常好解决后两个操作,重点在于如何解决区间取模的操作。
一开始想到的是暴力单点修改,但是复杂度就飙到了\(mnlogn\),直接爆炸。
但是重新看到了题目中给出的4s的操作,说明,我们可以优化单点修改的操作。
那么我们顺便维护一下区间的最大值,如果当前的区间的最大值是小于mod数的,那么这个区间内的所有数都是没有必要mod的。
后面随着数据的越来越大,那么就可以剪去不必要的操作。
代码
#include <bits/stdc++.h>
#define ll long long
#define N 100005
using namespace std;
struct segment_tree {
#define lc (nod << 1)
#define rc (nod << 1 | 1)
#define mid ((l + r) >> 1)
struct node {
ll s;
int l, r, mx;
node() {
mx = s = 0;
}
}tr[N << 2];
void pushup(int nod) {
tr[nod].s = tr[lc].s + tr[rc].s;
tr[nod].mx = max(tr[lc].mx, tr[rc].mx);
}
void build(int l, int r, int nod, int *a) {
tr[nod].l = l, tr[nod].r = r;
if (l == r) {
tr[nod].mx = tr[nod].s = a[l];
return;
}
build(l, mid, lc, a);
build(mid + 1, r, rc, a);
pushup(nod);
}
ll query_sec_sum(int nod, int ql, int qr) {
ll res = 0;
int l = tr[nod].l, r = tr[nod].r;
if (ql <= l && r <= qr) return tr[nod].s;
if (ql <= mid) res += query_sec_sum(lc, ql, qr);
if (qr > mid) res += query_sec_sum(rc, ql, qr);
return res;
}
void update_point(int nod, int k, int val) {
int l = tr[nod].l, r = tr[nod].r;
if (l == r) {
tr[nod].mx = tr[nod].s = val;
return;
}
if (k <= mid) update_point(lc, k, val);
else update_point(rc, k, val);
pushup(nod);
}
void update_sec_mod(int nod, int ql, int qr, int p) {
int l = tr[nod].l, r = tr[nod].r;
if (tr[nod].mx < p) return;
if (l == r) {
tr[nod].s %= p;
tr[nod].mx %= p;
return;
}
if (ql <= mid) update_sec_mod(lc, ql, qr, p);
if (qr > mid) update_sec_mod(rc, ql, qr, p);
pushup(nod);
}
}tr;
int a[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++) scanf("%d", &a[i]);
tr.build(1, n, 1, a);
while(m --) {
int opt, x, y, z;
scanf("%d", &opt);
if (opt == 1) {
scanf("%d%d", &x, &y);
printf("%lld\n", tr.query_sec_sum(1, x, y));
}
if (opt == 2) {
scanf("%d%d%d", &x, &y, &z);
tr.update_sec_mod(1, x, y, z);
}
if (opt == 3) {
scanf("%d%d", &x, &z);
tr.update_point(1, x, z);
}
}
return 0;
}
[CF438D]The Child and Sequence【线段树】的更多相关文章
- CF438D The Child and Sequence 线段树
给定数列,区间查询和,区间取模,单点修改. n,m小于10^5 ...当区间最值小于模数时,就直接返回就好啦~ #include<cstdio> #include<iostream& ...
- 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 - 线段树
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...
- 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 ...
- cf250D. The Child and Sequence(线段树 均摊复杂度)
题意 题目链接 单点修改,区间mod,区间和 Sol 如果x > mod ,那么 x % mod < x / 2 证明: 即得易见平凡, 仿照上例显然, 留作习题答案略, 读者自证不难. ...
- CF(438D) The Child and Sequence(线段树)
题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...
- CodeForces 438D The Child and Sequence (线段树 暴力)
传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)
题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
随机推荐
- ES优化
1.内存优化 在bin/elasticsearch.in.sh中进行配置 修改配置项为尽量大的内存: 1 2 ES_MIN_MEM=8g ES_MAX_MEM=8g 两者最好改成一样的,否则容易引发长 ...
- MySQL 使用左连接替换not in
众所周知,左连接和右连接的含义是以哪一张表为准. 左连接就是以左表为准,查出的结果中包含左表所有的记录,如果右表中没有与其对应的记录,那么那一行记录中B表部分的内容就全是NULL. 现在有两个表,一个 ...
- WCF使用相关
1.不显示WCF服务主机 在WCF项目属性中的WCF选项卡总关闭下图的选项 2.在其他项目中承载WCF服务 其他加载的操作一致,需要把WCF的endpoint和behavior节点复制到 启动服务的那 ...
- Windows Docker 安装
win7.win8 .win10等需要利用 docker toolbox 来安装,国内可以使用阿里云的镜像来下载,下载地址:http://mirrors.aliyun.com/docker-toolb ...
- Oracle循环
--无条件循环 declare v_num ; begin loop dbms_output.put_line(v_num); v_num:; ; end loop; end; --有条件循环 dec ...
- WIndows下使用Grafana+InfluxDB打造监控系统
前言 对于一个运维DBA来说,了解数据库的TPS.QPS很有必要(QPS:每秒查询数,即对数据库每秒的DML的操作数:TPS:每秒事物处理,即对数据库每秒DDL操作数),通过了解他们,可以掌握一个实 ...
- Python turtle绘制阴阳太极图代码解析
本文详细分析如何使用Python turtle绘制阴阳太极图,先来分解这个图形,图片中有四种颜色,每条曲线上的箭头表示乌龟移动的方向,首先从中心画一个半圆(红线),以红线所示圆的直径作半径画一个校园, ...
- Canvas & SVG
Canvas & SVG https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-dev ...
- Java ME之Android开发从入门到精通
1. 搭建Android开发环境 方式一:使用ADT插件安装 ADT插件的下载与安装,ADT插件获取网址:http://www.androiddevtools.cn/ 下载好的ADT插件如图所示: 在 ...
- springboot+jpa+mysql+swagger整合
Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency> < ...