CF#52 C Circular RMQ (线段树区间更新)
Description
You are given circular array a0, a1, ..., an - 1.
There are two types of operations with it:
- inc(lf, rg, v) — this operation increases each element on the segment
[lf, rg] (inclusively) by
v; - 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 ≤ n ≤ 200000). The next line contains initial state of the array:
a0, a1, ..., an - 1
( - 106 ≤ ai ≤ 106),
ai are integer. The third line contains integer
m (0 ≤ m ≤ 200000),
m — the number of operartons. Next
m lines contain one operation each. If line contains two integer
lf, rg (0 ≤ lf, rg ≤ n - 1) it means
rmq operation, it contains three integers
lf, rg, v (0 ≤ lf, rg ≤ n - 1; - 106 ≤ v ≤ 106)
— 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
1
0
0
题意非常好理解。
假设a>b的话,就查0~b和a~n-1,其余的就是线段树区间更新模板,推断m个询问里是否存在c,详见代码,非常好理解。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#include <iostream>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef __int64 LL;
const __int64 MAX = 9223372036854775807;
const int maxn = 200010;
int n, a, q, c, b;
char str[1200];
LL mi[maxn<<2], add[maxn<<2];
void up(int o) {
mi[o] = min(mi[o<<1], mi[o<<1|1]);
}
void down(int o) {
if(add[o]) {
add[o<<1] += add[o];
add[o<<1|1] += add[o];
mi[o<<1] += add[o];
mi[o<<1|1] += add[o];
add[o] = 0;
}
}
void build(int o, int l, int r) {
if(l == r) {
scanf("%I64d", &mi[o]);
return;
}
int m = (l+r) >> 1;
build(lson);
build(rson);
up(o);
}
void update(int o, int l, int r) {
if(a <= l && r <= b) {
add[o] += c;
mi[o] += c;
return ;
}
down(o);
int m = (l+r) >> 1;
if(a <= m) update(lson);
if(m < b ) update(rson);
up(o);
}
LL query(int o, int l, int r) {
if(a <= l && r <= b) return mi[o];
down(o);
int m = (l+r) >> 1;
LL res = MAX;
if(a <= m) res = query(lson);
if(m < b ) res = min(res, query(rson));
return res;
}
int main()
{
scanf("%d", &n);
build(1, 0, n-1);
scanf("%d", &q); getchar(); while(q--) {
gets(str);
if(sscanf(str,"%d %d %d", &a, &b, &c) == 3) {
if(a <= b) update(1, 0, n-1);
else {
int tmp = b;
b = n-1; update(1, 0, n-1);
a = 0, b = tmp; update(1, 0, n-1);
}
} else {
LL ans ;
if(a <= b) ans = query(1, 0, n-1);
else {
int tmp = b;
b = n-1; ans = query(1, 0, n-1);
a = 0, b = tmp; ans = min(ans, query(1, 0, n-1));
}
printf("%I64d\n", ans);
}
}
return 0;
}
CF#52 C Circular RMQ (线段树区间更新)的更多相关文章
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- HDU5039--Hilarity DFS序+线段树区间更新 14年北京网络赛
题意:n个点的树,每个条边权值为0或者1, q次操作 Q 路径边权抑或和为1的点对数, (u, v)(v, u)算2个. M i修改第i条边的权值 如果是0则变成1, 否则变成0 作法: 我们可以求出 ...
随机推荐
- Ajax中send方法的使用
Ajax中send方法参数的使用 一般情况下,使用Ajax提交的参数多是些简单的字符串,可以直接使用GET方法将要提交的参数写到open方法的url参数中,此时send方法的参数为null. 例如 : ...
- erlang学习笔记(1)
提示符erl 注释% comment 表达式123456789 * 123456789. 变量(单一赋值)X = 123456789.X.Y = X * X * X.Y.f(). 整数浮点数X = 5 ...
- c语言中的制表符\t与空格
(本文不讨论制表符与空格缩进问题) 编程过程中,我们常常用多个空格或制表符分隔两个字符串,那么这两个在显示效果上有什么区别呢? 比较如下两行代码的输出效果 代码1: printf("1\t1 ...
- poj1656---数黑格子
题意:有white,black,test操作 black将给定范围涂黑,white将给定范围涂白,test将给定范围的黑格子数出来并且输出 思路:无论哪个操作格子范围都在 (x,y) (x+L-1 ...
- PLSQl远程连接oracle数据库
PLSQL远程连接Oracle 10G 1.在安装ORACLE服务器的机器上搜索下列文件, ORACLE 服务器上的文件 oci.dll ocijdbc10.dll ociw32.dl ...
- ceph 块设备
数据的存储设备? 数据的存储有3种形式,1种是直接以二进制数据的形式存储在裸设备(包括块设备)上,另外一种是以文件的形式经过文件系统管理进行存储.第三种就是以对象的形式进行对象存储.本篇讨论围绕着块设 ...
- OC 中的block使用
在iOS的开发过程中,使用块的地方很多也很方便,但是在使用块的过程中要注意内存泄露的问题. 在块创建的时候,会对块内的所有对象的引用计数加一,直到块销毁,所以在使用块的过程中需要我们进行处理,在这里以 ...
- VS2013 快捷键 与 RESHARPER 冲突
1.VS设置工具-->选项-->环境-->键盘-->重置 2.RESHARPER -->Options-->Environment → Keyboard & ...
- 请求http服务
①服务方法 [HttpGet]//get服务 public JsonResult GetUserName(int id) { try { IXiao_UserBLL bll = new Xiao_Us ...
- 解决全站ie6下PNG图片不透明问题只要几行代码
解决全站ie6下PNG图片不透明问题只要复制下面这几行代码粘贴在你的文档最底部,需要用到的包DD_belatedPNG_0.0.8a.js自己网上下载吧 代码走起 /*在文档底部加入以下代码*/ &l ...