#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
template<class X, class Y> bool maximize (X &x, const Y &y) {return x < y ? x = y, 1 : 0;}
template<class X, class Y> bool minimize (X &x, const Y &y) {return x > y ? x = y, 1 : 0;}
const int maxn = 2e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int n, q;
pii a[maxn];
pair<pair<int, int>, int> queries[maxn];
int BIT[maxn];
void upd(int x, int val) {for (; x <= n; x += x & (-x)) BIT[x] += val;}
int query(int x)
{
int res = 0;
for (; x; x -= x & (-x)) res += BIT[x];
return res;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> q;
vector<int> comp;
for (int i = 0; i < n; ++i)
{
cin >> a[i].fi >> a[i].se;
comp.pb(a[i].se);
}
for (int i = 0; i < q; ++i)
{
cin >> queries[i].fi.fi >> queries[i].fi.se;
queries[i].se = i;
}
sort(all(comp));
comp.erase(unique(all(comp)), comp.end());
for (int i = 0; i < n; ++i) a[i].se = lower_bound(all(comp), a[i].se) - comp.begin() + 1;
sort(a, a+n, [](pii &lhs, pii &rhs) {return (lhs.fi == rhs.fi) ? lhs.se < rhs.se : lhs.fi > rhs.fi;});
sort(queries, queries+q, greater<pair<pair<int, int>, int>>());
vector<int> ans(q);
multiset<int> stacks;
int idx = 0;
for (int i = 0; i < q; ++i)
{
while (idx < n && a[idx].fi >= queries[i].fi.fi)
{
upd(a[idx].se, 1);
//cout << 1 << " at " << a[idx].se << '\n';
auto it = stacks.upper_bound(a[idx].se);
if (it == stacks.end()) stacks.insert(a[idx].se);
else
{
upd(*it, -1);
//cout << -1 << " at " << *it << '\n';
stacks.erase(it);
stacks.insert(a[idx].se);
}
++idx;
}
ans[queries[i].se] = query(upper_bound(all(comp), queries[i].fi.se) - comp.begin());
}
for (auto i : ans) cout << i << '\n';
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |