「CF52C」Circular RMQ
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的更多相关文章
- 【CF52C】Circular RMQ(线段树区间加减,区间最值)
给定一个循环数组a0, a1, a2, …, an-1,现在对他们有两个操作: Inc(le, ri, v):表示区间[le, ri]范围的数值增加v Rmq(le, ri):表示询问区间[le, r ...
- 「CF1380G」 Circular Dungeon
CF1380G Circular Dungeon 看懂样例就能做. 虽然我瞪了 20 分钟 菜是原罪 首先可以将从每一个点出发所能获得的价值相加,再除以 \(n\) 就可以得到价值的期望. 所以问题转 ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
随机推荐
- classpath:类路径
classpath:可以用于web.xml中获取spring springmvc配置文件的位置 用于sprnig配置文件中获取mapper的位置 classpath:可以获取到java目录下的,res ...
- BZOJ - 2783 树
数列 提交文件:sequence.pas/c/cpp 输入文件: sequence.in 输出文件: sequence.out 问题描述: 把一个正整数分成一列连续的正整数之和.这个数列必须包含至少两 ...
- python程序编译成exe文件
最近越来越喜欢使用python写工具.使用的时候,发现程序内部成员python安装目录常常不同,如果用bat双击执行,常常需要修改从svn上down下来的bat文件中python.exe的路径.而给策 ...
- Cocos2d-x入门之旅[3]动作
Cocos通过动作(Action)可以让精灵动起来,把数个动作组成序列(Sequence)就能让精灵做出连续的动作,在动作中我们可以改变精灵的位置,旋转角度,缩放比例,等等 动作(Action) 首先 ...
- RF用例执行方法
用例如下图: 1.执行整个项目下的所有用例 dos命令下输入robot D:\work_doc\RF (RF为下图中脚本项目Test目录的上级目录) 2.执行某个suite中的所有用例 dos命令下输 ...
- Hadoop实战1:MapR在ubuntu集群中的安装
由于机器学习算法在处理大数据处理的时候在所难免的会效率降低,公司需要搭建hadoop集群,最后采用了商业版的Hadoop2(MapR). 官网: http://doc.mapr.com/display ...
- 微信小程序尺寸单位rpx以及样式相关介绍
rpx单位是微信小程序中css的尺寸单位,rpx可以根据屏幕宽度进行自适应.规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则750rpx = 375p ...
- [Luogu2737] [USACO4.1]麦香牛块Beef McNuggets
题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如 ...
- [POJ2356] Find a multiple 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8776 Accepted: 3791 ...
- opencv实践::直线检测
问题描述 寻找英语试卷填空题的下划线,这个对后期的切图与自动 识别都比较重要. 解决思路 方法: 通过图像形态学操作来寻找直线,霍夫获取位置信息与显示. #include <opencv2/op ...