问题 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.

样例输入

3 31 2 40 20 0 10 2

样例输出

12

提示

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线段树成段更新的更多相关文章

  1. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

  2. 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. 这题 ...

  3. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  4. hdu 4747【线段树-成段更新】.cpp

    题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...

  5. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

  6. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

  7. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  8. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  9. poj 3648 线段树成段更新

    线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...

随机推荐

  1. 设计模式 ( 十九 ) 模板方法模式Template method(类行为型)

      设计模式 ( 十九 ) 模板方法模式Template method(类行为型) 1.概述 在面向对象开发过程中,通常我们会遇到这样的一个问题:我们知道一个算法所需的关键步骤,并确定了这些步骤的执行 ...

  2. HapiJS开发手冊

    HapiJS开发手冊 作者:chszs.转载需注明.博客主页:http://blog.csdn.net/chszs 一.HapiJS介绍 HapiJS是一个开源的.基于Node.js的应用框架,它适用 ...

  3. jQuery推断复选框是否勾选

    今天要实现一功能就是:复选框勾选时给input表单赋值,复选框取消时将表单值清除. 效果如图: 实现源代码:cyfID为复选框的id $("#cyfID").click(funct ...

  4. shu_1548 悟空问题(大哥,主妖怪抓走的朋友!)

    http://202.121.199.212/JudgeOnline/problem.php?cid=1078&pid=17 分析:  直接暴力了.. . 代码: #include <s ...

  5. 2014 International Conference on Robotics and Computer Vision (ICRVC 2014)

    2014机器人与计算机视觉国际会议ICRVC 与会地点:北京 与会时间:2014.10.24-26 截稿日期:2014-07-10 关于征稿: 语言:英文 主题: • Evolutionary Rob ...

  6. Activity的创建和使用

    Activity: 1:创建一个类继承Activity或者它的子类 public class MainActivity extends Activity { @Override protected v ...

  7. ultraedit比较两个文件差异经验

    链接地址:http://jingyan.baidu.com/article/fcb5aff7876551edab4a714b.html 程序开发人员经常要使用到两个文件的对比,有很多工具可以实现该功能 ...

  8. Java之从控制台读入数据

    0 引言    从控制台中读取数据是一个比较常用的功能,在 JDK 5.0 以前的版本中的实现是比较复杂的,需要手工处理系统的输入流.有意思的是,从 JDK 5.0 版本开始,能从控制台中输入数据的方 ...

  9. PowerManager.WakeLock

    PowerManager.WakeLock PowerManager.WakerLock是我分析Standup Timer源代码时发现的一个小知识点,Standup Timer 用WakeLock保证 ...

  10. Android学习笔记:进度条ProgressBar的使用以及与AsyncTask的配合使用

    ProgressBar时android用于显示进度的组件.当执行一个比较耗时的操作(如io操作.网络操作等),为了避免界面没有变化让用户体验降低,提供一个进度条可以让用户知道程序还在运行. 一.Pro ...