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. javaScript的2种变量范围有什么不同

    1.javascript怎样选中一个checkbox,怎样设置它无效? document.all.cb1[0].disabled = true;   2.js中的3种弹出式消息提醒(警告窗口,确认窗口 ...

  2. <转>eclipse如何修改dynamic web module version .

         --------------------------------------------------------------------------------------------- 原 ...

  3. CSS3 Test

    CSS3Test 如何判定一个浏览器对css3的支持情况呢 有这么一个站点http://css3test.com 可以测试浏览器对CSS3的支持情况 对应的Github在这里 原理 实际上浏览器对CS ...

  4. CentOS和Redhat发行版linux内核版本的对应关系

    由于Redhat和CentOS的发行版本现在众多,所以我们应该知道CentOS和Redhat及linux内核之间版本的对应关系对维护系统还是很有帮助的.对应的列表如下: Redhat 9.0————— ...

  5. JS中的prototype属性

    JavaScript是基于对象的,任何元素都可以看成对象.然而,类型和对象是不同的.本文中,我们除了讨论类型和对象的一些特点之外,更重要的 是研究  如何写出好的并且利于重用的类型.毕竟,JavaSc ...

  6. qutIm编译

    官网:http://www.qutim.org/ 原文地址:http://wiki.qutim.org/en/building_from_git 依赖: Qt4-dev 4.7:http://qt-p ...

  7. 获取EIP(汇编语言直接给Delphi变量赋值)

    var EIP: Cardinal; procedure GetEIP(); stdcall; asm pop eax; mov EIP,eax; push eax; end; procedure T ...

  8. C#动态编译、执行代码

    在开始之前,先熟悉几个类及部分属性.方法:CSharpCodeProvider.ICodeCompiler.CompilerParameters.CompilerResults.Assembly. 一 ...

  9. 菜鸟级SQL Server21天自学通(文档+视频)

    SQL语言的主要功能就是同各种数据库建立联系,进行沟通.按照ANSI(美国国家标准协会)的规定,SQL被作为关系型数据库管理系统的标准语言.SQL语句可以用来执行各种各样的操作,例如更新数据库中的数据 ...

  10. Android中进行流量统计

    // ---------------------流量统计-------------------------------- try { PackageManager pm = getPackageMan ...