// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using vii = vector<pii>;
using vvii = vector<vii>;
using l = long long;
using vl = vector<l>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using pll = pair<l, l>;
using vll = vector<pll>;
using vvll = vector<vll>;
using d = double;
using vd = vector<d>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using ld = long double;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pbb = pair<bool, bool>;
using vbb = vector<pbb>;
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
void setIO(string name = "")
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if (!name.empty())
{
(void)freopen((name + ".in").c_str(), "r", stdin);
(void)freopen((name + ".out").c_str(), "w", stdout);
}
}
const ld pi = 3.14159265358979323846;
const l LINF = 1e18;
const l INF = 1e9;
const l MOD = 1e9 + 7;
// const l MOD = 998244353;
const l MAXN = 2e5 + 5;
struct BIT
{
l n;
vl table;
BIT(l _n = MAXN)
{
n = _n;
table.assign(n + 1, 0);
}
BIT(const vl &a) : BIT(a.size())
{
n = a.size();
for (l i = 1; i <= n; ++i)
table[i] = a[i - 1];
for (l i = 1; i <= n; ++i)
{
l j = i + (i & -i);
if (j <= n)
table[j] += table[i];
}
// for (int i = 0; i < a.size(); ++i)
// update(i + 1, a[i]);
}
void update(l i, l delta)
{
while (i <= n)
{
table[i] += delta;
i += (i & -i);
}
}
l sum(l i)
{
l s = 0;
while (i != 0)
{
s += table[i];
i -= (i & -i);
}
return s;
}
l rangeSum(l i, l j)
{
return sum(j) - sum(i - 1);
}
};
int n, q;
vector<BIT> bit;
vi coord;
int getX(int x)
{
return lower_bound(all(coord), x) - coord.begin() + 1;
}
void update2D(int cx, int A)
{
for (int i = A; i <= n; i += i & -i)
bit[i].update(cx, 1);
}
int sum2D(int A, int Y)
{
int sum = 0;
for (int i = A; i; i -= i & -i)
sum += bit[i].sum(Y);
return sum;
}
int query2D(int Y, int B)
{
if (sum2D(n, Y) - sum2D(B - 1, Y) == 0)
return -1;
int cur = B - 1;
for (int k = 18; k >= 0; --k)
{
int nxt = cur + (1 << k);
if (nxt <= n && sum2D(nxt, Y) - sum2D(B - 1, Y) == 0)
cur = nxt;
}
return cur + 1;
}
void solve()
{
cin >> n >> q;
vector<tuple<bool, int, int>> query(q);
for (int i = 0; i < q; ++i)
{
char t;
int x, a;
cin >> t >> x >> a;
query[i] = {t == 'M', x, a};
coord.push_back(x);
}
sort(all(coord));
coord.erase(unique(all(coord)), coord.end());
int K = coord.size();
bit.assign(n + 1, BIT(K));
for (auto [t, x, a] : query)
if (t)
update2D(getX(x), a);
else
cout << query2D(getX(x), a) << "\n";
}
int main()
{
setIO("");
#ifndef ONLINE_JUDGE
// setIO("filename");
#endif
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
Compilation message (stderr)
deda.cpp: In function 'void setIO(std::string)':
deda.cpp:53:22: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | (void)freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
deda.cpp:54:22: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | (void)freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |