A - Range Minimum Query (RMQ) (cugb2023-s)

Write a program which manipulates a sequence A = {a0, a1, . . . , an-1} with the following operations:

  • find(s, t): report the minimum element in as, as+1, . . . ,at.
  • update(i, x): change ai to x.

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

Input

n q
com0 x0 y0
com1 x1 y1
...
comq−1 xq−1 yq−1

In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, q queries are given where com represents the type of queries. '0' denotes update(xi, yi) and '1' denotes find(xi, yi).

Output

For each find operation, print the minimum element.

Constraints

  • 1 ≤ n ≤ 100000
  • 1 ≤ q ≤ 100000
  • If comi is 0, then 0 ≤ xi < n, 0 ≤ yi < 231-1.
  • If comi is 1, then 0 ≤ xi < n, 0 ≤ yi < n.

Sample Input 1

3 5
0 0 1
0 1 2
0 2 3
1 0 2
1 1 2

Sample Output 1

1
2

Sample Input 2

1 3
1 0 0
0 0 5
1 0 0

Sample Output 2

2147483647
5

#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=-1;
 	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!=-1)
	{
		tr[u<<1].lazy=tr[u].lazy;
		tr[u<<1].minn=min(tr[u].lazy,tr[u].minn);
		tr[u<<1|1].lazy=tr[u].lazy;
		tr[u<<1|1].minn=min(tr[u].lazy,tr[u].minn);
		tr[u].lazy=-1;
	}
}
//区间修改
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;
		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]=pow(2,31)-1;
	}
	build(1,1,n);
	for(int i=1;i<=m;i++)
	{
		int t;
		cin>>t;
		if(t==0)
		{
			int lr;
			ll k;
			cin>>lr>>k;
			modify(1,lr+1,lr+1,k);
		}
		else
		{
			int l,r;
			cin>>l>>r;
			cout<<query(1,l+1,r+1)<<endl;
		}
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

linalw

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

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

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

打赏作者

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

抵扣说明:

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

余额充值