Submission #1200823

#TimeUsernameProblemLanguageResultExecution timeMemory
1200823GoBananas69Examination (JOI19_examination)C++20
41 / 100
110 ms15840 KiB
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct Query {
    int type, s, t, sum, idx;
};

struct segment_tree {
    vector<int> tree;
    int n;
    void build(int sz) {
        tree.resize(4 * sz, 0);
        n = sz;
    }
    void update(int i, int L, int R, int p) {
        if (L == R) {
            tree[i]++;
            return;
        }
        int m = (L + R) / 2;
        int x = 2 * i + 1, y = x + 1;
        if (p <= m) {
            update(x, L, m, p);
        } else {
            update(y, m + 1, R, p);
        }
        tree[i] = tree[x] + tree[y];
    }
    void update(int p) {
        update(0, 0, n - 1, p);
    }
    int query(int i, int L, int R, int l, int r) {
        if (L == l && r == R) {
            return tree[i];
        }
        int m = (L + R) / 2;
        int x = 2 * i + 1, y = x + 1;
        if (r <= m) {
            return query(x, L, m, l, r);
        } else if (l > m) {
            return query(y, m + 1, R, l, r);
        } else {
            int q1 = query(x, L, m, l, m);
            int q2 = query(y, m + 1, R, m + 1, r);
            return q1 + q2;
        }
    }
    int query(int l, int r) {
        return query(0, 0, n - 1, l, r);
    }
} A, B;

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<Query> x(n + k), y(n + k);
    for (int i = 0; i < n; ++i) {
        int s, t;
        cin >> s >> t;
        x[i] = {0, s, t, s + t, -1};
        y[i] = {0, s, t, s + t, -1};
    }
    for (int i = n; i < n + k; ++i) {
        int s, t, sum;
        cin >> s >> t >> sum;
        x[i] = {1, s, t, max(sum, s + t), i - n};
        y[i] = {1, s, t, max(sum, s + t), i - n};
    }
    sort(x.begin(), x.end(), [](const Query &a, const Query &b) {
        if (a.s != b.s) return a.s > b.s;
        return a.type < b.type;
    });
    sort(y.begin(), y.end(), [](const Query &a, const Query &b) {
        if (a.t != b.t) return a.t < b.t;
        return a.type > b.type;
    });
    // for (const auto &q : x) {
    //     cout << q.type << " " << q.s << " " << q.t << " " << q.sum << " " << q.idx << '\n';
    // }
    // cout << '\n';
    // for (const auto &q : y) {
    //     cout << q.type << " " << q.s << " " << q.t << " " << q.sum << " " << q.idx << '\n';
    // }
    int sz = 0;
    for (Query &q : x) {
        sz = max({sz, q.s, q.t, q.sum});
    }
    A.build(sz + 1);
    B.build(sz + 1);
    vector<int> ac(k), bc(k);
    for (Query &q : x) {
        if (q.type == 0) {
            A.update(q.sum);
        } else if (q.type == 1) {
            ac[q.idx] = A.query(q.sum, sz);
        }
    }
    for (Query &q : y) {
        if (q.type == 0) {
            B.update(q.sum);
        } else if (q.type == 1) {
            bc[q.idx] = B.query(q.sum, sz);
        }
    }
    for (int i = 0; i < k; ++i) {
        cout << ac[i] - bc[i] << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...