「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 世界的博 ...
随机推荐
- 疑似网络抖动引起的RAC单节点宕机
- vue图片点击放大预览
第一种:viewerjs使用介绍(PC.移动端都兼容) 1.先安装依赖 npm install v-viewer --save 2.main.js内引用并注册调用 //main.js import V ...
- Python小游戏——猜数字教程(random库教程)
今天来开发一个简单的数字逻辑游戏,猜数字(数字炸弹) 首先开发游戏第一件事,了解需求. 猜数字游戏规则: 计算机随机生成一个指定范围的数字,由玩家来猜测, 之后计算机会根据玩家提供数字来与自己生成的数 ...
- VPGAME 的 Kubernetes 迁移实践
作者 | 伍冲斌 VPGAME 运维开发工程师 导读:VPGAME 是集赛事运营.媒体资讯.大数据分析.玩家社群.游戏周边等为一体的综合电竞服务平台.总部位于中国杭州,在上海和美国西雅图分别设立了电 ...
- eclipse基础设置
主要参考blog https://jingyan.baidu.com/article/d5a880eb6c4f7813f147ccef.html https://blog.csdn.net/Ricar ...
- 面试官,Java8 JVM内存结构变了,永久代到元空间
在文章<JVM之内存结构详解>中我们描述了Java7以前的JVM内存结构,但在Java8和以后版本中JVM的内存结构慢慢发生了变化.作为面试官如果你还不知道,那么面试过程中是不是有些露怯? ...
- 常用windows命令
目录 本教程概述 用到的工具 标签 简介 1.cmd的一些规则 2.cd切换目录命令 3.dir显示目录命令 4.type显示文本内容 5.del 删除文件 6.查看IP地址 7.net 命令 8.n ...
- [BZOJ3813] 奇数国 - 线段树
3813: 奇数国 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 912 Solved: 508[Submit][Status][Discuss] ...
- 如何巧妙地在基于 TCP Socket 的应用中实现用户注册功能?
通常,在基于TCP的应用中(比如我开源的GGTalk即时通信系统),当TCP连接建立之后,第一个请求就是登录请求,只有登录成功以后,服务器才会允许客户端进行其它性质的业务请求.但是,注册用户这个功能比 ...
- fenby C语言P24
#include <stdio.h> int main(){ char cArr[]={'I','L','O','V','E','C'}; char sArr[]="ilovec ...