转载请注明出处: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. Free Mind » Blog Archive » Yakuake + dtach vs Screen + urxvt

    Free Mind » Blog Archive » Yakuake + dtach vs Screen + urxvt Yakuake + dtach vs Screen + urxvt

  2. Cordova/Phonegap 升级至 2.8.1

    相关链接 Apache Cordova 项目首页: http://cordova.apache.org/ Apache Cordova 历史版本列表: http://archive.apache.or ...

  3. 无需安装Mono的Jexus

    ASP.NET跨平台实践:无需安装Mono的Jexus“独立版”   在Linux上运行ASP.NET网站或WebApi的传统步骤是,先安装libgdiplus,再安装mono,然后安装Jexus.在 ...

  4. 使用国内源解决Qt在线更新慢的问题

    Qt在线安装更新工具默认使用官方的源,国内访问比较慢,可以在setting中增加国内的源来提高更新速度,如下面的源: http://mirrors.ustc.edu.cn/qtproject/onli ...

  5. SICP 解题集 — SICP 解题集

    SICP 解题集 — SICP 解题集 SICP 解题集¶ 这个文档的目标是成为中文化的.完整的<计算机程序的构造和解释>一书的解题集. 这个解题集的特色是: 对于每道习题,除了习题答案之 ...

  6. oschina jQuery 插件

    jQuery 插件 jQuery自动完成插件(25) jQuery分页插件(20) jQuery 文件上传(21) jQuery 地图插件(14) jQuery对话框(109) jQuery图片展示/ ...

  7. poj1459(最大流)

    传送门:Power Network 题意:在一个网络图中有n个点,其中有np个发电站,nc个用户,m条电线;每个发电站,用户,和电线都对应有一个最大的电流;让求出该网络中最大的电流. 分析:最大流裸题 ...

  8. LeetCode总结 -- 树的性质篇

    树的性质推断是树的数据结构比較主要的操作,一般考到都属于非常easy的题目,也就是第一道入门题.面试中最好不能有问题,力求一遍写对.不要给面试官不论什么挑刺机会.LeetCode中关于树的性质有下面题 ...

  9. Spring3.0官网文档学习笔记(七)--3.4.2

    3.4.2 依赖与配置的细节     3.4.2.1  Straight values (primitives, Strings, and so on)     JavaBeans PropertyE ...

  10. [ACM] hdu 4418 Time travel (高斯消元求期望)

    Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...