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.

样例输入

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;
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TommyTT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值