CF#52 C Circular RMQ (线段树区间更新)
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
4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1
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 (线段树区间更新)的更多相关文章
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- HDU5039--Hilarity DFS序+线段树区间更新 14年北京网络赛
题意:n个点的树,每个条边权值为0或者1, q次操作 Q 路径边权抑或和为1的点对数, (u, v)(v, u)算2个. M i修改第i条边的权值 如果是0则变成1, 否则变成0 作法: 我们可以求出 ...
随机推荐
- 数组初始化(c, c++, gcc, g++)
这是很基础的东西,但基础的重要性不言而喻,我敢肯定这个知识点我肯定曾经了解过,但现在,我不敢确定,由此可见纪录的重要性,这世界没有什么捷径,找对方向,然后不停重复.所以从今天开始,我会比较详细的纪录这 ...
- Oracle EBS-SQL (INV-2):库存会计期间.sql
SELECT STATUS, PERIOD_NAME, PERIOD_NUMBER, PERIOD_YEAR, START_DATE, END_DATE, CLOSE_DATE, REC_TYPE, ...
- 两个DIV,左DIV宽度固定,右DIV自动填满剩余空间
<style type="text/css"> #main{ width:98%; } #sidebar{ float:left; width:200px; backg ...
- Java学习之InputStream中read()与read(byte[] b)
Java学习之InputStream中read()与read(byte[] b) 这两个方法在抽象类InputStream中都是作为抽象方法存在的, JDK API中是这样描述两者的: read() ...
- MYSQL 巧用count,sum进行统计数据
SELECT a.user,count(b.order_id) as subcount,sum(if(b.verifysta='Y',1,0)) as passcount FROM vicidial_ ...
- Filter及FilterChain的使用具体解释
一.Filter的介绍及使用 什么是过滤器? 与Servlet类似,过滤器是一些web应用程序组件,能够绑定到一个web应用程序中.可是与其它web应用程序组件不同的是,过滤器是"链&quo ...
- Vijos 1493 传纸条
此题,刚开始看上去以为是加简单的动态规划,但是写了后,交上去发自现不对.后来在网上查了题解后发现用到了“多线程DP”的东西.这种DP就是用来解决这种问题的.和P1143 三取方格数那道题很像.只不过是 ...
- Convert Sorted List to Balanced Binary Search Tree (BST)
(http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list ...
- BZOJ 1832: [AHOI2008]聚会( LCA )
LCA模板题...不难发现一定是在某2个人的LCA处集合是最优的, 然后就3个LCA取个最小值就OK了. 距离就用深度去减一减就可以了. 时间复杂度O(N+MlogN) (树链剖分) -------- ...
- Java基础知识总结(二)
&和&&的区别: 按位与:a&b是把a和b都转换成二进制数后逐位进行与的运算.若两数字的某位都为1,则该位的运算结果才为1.运算的最终结果是数字. 逻辑与:a& ...