更好的阅读体验

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. python编程基础之一

    编译:将全部代码转成二进制可执行文件 速度快, c,c++等 解释:一行一行的将代码解释 速度慢 python,php等 python简介:Guido van Rossum 1989年 常用的pyth ...

  2. Get https://172.18.255.243:6443/api/v1/namespaces/kube-system/configmaps/kubelet-config-1.12: dial tcp 172.18.255.243:6443: i/o timeout

    问题描述 使用外网加入集群的时候报如下错误: Get https://172.18.255.243:6443/api/v1/namespaces/kube-system/configmaps/kube ...

  3. Redis 哨兵机制以及灾难演练

    #### 哨兵都采用这个配置即可 ##### 1.修改sentinel.conf配置文件 ![image](https://img2018.cnblogs.com/blog/1334966/20191 ...

  4. PHP弱性处理0e开头md5哈希字符串缺陷/bug

    PHP在处理哈希字符串时,会利用”!=”或”==”来对哈希值进行比较,它把每一个以”0E”开头的哈希值都解释为0,所以如果两个不同的密码经过哈希以后,其哈希值都是以”0E”开头的,那么PHP将会认为他 ...

  5. Yroot of High-Precisions (luogu P2293 [HNOI2004]高精度开根)

    Background\text{Background}Background I've accepted\text{I've accepted}I've accepteda very good prob ...

  6. [LUOGU3413] SAC#1 - 萌数

    题目背景 本题由世界上最蒟蒻最辣鸡最撒比的SOL提供. 寂月城网站是完美信息教室的官网.地址:http://191.101.11.174/mgzd . 题目描述 辣鸡蒟蒻SOL是一个傻逼,他居然觉得数 ...

  7. Cocos2d-x 学习笔记(11.7) Repeat RepeatForever

    1. 成员变量 Repeat: unsigned int _times; //create参数 unsigned int _total; //执行的次数 float _nextDt; //startW ...

  8. 并发编程之Fork/Join

    并发与并行 并发:多个进程交替执行. 并行:多个进程同时进行,不存在线程的上下文切换. 并发与并行的目的都是使CPU的利用率达到最大.Fork/Join就是为了尽可能提高硬件的使用率而应运而生的. 计 ...

  9. Shiro 并发登录控制

    本文转载于:https://www.w3cschool.cn/shiro/epht1ifg.html

  10. 线性回归 python 代码实现

    本代码参考自:https://github.com/lawlite19/MachineLearning_Python#%E4%B8%80%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%B ...