#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
struct BIT {
BIT(const int size) : n(size + 1), A(n, 0) {}
void range_update(const int i, const int j, const int v) {
update(i, v);
update(j + 1, -v);
}
int query(int i) const {
i++;
int sum = 0;
for (; i > 0; i -= i & -i) sum += A[i];
return sum;
}
private:
const int n;
vector<int> A;
void update(int i, const int v) {
i++;
for (; i < n; i += i & -i) A[i] += v;
}
};
struct Point {
int x, y, color;
bool operator<(const Point p) const { return x < p.x || (x == p.x && y < p.y); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
while (cin >> n >> m) {
vector<pair<int, int>> events;
vector<tuple<int, int, int, int>> rectangles;
vector<Point> points;
vector<tuple<int, int, int>> ys;
for (int i = 0; i < n; i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
rectangles.emplace_back(x1, y1, x2, y2);
events.emplace_back(x1, i);
events.emplace_back(x2, ~i);
ys.emplace_back(y1, 0, ~i);
ys.emplace_back(y2, 2, i);
assert(y1 < y2);
}
for (int i = 0; i < m; i++) {
Point p;
cin >> p.x >> p.y >> p.color;
points.emplace_back(p);
ys.emplace_back(p.y, 1, i);
}
sort(ys.begin(), ys.end());
int comp_y = 0;
for (const auto& [y, type, i] : ys) {
if (type == 0 || type == 2) {
if (i < 0) {
get<1>(rectangles[~i]) = comp_y;
} else {
get<3>(rectangles[i]) = comp_y;
}
} else {
points.at(i).y = comp_y;
}
comp_y++;
}
// sort(points.begin(), points.end());
sort(events.begin(), events.end());
const int bit_size = comp_y + 1;
BIT bit(bit_size);
bit.range_update(0, bit_size - 1, -1);
vector<int> delta(n);
// size_t k = 0;
vector<vector<int>> graph(n);
for (const auto& [x, i] : events) {
const auto [_, y1, __, y2] = rectangles[i < 0 ? ~i : i];
if (i < 0) {
bit.range_update(y1, y2, -delta[~i]);
continue;
}
const int curr_value = bit.query(y1);
delta[i] = i - curr_value;
bit.range_update(y1, y2, delta[i]);
if (curr_value == -1) {
// Doesn't point to any other node.
} else {
graph[i].emplace_back(curr_value);
}
}
// TODO: DFS and collect results. Warning: It's a DAG, will it get TLE?? Do a DP from
// the leaves?
vector<int> in_deg(n, 0);
for (const vector<int>& adj : graph) {
for (const int v : adj) {
in_deg[v]++;
}
}
set<Point> bullets{points.begin(), points.end()};
vector<set<int>> res(n);
const function<void(int)> dfs = [&](const int u) {
vector<Point> inside;
const auto [x1, y1, x2, y2] = rectangles[u];
for (const Point b : bullets) {
if (x1 <= b.x && b.x <= x2 && y1 <= b.y && b.y <= y2) {
inside.emplace_back(b);
res[u].emplace(b.color);
}
}
for (const Point b : inside) {
assert(bullets.count(b));
bullets.erase(b);
}
for (const int v : graph[u]) {
for (const int c : res[u]) {
res[v].emplace(c);
}
dfs(v);
}
};
for (int i = 0; i < n; i++) {
if (in_deg[i] != 0) continue;
dfs(i);
}
for (const set<int>& x : res) {
cout << x.size() << endl;
}
}
}
| # | 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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |