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