更好的阅读体验

Portal

Portal1: Codeforces

Portal2: Luogu

Description

You are given circular array \(a_0, a_1, \cdots, a_{n - 1}\). There are two types of operations with it:

  • \(\textrm{inc}(lf, rg, v)\) — this operation increases each element on the segment \([lf, rg]\) (inclusively) by \(v\);

  • \(\textrm{rmq}(lf, rg)\) — this operation returns minimal value on the segment \([lf, rg]\) (inclusively).

Assume segments to be circular, so if \(n = 5\) and \(lf = 3, rg = 1\), it means the index sequence: \(3, 4, 0, 1\).

Write program to process given sequence of operations.

Input

The first line contains integer \(n (1 \le n \le 200000)\). The next line contains initial state of the array: \(a_0, a_1, \cdots, a_{n - 1} ( -10^6 \le ai \le 10^6)\), \(a_i\) are integer. The third line contains integer \(m (0 \le m \le 200000)\), \(m\) — the number of operartons. Next \(m\) lines contain one operation each. If line contains two integer \(lf, rg (0 \le lf, rg \le n - 1)\) it means rmq operation, it contains three integers \(lf, rg, v (0 \le lf, rg \le n - 1; -10^6 \le v \le 10^6)\) — inc operation.

Output

For each rmq operation write result for it. Please, do not use %lld specificator to read or write \(64\)-bit integers in C++. It is preffered to use cout (also you may use %I64d).

Sample Input

4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1

Sample Output

1
0
0

Solution

我们可以用线段树来解决区间RMQ问题,我们在线段树上维护一个最小值与懒标记,这样问题就解决了。

读入的时候我们可以判断后面一个字符是不是空格,可以直接在快速读入里判断,这样就可以判断出一行有三个数还是两个数。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; const int MAXN = 200005;
int n, m, l, r, val, a[MAXN];
bool opt;
namespace Segtree {
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long LL;
const LL Seg_INF = 1e18;
const int Seg_MAXN = 1000005;
struct SMT {
LL Min, tag;
} tree[Seg_MAXN];
inline void build(int rt, int l, int r) {//建立线段树
if (l == r) {
tree[rt].Min = a[l];
return ;
}
int mid = l + r >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
tree[rt].Min = min(tree[ls].Min, tree[rs].Min);
}
inline void update(int rt, int l, int r, int ansl, int ansr, int val) {//线段树修改
if (ansl <= l && r <= ansr) {
tree[rt].tag += val;
return ;
}
int mid = l + r >> 1;
if (ansl <= mid) update(ls, l, mid, ansl, ansr, val);
if (mid < ansr) update(rs, mid + 1, r, ansl, ansr, val);
tree[rt].Min = min(tree[ls].Min + tree[ls].tag, tree[rs].Min + tree[rs].tag);
}
inline LL query(int rt, int l, int r, int ansl, int ansr) {//线段树查询
if (ansl <= l && r <= ansr) return tree[rt].Min + tree[rt].tag;
int mid = l + r >> 1;
LL ret = Seg_INF;
if (ansl <= mid) ret = min(ret, query(ls, l, mid, ansl, ansr));
if (mid < ansr) ret = min(ret, query(rs, mid + 1, r, ansl, ansr));
return ret + tree[rt].tag;
}
} using namespace Segtree; inline int read() {
opt = 0;
char ch = getchar();
int x = 0, f = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
if (ch == ' ') opt = 1;//判断空格
return x * f;
}
int main() {
n = read();
for (int i = 1; i <= n; i++)
a[i] = read();
build(1, 1, n);
m = read();
for (int i = 1; i <= m; i++) {
l = read(); r = read(); l++; r++;
if (!opt) {
if (l <= r) printf("%lld\n", query(1, 1, n, l, r)); else printf("%lld\n", min(query(1, 1, n, l, n), query(1, 1, n, 1, r)));
} else {
val = read();
if (l <= r) update(1, 1, n, l, r, val); else {
update(1, 1, n, l, n, val);
update(1, 1, n, 1, r, val);
}
}
}
return 0;
}

「CF52C」Circular RMQ的更多相关文章

  1. 【CF52C】Circular RMQ(线段树区间加减,区间最值)

    给定一个循环数组a0, a1, a2, …, an-1,现在对他们有两个操作: Inc(le, ri, v):表示区间[le, ri]范围的数值增加v Rmq(le, ri):表示询问区间[le, r ...

  2. 「CF1380G」 Circular Dungeon

    CF1380G Circular Dungeon 看懂样例就能做. 虽然我瞪了 20 分钟 菜是原罪 首先可以将从每一个点出发所能获得的价值相加,再除以 \(n\) 就可以得到价值的期望. 所以问题转 ...

  3. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  4. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  5. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  6. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  7. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  8. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  9. 「2014-3-18」multi-pattern string match using aho-corasick

    我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...

随机推荐

  1. spring boot使用vue+vue-router构建单页面应用

    spring boot http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/ vue https: ...

  2. BUUCTF刷题记录(Web方面)

    WarmUp 首先查看源码,发现有source.php,跟进看看,发现了一堆代码 这个原本是phpmyadmin任意文件包含漏洞,这里面只不过是换汤不换药. 有兴趣的可以看一下之前我做的分析,http ...

  3. kafka-0.10.2.1:Producer生产时无法自动创建Topic

    集群环境: CenterOS 1台 Kafka:0.10.2.1版本. 今天在测试环境下,我们的Kafka集群工作不正常,具体现象为,使用confulentkafka向kafka集群生产消息失败,且并 ...

  4. 解决:Specifying a namespace in include() without providing an app_name和XXX is not a registered namespace问题

    python3 Django 环境下,如果你遇到namespace没有注册以及在根目录下urls.py中的include方法的第二个参数namespace添加之后就出错的问题. 出错问题: 'Spec ...

  5. 全球首个开放应用模型 OAM 开源 | 云原生生态周报 Vol. 23

    作者 | 临石.元毅.冬岛.衷源.天元 业界要闻 全球首个开放应用模型 OAM 开源 2019 年 10 月 17 日,阿里巴巴合伙人.阿里云智能基础产品事业部总经理蒋江伟(花名:小邪)在 Qcon ...

  6. RF页面断言

    title should be(断言title与预期指定的title内容相等): should be equal  (断言某个字符串与预期指定的字符串相等) should not be equal ( ...

  7. .NET北京俱乐部,技术的饕餮盛宴,不枉此行

    在19年年尾,我们相约于.NET北京俱乐部 DNT精英论坛暨.NET北京俱乐部是由资深.NET专家和社区活跃分子发起的技术论坛,以“分享.成长.合作.共赢”为原则,致力于打造一个领先的技术分享平台和成 ...

  8. 关于sqlmapapi一点记录

    关于sqlmapapi自己练习的还是很少 今天看见freebuf上师傅的分享的内容 自己练习了一下 来自: https://www.freebuf.com/articles/web/204875.ht ...

  9. cobalt strike笔记-常用beacon扫盲

    最近还是重新补一下cs的东西 0x01 Beacon命令 Beacon Commands =============== Command Description ------- ----------- ...

  10. win2008加入域控之尝试解析加入域中域控制器的dns名称失败解决办法

    记录下今天遇到以前没遇到的问题 加入域的时候提示“尝试解析加入域中控制器的DNS”名称失败 可能的原因: 如果确认dns没问题 dc正常访问,那可能就是因为域控制器无法向dns注册srv记录. SRV ...