问题 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. Python 做过哪些有趣的项目

          1 icedx   241 天前 via Android   ♥ 1 考虑到Windows 下的类Alfred 软件都太傻逼 自己用PyQT 写了一个       2 crazyxin19 ...

  2. 知识点1-3:MVC设计模式

    MVC代表模型-视图-控制器(Model-View-Controller),起源于20世纪70年代的Smalltalk开发社区,2003年起随着Ruby on Rails的出现才在Web上流行使用. ...

  3. 转:onConfigurationChanged的作用

    API原文说明:android:configChangesLists configuration changes that the activity will handle itself. When ...

  4. c 中关于int,unsigned int , short 各种类型总结

    int类型比较特殊,具体的字节数同机器字长和编译器有关.如果要保证移植性,尽量用__int16 __int32 __int64吧__int16.__int32这种数据类型在所有平台下都分配相同的字节. ...

  5. 基于visual Studio2013解决C语言竞赛题之0703乾坤大挪移

       题目

  6. Android开发之SoundPool使用具体解释

    使用SoundPool播放音效 假设应用程序常常播放密集.急促而又短暂的音效(如游戏音效)那么使用MediaPlayer显得有些不太适合了.由于MediaPlayer存在例如以下缺点: 1)      ...

  7. JAVA编程相关:eclipse如何导入已有工程

    eclipse使用过程中,经常会遇到导入外部eclispe工程的情况,导入外部eclipse也就是将已有的eclipse工程导入到eclipse中,那么如何导入外部工程呢?下面为大家分享导入已有ecl ...

  8. .net版Git Server --- bonobo

    官网地址: https://bonobogitserver.com/ Demo: http://demo.bonobogitserver.com/Home/LogOn  登入admin:admin C ...

  9. CCIE路由实验(9) -- IPv6

    1.IPv6地址的各种情况2.配置通过DHCP-PD方式分配前缀信息3.IPv6路由基本配置4.IPv6路由--RIPng5.IPv6路由--EIGRPv66.IPv6路由--OSPFv37.IPv6 ...

  10. Android网络:开发浏览器(二)——功能完善之长按网页图片菜单

    上述的历史和书签的功能已经实现.不过如果我们长时间按住图片,并不会出现如同UC中的一系列选项,我们可以来看看UC中的长按图片出现的菜单. 图10.2.9    UC中的长按图片菜单 我们可以看到UC中 ...