转载请注明出处: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. 如果用float实现居中

    今天发现自己做的一个项目中有个图片切换的下面的按钮不是固定个数,程序那边根据循环实现放几个切换的按钮,但是按钮相对于整体的要居中,刚开始想着用display:inline-block;实现,但是ie6 ...

  2. 浙江大学PAT上机题解析之2-06. 数列求和

    给定某数字A(1<=A<=9)以及非负整数N(0<=N<=100000),求数列之和S = A + AA + AAA + … + AA…A(N个A).例如A=1, N=3时,S ...

  3. 自己定义android 4.0以上的对话框风格

    做个笔记.这里是Dialog的风格,假设是用AlertDialog创建的,不能直接用.在styles.xml的写法: <style name="DialogWindowTitle&qu ...

  4. QML中多样化的ListModel(MultiDelegate)

    在QML的官方例子里面, 基本上都是一样的Delegate, 也就是说不管数据怎样, 样式都是不变的. 如果我们想要根据不同的数据类型来显示不同的UI该怎么办? 这里有一个例子. DataBank L ...

  5. [Django实战] 第5篇 - 用户认证(修改密码)

    上一篇我们实现了用户认证系统的登录模块,这一篇实现修改密码模块. 同样地,我们首先得给修改密码创建表单(forms.py): class ChangepwdForm(forms.Form): oldp ...

  6. Codeforces325-B(二分搜索)

    题目:B. Stadium and Games 分析:问题可以转化为下面的等式求解问题: 由于n在10^18范围内,所以k的范围是从0到63即可,这样就可以枚举k,二分m,然后所有符合条件的就是答案了 ...

  7. codeforces 659C Tanya and Toys

    题目链接:http://codeforces.com/problemset/problem/659/C 题意: n是已经有的数字,m是可用的最大数字和 要求选自己没有的数字,且这些数字的数字和不能超过 ...

  8. 开启程序的Visual Styles

    首先看看MS对Visual Styles的解释: Windows XP and later operating systems support a feature called visual styl ...

  9. Java程序员须知的七个日志管理工具(转)

    Splunk vs. Sumo Logic vs. LogStash vs. GrayLog vs. Loggly vs. PaperTrails vs. Splunk>Storm 英文原文:T ...

  10. H264 Decoder

    http://www.cnblogs.com/mcodec/category/213433.html