#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
vector<pair<pair<int, int>, int> > v, w;
int ans[200005], n, q;
void recur(int l, int r)
{
if (l==r)
return;
int mid=(l+r)/2;
w.clear();
for (int i=l; i<=r; i++)
if ((i<=mid)^v[i].se)
w.push_back({v[i].fi, i*v[i].se});
sort(w.begin(), w.end());
set<int> s;
for (auto x:w)
{
if (x.se)
{
auto it=s.lower_bound(-x.fi.se);
ans[x.se]=min(ans[x.se], it==s.end()?n+1:*it);
}
else
s.insert(-x.fi.se);
}
recur(l, mid);
recur(mid+1, r);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i=0; i<q; i++)
{
char c;
int x, y;
cin >> c >> x >> y;
v.push_back({{x, -y}, c=='D'});
ans[i]=n+1;
}
recur(0, q-1);
for (int i=0; i<q; i++)
if (v[i].se)
cout << (ans[i]==n+1?-1:ans[i]) << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |