CodeForces 52C Circular RMQ(间隔周期段树,间隔更新,间隔总和)
转载请注明出处: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.
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.
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).
4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1
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(间隔周期段树,间隔更新,间隔总和)的更多相关文章
- CodeForces 52C Circular RMQ (线段树)
线段树区间更新维护最小值...记得下放标记... 如果线段树上的一个完整区间被修改,那么最小值和最大值增加相应的值后不变, 会改变是因为一部分改变而另外一部分没有改变所以维护一下就好. 询问的时候也要 ...
- [CodeForces 52C]Circular RMQ
题目传送门 评分:省选/NOI-,难度:普及+/提高 这题真的和RMQ没有半点关系,只需要一个裸的线段树,连pushdown都不需要,只需要两种操作:区间修改和区间求最小值,在回溯时加上标记即可,唯一 ...
- ZOJ 1610 间隔染色段树
要长8000仪表板.间染色的范围,问:最后,能看到的颜色,而且颜色一共有段出现 覆盖段 数据对比水 水可太暴力 段树: #include "stdio.h" #include ...
- HDU 6356.Glad You Came-线段树(区间更新+剪枝) (2018 Multi-University Training Contest 5 1007)
6356.Glad You Came 题意就是给你一个随机生成函数,然后从随机函数里确定查询的左右区间以及要更新的val值.然后最后求一下异或和就可以了. 线段树,区间最大值和最小值维护一下,因为数据 ...
- 计蒜客 28315.Excellent Engineers-线段树(单点更新、区间最值) (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 E)
先写这几道题,比赛的时候有事就只签了个到. 题目传送门 E. Excellent Engineers 传送门 这个题的意思就是如果一个人的r1,r2,r3中的某一个比已存在的人中的小,就把这个人添加到 ...
- POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)
水博客,水一水. Horizontally Visible Segments Time Limit: 5000MS Memory Limit: 65536K Total Submissions: ...
- TOJ 4325 RMQ with Shifts / 线段树单点更新
RMQ with Shifts 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 描述 In the traditional RMQ (Range M ...
- hdu4521 小明系列的问题——小明序列(LIS变种 (段树+单点更新解决方案))
链接: huangjing 题目:中文题目 思路: 1:这个题目假设去掉那个距离大于d的条件,那么必定是一个普通的LIS.可是加上那个条件后就变得复杂了.我用的线段树的解法. . .就是採用延迟更新的 ...
- ACM-线段树区间更新+离散化
区间更新与单点更新最大的不同就在于Lazy思想: http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html 可以看这篇文章,讲得比较清楚 在具体使用上, ...
随机推荐
- 不用splitter控件 简单实现对mfc对话框的分割的方法
不用splitter控件 简单实现对mfc对话框的分割的方法 直接贴上源代码主要部分吧 这个是基于对话框的工程 进行对话框的分割实现 只是相应了三个消息函数,看一下就会明白的 我空间资源里边有现成的 ...
- Lucene.Net 2.3.1开发介绍 —— 二、分词(六)
原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(六) Lucene.Net的上一个版本是2.1,而在2.3.1版本中才引入了Next(Token)方法重载,而ReusableStrin ...
- Android改变系统自带环形ProgressBar的大小
MainActivity如下: package cc.testprogressbar; import android.os.Bundle; import android.app.Activity; / ...
- Swift - 使用表格组件(UITableView)实现分组列表
1,样例说明: (1)列表以分组的形式展示 (2)同时还自定义分区的头部和尾部 (3)点击列表项会弹出消息框显示该项信息. 2,效果图: 3,代码如下: 1 2 3 4 5 6 7 8 9 ...
- enum可以做索引
enum可以做索引 enum可以做索引, 配上虚函数,或者函数指针,可以实现上层的统一封装和快速索引. 点击(此处)折叠或打开 MoTbl.cpp #include <stdio.h> # ...
- HTTP POST请求的Apache Rewrite规则设置
最近自测后端模块时有个业务需求需要利用WebServer(我用的是Apache)将HTTP POST请求转发至后端C模块,后端处理后返回2进制加密数据.http post请求的url格式为: ...
- C语言实现通用数据结构的高效设计
近期在阅读一个开源的C++代码.里面用到了大量的STL里面的东西.或许是自己一直用C而非常少用C++来实现算法的原因.STL里面大量的模板令人心烦.一直对STL的效率表示怀疑,但在网上搜到这样一个帖子 ...
- http调试工具Charles Proxy用法详解
Charles Proxy 通常称为Charles,Charles是目前最强大的http调试工具,在界面和功能上远强于Fiddler,同时是全平台支持,堪称圣杯级工具,不过在这里为您提供了Charle ...
- grep与正则表达式,grep、egrep和fgrep
grep用法详解:grep与正则表达式 首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串. ...
- LLVM每日谈21 一些编译器和LLVM/Clang代码
作者:闪亮宁(snsn1984) 一些自己的收藏LLVM/Clang代码,而他自己写一些一点点LLVM/Clang译器的代码.在这里把这些代码库分享出来,欢迎大家交流探讨. 1.crange http ...