#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 = -1;
bool operator<(const Point p) const { return x < p.x || (x == p.x && y < p.y); }
};
ostream& operator<<(ostream& os, const Point p) {
return os << "(" << p.x << ", " << p.y << ")";
}
string polygon_to_str(const vector<Point>& polygon) {
stringstream ss;
ss << fixed << setprecision(6);
ss << "polygon(";
for (size_t i = 0; i < polygon.size(); i++) {
if (i > 0) ss << ", ";
ss << polygon[i];
}
ss << ")";
return ss.str();
}
void print_rect(const tuple<int, int, int, int>& rect) {
const auto [x1, y1, x2, y2] = rect;
const Point a{x1, y1};
const Point b{x2, y1};
const Point c{x2, y2};
const Point d{x1, y2};
cerr << polygon_to_str({a, b, c, d}) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
while (cin >> n >> m) {
vector<tuple<int, 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;
// x1 *= 2;
// x2 *= 2;
// x2++;
rectangles.emplace_back(x1, y1, x2, y2);
events.emplace_back(x1, 0, i);
events.emplace_back(x2, 2, ~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;
// p.x *= 2;
points.emplace_back(p);
ys.emplace_back(p.y, 1, i);
events.emplace_back(p.x, 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);
vector<vector<int>> graph(n);
vector<unordered_set<int>> values(n);
for (const auto& [x, type, i] : events) {
if (type == 0 || type == 2) {
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);
graph[curr_value].emplace_back(i);
}
} else {
const Point p = points.at(i);
const int curr_value = bit.query(p.y);
if (curr_value == -1) continue;
values.at(curr_value).emplace(p.color);
}
}
vector<int> in_deg(n, 0);
for (const vector<int>& adj : graph) {
for (const int v : adj) {
in_deg[v]++;
}
}
vector<int> res(n);
const function<void(int)> dfs = [&](const int u) {
for (const int v : graph[u]) dfs(v);
for (const int v : graph[u]) {
if (values[u].size() < values[v].size()) {
values[u].swap(values[v]);
}
for (const int c : values[v]) {
values[u].emplace(c);
}
}
res[u] = values[u].size();
};
for (int i = 0; i < n; i++) {
if (in_deg[i] != 0) continue;
dfs(i);
}
for (const auto& x : res) {
cout << x << 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... |