제출 #216208

#제출 시각아이디문제언어결과실행 시간메모리
216208DS007Examination (JOI19_examination)C++14
100 / 100
2233 ms161440 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define mid (l + r) / 2
#define child v * 2
#define index_set tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>

struct query {
    int x, y, z, i, ans;

    query() {
        x = y = z = i = ans = -1;
    }
};

const int N = 1e5 + 5;
int a[N], b[N];
pair<int, int> s[N];
query qry[N];
int n, q;

index_set t[N * 4];
multimap<int, int> m;

void update(int v, int l, int r, int tl, int val) {
    t[v].insert({val, tl});
    if (l == r && tl == l)
        return;
    else if (tl <= mid)
        update(child, l, mid, tl, val);
    else
        update(child + 1, mid + 1, r, tl, val);
}

int query(int v, int l, int r, int tl, int tr, int val) {
    if (tl > tr)
        return 0;
    else if (l == tl && r == tr)
        return t[v].size() - t[v].order_of_key({val, -1});
    return query(child, l, mid, tl, min(mid, tr), val) + query(child + 1, mid + 1, r, max(mid + 1, tl), tr, val);
}

void solveTestCase() {
    cin >> n >> q;
    for (int i = 0; i < n; i++)
        cin >> s[i].first >> s[i].second, a[i] = s[i].first, b[i] = s[i].second;
    for (int i = 0; i < q; i++)
        cin >> qry[i].x >> qry[i].y >> qry[i].z, qry[i].i = i;

    sort(a, a + n, greater<>());
    sort(b, b + n);
    sort(s, s + n, greater<>());

    sort(qry, qry + q, [](auto &q1, auto &q2) {
        return q1.x > q2.x;
    });

    for (int i = 0; i < n; i++)
        m.insert({b[i], i});

    for (int i = 0, p = 0; i < q; i++) {
        while (p != n && s[p].first >= qry[i].x) {
            auto itr = m.find(s[p].second);
            update(1, 0, n - 1, itr->second, s[p].first + s[p].second);
            m.erase(itr);
            p++;
        }

        int tl = lower_bound(b, b + n, qry[i].y) - b;
        qry[i].ans = query(1, 0, n - 1, tl, n - 1, qry[i].z);
    }

    sort(qry, qry + q, [](auto &q1, auto &q2) {
        return q1.i < q2.i;
    });

    for (int i = 0; i < q; i++)
        cout << qry[i].ans << "\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int test = 1;
    // cin >> test;
    while (test--)
        solveTestCase();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...