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

Input
4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1
Output
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 (线段树区间更新)的更多相关文章

  1. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  2. hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...

  3. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  4. HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...

  5. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  6. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  7. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  8. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  9. HDU5039--Hilarity DFS序+线段树区间更新 14年北京网络赛

    题意:n个点的树,每个条边权值为0或者1, q次操作 Q 路径边权抑或和为1的点对数, (u, v)(v, u)算2个. M i修改第i条边的权值 如果是0则变成1, 否则变成0 作法: 我们可以求出 ...

随机推荐

  1. U盘装centos7系统过程

    1. 使用最新版UltraISO将ISO镜像刻录到U盘 一定要是最新版,试用版都可以,按下图操作: 2. U盘启动电脑进入安装界面 正常情况下你应该会看到下面的这个界面: 选择第一项,然后按TAB键, ...

  2. 原版Windows XP Pro With SP3 VOL MSDN简体中文专业版

    2008年5月2日,微软推出Windows XP Pro With SP3 VOL MSDN x86 32位简体中文专业版,这是最经典也是我最喜爱的操作系统之一.在MSDN(微软开发者网络)的网站上查 ...

  3. C,C++经典(程序、错误程序)

    1,程序 未执行完错误的return 0

  4. Hat's Fibonacci(大数,好)

    Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. Android牛博

    Android实现伸缩弹力分布菜单效果 摘要: 本文介绍下在Android中实现伸缩弹力分布菜单效果.关于这种菜单效果在IPhone中比较常见,效果比较酷.那么在Android中实现只是一种简单的模仿 ...

  6. 潜在语义分析Latent semantic analysis note(LSA)原理及代码

    文章引用:http://blog.sina.com.cn/s/blog_62a9902f0101cjl3.html Latent Semantic Analysis (LSA)也被称为Latent S ...

  7. jsoup UnsupportedMimeTypeExceptio

    Exception in thread "main" <strong><span style="font-size:18px;">org ...

  8. PowerBuilder预防数据库死锁相关处理

    实际业务中碰到了PB开发的业务系统造成的数据死锁情况,整理了一些PB关于数据库死锁的一些处理. PB死锁相关 1. 即时的commit和rollback 不同数据库的锁机制各不相同,但对应用程序来说, ...

  9. 创建一个支持异步操作的operation

    NSOperationQueue时iOS中常用的任务调度机制.在创建一个复杂任务的时候,我们通常都需要编写NSOperation的子类.在大部分情况下,重写main方法就可以满足要求.main方法执行 ...

  10. poj2987 Firing

    以前只是A过很简单的最大闭合权像hdu1565之类,完全的最大流模板题.但是都完全不太懂最大闭合权的定义及其用途. 关于最大流的基础知识,大家可以自己网上搜索关键字.有点基础的哥们妹们,推荐看看胡伯涛 ...