#include <algorithm>
#include <iostream>
#include <vector>
#include <stack>
#include <set>
#define ii pair <int, int>
#define iii pair <ii, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 1;
int n, m, ans[N], par[N];
set <int> s[N];
vector <int> v[N], Y;
vector <iii> events;
bool IsRoot[N];
struct SegmentTree {
int val[N << 2], lazy[N << 2];
void change (int id, int VAL) {
val[id] = lazy[id] = VAL;
}
void down(int id) {
if (lazy[id] == -1) return;
change(id << 1, lazy[id]);
change(id << 1 | 1, lazy[id]);
lazy[id] = -1;
}
void update(int l, int r, int A, int b, int VAL, int id) {
if (l > b || r < A) return;
if (l >= A && r <= b) {
val[id] = lazy[id] = VAL;
return;
}
int mid = (l + r) >> 1;
down(id);
update(l, mid, A, b, VAL, id << 1);
update(mid + 1, r, A, b, VAL, id << 1 | 1);
}
int get(int l, int r, int A, int id) {
if (l == r) return val[id];
int mid = (l + r) >> 1;
down(id);
if (A <= mid) return get(l, mid, A, id << 1);
return get(mid + 1, r, A, id << 1 | 1);
}
} seg;
struct rect {
int x1, y1, x2, y2;
int id, k;
void input(int i) {
if (i <= n) {
cin >> x1 >> y1 >> x2 >> y2;
k = -1;
} else {
cin >> x1 >> y1 >> k;
x2 = x1;
y2 = y1;
}
Y.push_back(y1);
Y.push_back(y2);
id = i;
}
bool in (int l, int r, int u) {
return u >= l && u <= r;
}
bool rect_in (rect o) {
return in(x1, x2, o.x1) && in(x1, x2, o.x2) && in(y1, y2, o.y1) && in(y1, y2, o.y2);
}
} a[N];
bool cmp (rect x, rect y) {
if (x.rect_in(y)) return true;
if (y.rect_in(x)) return false;
return ii(x.x1, x.y1) < ii(y.x1, y.y2);
}
int GetID(int p) {
return lower_bound(Y.begin(), Y.end(), p) - Y.begin() + 1;
}
void DFS(int u) {
if (a[u].k != -1) {
s[u].insert(a[u].k);
}
for (int z: v[u]) {
DFS(z);
if (s[u].size() > s[z].size()) s[u].swap(s[z]);
for (int j: s[z]) s[u].insert(j);
s[z].clear();
}
ans[a[u].id] = s[u].size();
}
int main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n + m; ++i) {
a[i].input(i);
IsRoot[i] = true;
}
sort(Y.begin(), Y.end());
Y.erase(unique(Y.begin(), Y.end()), Y.end());
sort(a + 1, a + n + m + 1, cmp);
for (int i = 1; i <= n + m; ++i) {
events.push_back(iii(ii(a[i].x1, a[i].y1), i));
events.push_back(iii(ii(a[i].x2, a[i].y2), -i));
}
sort(events.begin(), events.end());
int j = 0;
for (int i = 1; i <= n + m; ++i) {
while (j < events.size() && events[j].fi < ii(a[i].x1, a[i].y1)) {
int cur = abs(events[j].se);
if (events[j].se > 0) {
seg.update(1, Y.size(), GetID(a[cur].y1), GetID(a[cur].y2), cur, 1);
} else {
int ph = par[cur];
if (ph != 0) seg.update(1, Y.size(), GetID(a[ph].y1), GetID(a[ph].y2), ph, 1);
}
++j;
}
int k = seg.get(1, Y.size(), GetID(a[i].y1), 1);
if (k != 0) {
par[i] = k;
v[k].push_back(i);
IsRoot[i] = false;
}
}
for (int i = 1; i <= n + m; ++i) {
if (IsRoot[i]) {
DFS(i);
}
}
for (int i = 1; i <= n; ++i) {
cout << ans[i] << "\n";
}
return 0;
}
# | 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... |