问题 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. Java字符串排序中文+数字

    编写日期: 2013年9月15日 另一中解法:点击查看 解决思路: 在Java中,排序需要复写的是 equals 方法 和 Comparable<T> 接口 的public int com ...

  2. CodeForces 191C 树链剖分 第4遍

    非常无奈,模板重新无奈的打错了.. 只是,非常快便找到了.. 题意:给一些边,有一些操作,每次操作,都要在这些边上加上1,求每一个边的边权.. #include<cstdio> #incl ...

  3. NGINX服务器打开目录浏览功能

    我们做文件服务器的时候,希望打开目录浏览的功能.但是Nginx默认是不允许列出目录功能的.若需要此功能,需要在配置文件中手动开启. 首先需要打开开关.autoindex on;autoindex_ex ...

  4. JMS开源比较

    Java开源JMS消息中间件 mom4j mom4j是一个完全实现JMS1.1规范的消息中间件并且向下兼容JMS1.0与1.02.它提供了自己的消息处理存储使它独立于关系数据与语言,所以它的客户端可以 ...

  5. 已知一指针p,你可以确定该指针是否指向一个有效的对象吗?如果可以,如何确定?如果不可以,请说明原因。

    这个问题我的思路是:首先用*p将其值输出来,如果编译器报错,证明p指向一个无效的对象,要么p=0要么p未进行初始化,此时可以用if(p == NULL)进行判断即可,不知道大家是否有好的思路噻...

  6. 【cocos2d-x】3.0使用cocos-console创建,编,部署游戏

    原文地址:http://fengchenluoyu.duapp.com/272.html cocos2d-x 3.0開始添加了一个cocos-console组件,它位于cocos2d-x 3.0的to ...

  7. tcp/ip协议listen函数中backlog參数的含义

    listen函数的定义例如以下所看到的: #include <sys/socket.h> int accept(int sockfd, struct sockaddr * restrict ...

  8. mysql导入sql文件过大或连接超时的解决的方法

    前段时间出差在现场开发的时候,导入数据库老是出问题.最后发现了一个奇妙sql语句交给实施,仅仅要导入出错就把例如以下语句运行就能够了.至今屡试不爽. set global max_allowed_pa ...

  9. RedHat Linux 5.5安装JDK+Tomcat并部署Java项目

    与大家分享下RedHat Linux 5.5安装JDK+Tomcat并部署Java项目的步骤,希望对大家有用. 1.下载并安装jdk 虚拟机中安装RedHat Linux 5.5 64位企业版, 这里 ...

  10. 总线接口与计算机通信(五)CAN总线

        CAN网络图示     CAN的特点      CAN协议具有以下特点.      (1) 多主控制  在总线空闲时,所有的单元都可开始发送消息(多主控制). 最先访问总线的单元可获得发送权( ...