CodeForces - 52C-Circular RMQ 线段树区间操作

题目链接:CodeForces - 52C

Circular RMQ

题目描述

You are given circular array a 0, a 1, …, a n - 1. There are two types of operations with it:

inc(lf, rg, v) — this operation increases each element on the segment [lf, rg] (inclusively) by v; rmq(lf, rg) — this operation returns minimal value on the segment [lf, rg] (inclusively). Assume segments to be circular, so if n = 5 and lf = 3, rg = 1, it means the index sequence: 3, 4, 0, 1.

Write program to process given sequence of operations.

Input

The first line contains integer n (1 ≤ n ≤ 200000). The next line contains initial state of the array: a 0, a 1, …, a n - 1 ( - 106 ≤ a i ≤ 106), a i are integer. The third line contains integer m (0 ≤ m ≤ 200000), m — the number of operartons. Next m lines contain one operation each. If line contains two integer lf, rg (0 ≤ lf, rg ≤ n - 1) it means rmq operation, it contains three integers lf, rg, v (0 ≤ lf, rg ≤ n - 1; - 106 ≤ v ≤ 106) — inc operation.

Output

For each rmq operation write result for it. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

题意

维护一个1-n的数组a[],可进行m次操作,操作有两种
①输入两个数l,r,
若l<=r,则查询区间 [ l , r ] [l,r] [lr]内的最小值
若l>r,则查询区间 [ l , n − 1 ] [l,n-1] [ln1] [ 0 , r ] [0,r] [0r]的最小值,
②输出三个数l,r,v
若l<=r,则区间 [ l , r ] [l,r] [lr]内的每个数加v
若l>r,则区间 [ l , n − 1 ] [l,n-1] [ln1] [ 0 , r ] [0,r] [0r]内的每个数加v

#pragma GCC optimize(2)
#include<bits/stdc++.h> 
using namespace std;
#define ll long long
#define endl "\n"
const int MAX=200005;
struct node
{
	ll lz;//lazy标记
	ll mn;//最小值min
}d[MAX<<2];
ll n,m,x;
void push_up(int p)
{
	d[p].mn=min(d[2*p].mn,d[2*p+1].mn);
}
void push_down(int s,int t,int mid,int p)
{
	if(d[p].lz==0)return ;
		d[2*p].lz+=d[p].lz;
		d[2*p].mn+=d[p].lz;	
		d[2*p+1].lz+=d[p].lz;
		d[2*p+1].mn+=d[p].lz;
		d[p].lz=0;	
}
void build(int s,int t,int p)
{
	d[p].lz=0;
	if(s==t){
		cin>>d[p].mn;
		return;
	}
	int mid=(s+t)/2;
	build(s,mid,2*p);
	build(mid+1,t,2*p+1);
	push_up(p);
}

void update(int l,int r,int c,int s,int t,int p)  //区间修改
{
	if(l<=s&&t<=r)
	{
		d[p].lz+=c;
		d[p].mn+=c;
		return ;
	}
	int mid=(s+t)/2;
	push_down(s,t,mid,p);
	if(l<=mid)update(l,r,c,s,mid,2*p);
	if(r>mid)update(l,r,c,mid+1,t,2*p+1);
	push_up(p);
}
void update(int l,int r,int c)
{
	update(l,r,c,1,n,1);
}

ll getmin(int l,int r,int s,int t,int p)  //区间查询最小值 
{
	if(l<=s&&t<=r){
		return d[p].mn;
	}
	int mid=(s+t)/2;
	push_down(s,t,mid,p);
	ll ans=INT_MAX;
	if(l<=mid)ans=min(ans,getmin(l,r,s,mid,2*p));
	if(r>mid)ans=min(ans,getmin(l,r,mid+1,t,2*p+1));
	return ans;
}
ll getmin(int l,int r)
{
	return getmin(l,r,1,n,1);
}
int main(){
    //ios_base::sync_with_stdio(0);cin.tie(0);  
    // 记得关闭同步流,不关同步流会影响输入输出
	cin>>n;
	build(1,n,1);
	ll a = INT_MAX , b = INT_MAX , v = INT_MAX;
	
	cin>>m;
	while(m--)
	{
		cin>>a>>b;
		a++,b++;
		char c=getchar();
		if(c!='\n'){
			cin>>v;
           		if(a <= b)update(a,b,v);
                else{
                        update(a,n,v);
                        update(1,b,v);
                }
        }
		else{
        	if(a <= b)cout<<getmin(a,b);
        	else cout<<min(getmin(1,b),getmin(a,n));
        	cout<<endl;
		}
	}
   return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值