// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
using namespace std;
vector<int>tree;
void update(int l,int h,int ti,int idx,int val)
{
if(l==h)
{
tree[ti]=val;
return;
}
int mid = l+(h-l>>1);
int temp = ti<<1;
if(idx<=mid) update(l,mid,temp,idx,val);
else update(mid+1,h,temp+1,idx,val);
tree[ti]=min(tree[temp],tree[temp+1]);
}
int query(int l,int h,int ti,int ql,int qh,int max_ride)
{
if(h<ql || qh<l) return INT_MAX;
if(ql<=l && h<=qh)
{
if(tree[ti]>max_ride) return INT_MAX;
return tree[ti];
}
int mid = l+(h-l>>1);
int temp = ti<<1;
int left = query(l,mid,temp,ql,qh,max_ride);
int right = query(mid+1,h,temp+1,ql,qh,max_ride);
return min(left,right);
}
void solve()
{
int n,q;
cin >> n >> q;
tree.resize(n<<2,INT_MAX);
while(q--)
{
char c;
cin >> c;
if(c=='M')
{
int idx,val;
cin >> val >> idx;
update(1,n,1,idx,val);
}
else
{
int min_child,max_ride;
cin >> max_ride >> min_child;
int l=min_child,h=n;
int ans=INT_MAX;
while(l<=h)
{
int mid = l+(h-l>>1);
int res = query(1, n, 1, min_child, mid, max_ride);
if(res>max_ride) l=mid+1;
else
{
ans=mid;
h=mid-1;
}
}
if(ans==INT_MAX) ans=-1;
cout << ans << "\n";
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
// cin >> t;
while(t--) solve();
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |