| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1306887 | chrisvilches | Plahte (COCI17_plahte) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
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;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
while (cin >> n >> m) {
vector<tuple<int, int, int>> events, points, ys;
vector<tuple<int, int, int, int>> rectangles;
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, 0, i);
events.emplace_back(x2, 2, ~i);
ys.emplace_back(y1, 0, ~i);
ys.emplace_back(y2, 2, i);
}
for (int i = 0; i < m; i++) {
int x, y, color;
cin >> x >> y >> color;
points.emplace_back(x, y, color);
ys.emplace_back(y, 1, i);
events.emplace_back(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 {
get<1>(points[i]) = comp_y;
}
comp_y++;
}
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), roots;
vector<vector<int>> graph(n);
vector<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) {
roots.emplace_back(i);
} else {
graph[curr_value].emplace_back(i);
}
} else {
const auto [_, y, color] = points[i];
const int curr_value = bit.query(y);
if (curr_value != -1) {
values[curr_value].emplace(color);
}
}
}
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 (const int r : roots) dfs(r);
for (const auto& x : res) {
cout << x << endl;
}
}
}
