G - RMQ and RAQ (cugb2023-s)

Write a program which manipulates a sequence AA = {a_0, a_1, ..., a_{n-1}a0​,a1​,...,an−1​} with the following operations:

  • add(s, t, x)add(s,t,x) : add xx to a_s, a_{s+1}, ..., a_tas​,as+1​,...,at​.
  • find(s, t)find(s,t) : report the minimum value in a_s, a_{s+1}, ..., a_tas​,as+1​,...,at​.

Note that the initial values of a_i ( i = 0, 1, ..., n-1 )ai​(i=0,1,...,n−1) are 0.

Input

nn qq
query_1query1​
query_2query2​
:
query_qqueryq​

In the first line, nn (the number of elements in AA) and qq (the number of queries) are given. Then, iith query query_iqueryi​ is given in the following format:

0 ss tt xx

or

1 ss tt

The first digit represents the type of the query. '0' denotes add(s, t, x)add(s,t,x) and '1' denotes find(s, t)find(s,t).

Output

For each findfind query, print the minimum value.

Constraints

  • 1 ≤ n ≤ 1000001≤n≤100000
  • 1 ≤ q ≤ 1000001≤q≤100000
  • 0 ≤ s ≤ t < n0≤s≤t<n
  • -1000 ≤ x ≤ 1000−1000≤x≤1000

Sample Input 1

6 7
0 1 3 1
0 2 4 -2
1 0 5
1 0 1
0 3 5 3
1 3 4
1 0 5

Sample Output 1

-2
0
1
-1

#include <iostream>
#include <cmath> 
using namespace std;
#define ll long long
#define inf 1e31+5
const int N=1e5+5;
ll a[N];
struct node
	{
		int l,r;
		ll lazy;
		ll minn;
	 } tr[N<<2];

//维护
void pushup(int u)
{
	tr[u].minn=min(tr[u<<1].minn,tr[u<<1|1].minn);
 } 
 //建树
 void build(int u,int l,int r)
 {
 	tr[u].l=l;
 	tr[u].r=r;
 	tr[u].lazy=0;
 	if(l==r)
 	{
 		tr[u].minn=a[l];
 		return ;
	 }
	 int mid=l+r>>1;
	 build(u<<1,l,mid);
	 build(u<<1|1,mid+1,r);
	 pushup(u);
  } 
void pushdown(int u)
{
	if(tr[u].lazy)
	{
		tr[u<<1].lazy+=tr[u].lazy;
		tr[u<<1].minn=tr[u<<1].minn+tr[u].lazy;
		tr[u<<1|1].lazy+=tr[u].lazy;
		tr[u<<1|1].minn=tr[u].lazy+tr[u<<1|1].minn;
		tr[u].lazy=0;
	}
}
//区间修改
void modify(int u,int l,int r,ll k)
{
	if(l<=tr[u].l&&r>=tr[u].r)
	{
		tr[u].lazy+=k;
		tr[u].minn=k+tr[u].minn;
		return;
	}
	pushdown(u);
	int mid=tr[u].l+tr[u].r>>1;
	if(l<=mid)
	modify(u<<1,l,r,k);
	if(r>mid)
	modify(u<<1|1,l,r,k);
	pushup(u);
 } 

//区间查询
 ll query(int u,int l,int r)
 {
 	if(l<=tr[u].l&&r>=tr[u].r)
 	{
 		return tr[u].minn;
	 }
	 pushdown(u);
	 int mid=tr[u].l+tr[u].r>>1;
	 ll res=inf;
	 if(l<=mid)
	 {
	 	res=min(res,query(u<<1,l,r));
	 }
	 if(r>mid)
	 {
	 	res=min(res,query(u<<1|1,l,r));
	 }
	return res;
  } 

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n+5;i++)
	{
		a[i]=0;
	}
	build(1,1,n);
	for(int i=1;i<=m;i++)
	{
		int t;
		cin>>t;
		if(t==0)
		{
			int l,r;
			ll k;
			cin>>l>>r>>k;
			modify(1,l+1,r+1,k);//注意下标从1开始
		}
		else
		{
			int l,r;
			cin>>l>>r;
			cout<<query(1,l+1,r+1)<<endl;
		}
	}
	
	
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

linalw

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

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

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

打赏作者

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

抵扣说明:

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

余额充值