# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
411192 | singh | Deda (COCI17_deda) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
include <bits/stdc++.h>
using namespace std;
const int maxn=2e5;
pair<int,int> tv[4*maxn];
int arr[maxn];
int dist[maxn];
pair<int,int> combine(pair<int,int> a, pair<int,int> b)
{
if(a.first>b.first)
return b;
else if(b.first>a.first)
return a;
}
void build(int v,int tl,int tr)
{
if(tl==tr)
tv[v]={dist[arr[tl]],arr[tl]};
else
{
int mid=(tl+tr)/2;
build(v*2,tl,mid);
build(v*2+1,mid+1,tr);
tv[v]=combine(tv[v*2],tv[v*2+1]);
}
}
pair<int,int> get_first(int v, int lv, int rv, int l, int r, int x) {
if(lv > r || rv < l) return {-1,-1};
if(l <= lv && rv <= r) {
if(tv[v].first> x) return {-1,-1};
while(lv != rv) {
int mid = lv + (rv-lv)/2;
if(tv[2*v].first <= x) {
v = 2*v;
rv = mid;
}else {
v = 2*v+1;
lv = mid+1;
}
}
return {arr[lv],tv[lv].first};
}
int mid = lv + (rv-lv)/2;
pair<int,int> rs = get_first(2*v, lv, mid, l, r, x);
if(rs.first != -1) return rs;
return get_first(2*v+1, mid+1, rv, l ,r, x);
}
void update(int v, int tl, int tr, int pos,int new_val) {
if (tl == tr) {
tv[v]={new_val,arr[tl]};
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos, new_val);
else
update(v*2+1, tm+1, tr, pos, new_val);
tv[v] = combine(tv[v*2], tv[v*2+1]);
}
}
int main()
{
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++)
{
arr[i]=i;
dist[i]=INT_MAX;
}
build(1,1,n);
while(q--)
{
int b,c;
char a;
cin>>a>>b>>c;
if(a=='M')
update(1,1,n,c,b);
else{
pair<int,int> r=get_first(1,1,n,c,n,b);
cout<<r.first<<endl;
}
}
}