Find the minimum线段树成段更新
问题 G:
Find the minimum
时间限制: 2 Sec 内存限制: 128 MB
提交: 83
解决: 20
[
提交][
状态][
讨论版]
题目描述
Given an integer array of size N, we define two kind of operators:
1. Add(L,R,W) : adding an integer W to every element in subarray[L,R];
2. Min(L,R) : returning the minimum number in subarray[L,R].
Note. L and R are the index of array starting from 0. L > R is possible. If L > R, the subarray is composed of array[L], array[L+1].....array[N-1], array[0], array[1],.....array[R].
输入
There are several test cases, processed to the end of file.
For each test, the first line contains two positive integers N and M. N is the size of array, and M is the number of the operation.
The second line contains N array elements, a1, a2, a3, ...., and an.
Then in the following M lines, each line contains an operation. If the line contains three integers L,R and W, it means the add(L,R,W) operator should be involved. If the line contains two integers L,R , it means the Min(L,R) operator should be involved.
(0<N, M<100,000; 0<= ai <= 10^6; 0 <= L, R <= N – 1, -10^6 <= W <= 10^6。)
输出
For each Min(L,R) operator in test case, output the return value.
样例输入
样例输出
提示
the output value may be very large ,long long type is recommended!
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 111111;
long long mins[maxn<<4];
long long col[maxn<<4]; void PushUP(int rt) {
mins[rt] = min(mins[rt<<1] , mins[rt<<1|1]);
} void PushDown(int rt,int l,int r) {
if (col[rt]) {
col[rt<<1]+=col[rt];
col[rt<<1|1]+=col[rt];
mins[rt<<1]+=col[rt];
mins[rt<<1|1]+=col[rt];
col[rt] = 0;
}
}
void build(int l,int r,int rt) {
col[rt]=0;
if (l == r) {
scanf("%lld",&mins[rt]);;
return;
}
int m = (l + r) >> 1;
build(l , m , rt << 1);
build(m + 1 , r , rt << 1 | 1);
PushUP(rt);
}
void update(int L,int R,long long add,int l,int r,int rt) {
if (L<=l && r<=R) {
mins[rt]+=add;
col[rt]+=add;
return ;
}
PushDown(rt,l,r);
int m = (l + r) >> 1;
if(L<=m) update(L,R,add,l , m , rt << 1);
if(R>m) update(L,R,add,m + 1 , r , rt << 1 | 1);
PushUP(rt);
}
long long ret;
void query(int L,int R,int l,int r,int rt) {
if (L <= l && r <= R) {
ret=min(ret,mins[rt]);
return;
}
int m = (l + r) >> 1;
PushDown(rt,l,r);
if (L <= m) query(L , R , l , m , rt << 1);
if (R > m) query(L , R , m + 1 , r , rt << 1 | 1);
}
int main()
{
int n,t;
long long v;
int l,r;
while(scanf("%d%d",&n,&t)!=EOF)
{
build(1,n,1);
while(t--)
{
int flag=1;
scanf("%d%d",&l,&r);
if(getchar()==' ') {scanf("%lld",&v);flag=0;}
l++;r++;
if(flag)
{
ret=0x7fffffffffffffffLL;
if(l>r)
{
query(l,n,1,n,1);
query(1,r,1,n,1);
printf("%lld\n",ret);
}
else
{
query(l,r,1,n,1);
printf("%lld\n",ret);
}
}
else
{
if(l>r){
update(l,n,v,1,n,1);
update(1,r,v,1,n,1);
}
else update(l,r,v,1,n,1);
}
}
}
return 0;
}
Find the minimum线段树成段更新的更多相关文章
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- poj 3648 线段树成段更新
线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
随机推荐
- 言未及之而言,谓之躁;言及之而不言,谓之隐;未见颜色而言,谓之瞽(gǔ)
前言 一个高效的团队离不开leader和组员之前,组员和组员之前的通力合作.而合作的基础便是彼此之间的商讨与协调,意见的统一,进而在达成共识的前提下行动.那么如何才能和组员达成共识呢? 和组员之间的沟 ...
- iOS:(接口适配器3)--iPhone适应不同型号 6/6plus 前
对于不同的苹果设备.检查每个参数<iOS:机型參数.sdk.xcode各版本号>. 机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了.表示机器屏幕尺寸变大了: 像素:表示屏幕 ...
- TOMCAT的域名配置
链接地址:http://blog.163.com/skk1987@126/blog/static/29303413201051383548377/ 现在很多的公司的网站都是用tomcat作为应用服务区 ...
- java--方法和成员的继承,访问
//在调用方法的时候,不是看句柄是哪一个类,而应该看对象是属于哪一个类的,属于哪一个类的,就调用哪一个类的成员和方法. //子类可以添加自己的新方法,但是子类对象的引用赋值给父类句柄之后,不能使用父类 ...
- 03-Foundation中NSMutableArray遍历、复制和排序
目录: 一.NSString补充 二.NSMutableArray可变数组 三.遍历 四.NSArray支持的新语法 五.数组复制 六.数组的排序 SDK.API.Foundation.Cocoa是什 ...
- 【译】在Asp.Net中操作PDF – iTextSharp - 操作图片
原文 [译]在Asp.Net中操作PDF – iTextSharp - 操作图片 作为我的iTextSharp系列的文章的第七篇,开始探索使用iTextSharp在PDF中操作图片,理解本篇文章需要看 ...
- UTL_RAW
The UTL_RAW package provides SQL functions for manipulating RAW data types. 该包的功能其实可以用来加密: SELECT ...
- hdu4620 Fruit Ninja Extreme
Fruit Ninja Extreme Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Hystrix 使用与分析
转载请注明出处哈:http://hot66hot.iteye.com/admin/blogs/2155036 一:为什么需要Hystrix? 在大中型分布式系统中,通常系统很多依赖(HTTP,hess ...
- 基于visual Studio2013解决面试题之1409基数排序
题目