//#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << "[" << H << "]";
debug_out(T...);
}
#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
clock_t startTime;
double getCurrentTime() {
return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 998244353;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e3 + 1;
const int M = 3055;
void solve(int TC) {
int n, m;
cin >> n >> m;
vector<int> x1(n + m), x2(n + m), y1(n + m), y2(n + m);
vector<pair<int, int>> events;
vector<int> color(n + m);
for (int i = 0; i < n + m; i++){
cin >> x1[i] >> y1[i];
if (i < n){
cin >> x2[i] >> y2[i];
color[i] = 0;
} else {
x2[i] = x1[i], y2[i] = y1[i];
cin >> color[i];
}
events.emplace_back(i, 1);
events.emplace_back(i, 2);
}
function<bool(int, int)> contains = [&](int i, int j){
return x1[i] <= x1[j] && x2[j] <= x2[i] && y1[i] <= y1[j] && y2[j] <= y2[i];
};
sort(all(events), [&](pair<int, int> foo, pair<int, int> hoo){
int i = foo.first, j = hoo.first;
int ti = foo.second, tj = hoo.second;
int xi = (ti == 1 ? x1[i] : x2[i]), xj = (tj == 1 ? x1[j] : x2[j]);
return make_tuple(xi, ti, i >= n) < make_tuple(xj, tj, j >= n);
});
set<pair<int, int>> s;
vector<int> p(n + m, -1);
for (auto now : events){
int i = now.first, t = now.second;
// cout << i << ' ' << t << (i < n ? " rectangle\n" : " point\n");
if (t == 1){ // opening
auto it = s.lower_bound(MP(y1[i], -1));
if (it == s.end()){
p[i] = -1;
} else {
int j = it->second;
if (contains(j, i)){
p[i] = j;
} else {
p[i] = p[j];
}
}
s.insert(MP(y1[i], i));
s.insert(MP(y2[i], i));
} else { // closing
s.erase(MP(y1[i], i));
s.erase(MP(y2[i], i));
}
}
vector<vector<int>> g(n + m);
for (int i = 0; i < n + m; i++){
if (p[i] != -1) g[p[i]].push_back(i);
}
vector<int> ans(n + m);
vector<set<int>> sub(n + m);
function<void(int)> dfs = [&](int v){
int big = -1;
for (int u : g[v]){
dfs(u);
if (big == -1 || sub[u].size() > sub[big].size()) big = u;
}
if (big != -1){
swap(sub[v], sub[big]);
sub[big].clear();
}
sub[v].insert(color[v]);
for (int u : g[v]){
if (u != big){
for (int x : sub[u]){
sub[v].insert(x);
}
sub[u].clear();
}
}
ans[v] = (int)sub[v].size();
};
// for (int x : p) cout << x << ' '; cout << endl;
for (int i = 0; i < n; i++){
if (p[i] == -1) dfs(i);
}
for (int i = 0; i < n; i++) cout << ans[i] - 1 << '\n';
}
int main() {
startTime = clock();
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#ifdef dddxxz
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int TC = 1;
// cin >> TC;
for (int test = 1; test <= TC; test++) {
//debug(test);
//cout << "Case #" << test << ": ";
solve(test);
}
#ifdef dddxxz
cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
8156 KB |
Output is correct |
2 |
Correct |
97 ms |
8024 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
10380 KB |
Output is correct |
2 |
Correct |
99 ms |
9260 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
18056 KB |
Output is correct |
2 |
Correct |
191 ms |
14676 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
29196 KB |
Output is correct |
2 |
Correct |
307 ms |
22580 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
285 ms |
27224 KB |
Output is correct |
2 |
Correct |
301 ms |
22900 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |