转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://codeforces.com/problemset/problem/52/C

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 test(s)
input
4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1
output
1
0
0

代码例如以下:

#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
//lson和rson分辨表示结点的左儿子和右儿子
//rt表示当前子树的根(root),也就是当前所在的结点
#define INF 0x3fffffff
__int64 mmin[5000047];
__int64 col[5000047];
__int64 MIN(__int64 a, __int64 b)
{
if( a < b)
return a;
return b;
}
void pushup(int rt)
{
//sum[rt] = sum[rt<<1] + sum[rt<<1|1];
mmin[rt]=MIN(mmin[rt<<1], mmin[rt<<1|1]);
}
void pushdown(int rt, int m)
{
if(col[rt])
{
col[rt<<1]+=col[rt];
col[rt<<1|1]+=col[rt];
mmin[rt<<1]+=col[rt];
mmin[rt<<1|1]+=col[rt];
col[rt]=0;
}
}
void build(int l, int r, int rt)
{
col[rt]=0;
if(l==r)
{
scanf("%I64d", &mmin[rt]);
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int L, int R, int v, int l, int r, int rt)
{
if(L<=l && r<=R)
{
col[rt]+=v;
mmin[rt]+=v;
return ;
}
pushdown(rt, r-l+1);
int m=(l+r)>>1;
if(L<=m)
update(L, R, v, lson);
if(R>m)
update(L, R, v, rson);
pushup(rt);
}
__int64 query(int L, int R, int l, int r, int rt)
{//查询区间[L,R]中的最小值
if(L<=l && r<=R)//当前结点全然包括在查询区间内
return mmin[rt];
pushdown(rt, r-l+1);
int m=(l+r)>>1;
__int64 ret=INF;
if(L<=m)//往左走
ret=MIN(ret, query(L, R, lson));
if(R>m)//往右走
ret=MIN(ret, query(L, R, rson));
return ret;
} int main()
{
int n, m, a, b, c;
char op;
scanf("%d", &n);
build(1, n, 1);
scanf("%d", &m);
while(m--)
{
scanf("%d%d%c", &a, &b, &op);
a++;//将区间转换为是1到n
b++;
if(a<=b)
{
if(op==' ')//意味着是输入三个数的操作
{
scanf("%d", &c);
if(c==0)
continue;
update(a, b, c, 1, n, 1);
}
else
printf("%I64d\n", query(a, b, 1, n, 1));
}
else
{
if(op==' ')
{
scanf("%d", &c);
if(c==0)
continue;
update(a, n, c, 1, n, 1);//拆分区间为a到n和1到b
update(1, b, c, 1, n, 1);
}
else
printf("%I64d\n", MIN(query(a, n, 1, n, 1), query(1, b, 1, n, 1)));
}
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

CodeForces 52C Circular RMQ(间隔周期段树,间隔更新,间隔总和)的更多相关文章

  1. CodeForces 52C Circular RMQ (线段树)

    线段树区间更新维护最小值...记得下放标记... 如果线段树上的一个完整区间被修改,那么最小值和最大值增加相应的值后不变, 会改变是因为一部分改变而另外一部分没有改变所以维护一下就好. 询问的时候也要 ...

  2. [CodeForces 52C]Circular RMQ

    题目传送门 评分:省选/NOI-,难度:普及+/提高 这题真的和RMQ没有半点关系,只需要一个裸的线段树,连pushdown都不需要,只需要两种操作:区间修改和区间求最小值,在回溯时加上标记即可,唯一 ...

  3. ZOJ 1610 间隔染色段树

    要长8000仪表板.间染色的范围,问:最后,能看到的颜色,而且颜色一共有段出现 覆盖段 数据对比水   水可太暴力 段树: #include "stdio.h" #include ...

  4. HDU 6356.Glad You Came-线段树(区间更新+剪枝) (2018 Multi-University Training Contest 5 1007)

    6356.Glad You Came 题意就是给你一个随机生成函数,然后从随机函数里确定查询的左右区间以及要更新的val值.然后最后求一下异或和就可以了. 线段树,区间最大值和最小值维护一下,因为数据 ...

  5. 计蒜客 28315.Excellent Engineers-线段树(单点更新、区间最值) (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 E)

    先写这几道题,比赛的时候有事就只签了个到. 题目传送门 E. Excellent Engineers 传送门 这个题的意思就是如果一个人的r1,r2,r3中的某一个比已存在的人中的小,就把这个人添加到 ...

  6. POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)

    水博客,水一水. Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536K Total Submissions:  ...

  7. TOJ 4325 RMQ with Shifts / 线段树单点更新

    RMQ with Shifts 时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte 描述 In the traditional RMQ (Range M ...

  8. hdu4521 小明系列的问题——小明序列(LIS变种 (段树+单点更新解决方案))

    链接: huangjing 题目:中文题目 思路: 1:这个题目假设去掉那个距离大于d的条件,那么必定是一个普通的LIS.可是加上那个条件后就变得复杂了.我用的线段树的解法. . .就是採用延迟更新的 ...

  9. ACM-线段树区间更新+离散化

    区间更新与单点更新最大的不同就在于Lazy思想: http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html 可以看这篇文章,讲得比较清楚 在具体使用上, ...

随机推荐

  1. javascript中外部js文件取得自身完整路径得办法

    原文:javascript中外部js文件取得自身完整路径得办法 有时候我们需要引入一个外部js文件,这个js文件又需要用到自己的路径或者是所在的目录,别问怎么又这么变态的需求,开发做久了各种奇葩需求也 ...

  2. Handler和HandlerThread

    1.什么是Handler? SDK中关于Handler的说明例如以下: A Handler allows you to sendand process Messageand Runnable obje ...

  3. timeout connect 10000 # default 10 second time out if a backend is not found

    timeout connect <timeout> timeout contimeout <timeout> (deprecated) Set the maximum time ...

  4. Spire.XLS for .NET 測评

         有一位朋友推荐了我 Spire.Office (官网:http://www.e-iceblue.com/) -- 基于.NET的办公软件库,说不错. 究竟怎样呢?仅仅有亲測一下才干知道了. ...

  5. R语言数据框行转列实例

    目的:须要把数据框的行列进行转置 方法: # 原始数据框 > hrl_jd_mon     年份 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 1 2010年 51 ...

  6. c#中的jQuery——HtmlAgilityPack

    原文:c#中的jQuery--HtmlAgilityPack c#中是否有javascript中的jQuery类库? jQuery在访问和操作HTML 的DOM的便捷是前端开发工程师的一种福音,在c# ...

  7. OCA读书笔记(14) - 备份和恢复基本概念

    备份恢复概念 如何判断数据库的一致性 在mount状态下,oracle如何判断数据库的一致性 scn:system change number,它是数据库时钟 如何查询当前系统的scn: select ...

  8. 以正确的方式开源 Python 项目 - 技术翻译 - 开源中国社区

    以正确的方式开源 Python 项目 - 技术翻译 - 开源中国社区 以正确的方式开源 Python 项目 英文原文:Open Sourcing a Python Project the Right ...

  9. python语言学习8——字符串和编码

    Unicode编码 计算机只能处理数字,如果要处理文本,就必须把文本转化为数字才能处理 有许多编码标准,但是不同的编码标准有时候会混乱,所以Unicode应运而生 Unicode把所有语言统一到一套编 ...

  10. FZU2179(数位dp)

    传送门:Chriswho 题意:求区间[1,n]内能整除自己本身各位数字的数的个数. 分析:这题跟CF 55D Beautiful numbers一样的,一个数能被它的所有非零数位整除,则能被它们的最 ...