Submission #1015817

#TimeUsernameProblemLanguageResultExecution timeMemory
1015817MilosMilutinovicPlahte (COCI17_plahte)C++14
160 / 160
244 ms73960 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<int> a(n), b(n), c(n), d(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i] >> c[i] >> d[i]; } vector<int> x(m), y(m), k(m); for (int i = 0; i < m; i++) { cin >> x[i] >> y[i] >> k[i]; } vector<int> xs; for (int i = 0; i < n; i++) { xs.push_back(b[i]); xs.push_back(d[i]); } for (int i = 0; i < m; i++) { xs.push_back(y[i]); } sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); int sz = (int) xs.size(); for (int i = 0; i < n; i++) { b[i] = (int) (lower_bound(xs.begin(), xs.end(), b[i]) - xs.begin()); d[i] = (int) (lower_bound(xs.begin(), xs.end(), d[i]) - xs.begin()); } for (int i = 0; i < m; i++) { y[i] = (int) (lower_bound(xs.begin(), xs.end(), y[i]) - xs.begin()); } vector<array<int, 3>> ev; for (int i = 0; i < n; i++) { ev.push_back({a[i], i, 0}); ev.push_back({c[i] + 1, ~i, 1}); } for (int i = 0; i < m; i++) { ev.push_back({x[i], n + i, 2}); } sort(ev.begin(), ev.end()); vector<vector<int>> g(n); vector<set<int>> col(n); int lst; vector<vector<int>> stk(8 * sz); function<void(int, int, int, int, int, int)> Insert = [&](int id, int l, int r, int ll, int rr, int v) { if (ll <= l && r <= rr) { stk[id].push_back(v); return; } int mid = (l + r) >> 1; if (rr <= mid) { Insert(id * 2, l, mid, ll, rr, v); } else if (ll > mid) { Insert(id * 2 + 1, mid + 1, r, ll, rr, v); } else { Insert(id * 2, l, mid, ll, rr, v); Insert(id * 2 + 1, mid + 1, r, ll, rr, v); } }; function<void(int, int, int, int, int)> Pop = [&](int id, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) { stk[id].pop_back(); return; } int mid = (l + r) >> 1; if (rr <= mid) { Pop(id * 2, l, mid, ll, rr); } else if (ll > mid) { Pop(id * 2 + 1, mid + 1, r, ll, rr); } else { Pop(id * 2, l, mid, ll, rr); Pop(id * 2 + 1, mid + 1, r, ll, rr); } }; function<void(int, int, int, int)> Query = [&](int id, int l, int r, int p) { if (!stk[id].empty()) { lst = stk[id].back(); } if (l == r) { return; } int mid = (l + r) >> 1; if (p <= mid) { Query(id * 2, l, mid, p); } else { Query(id * 2 + 1, mid + 1, r, p); } }; for (auto& p : ev) { int i = p[1]; if (p[2] == 0) { lst = -1; Query(1, 0, sz - 1, b[i]); Insert(1, 0, sz - 1, b[i], d[i], i); if (lst != -1) { g[lst].push_back(i); } } if (p[2] == 1) { i = ~i; Pop(1, 0, sz - 1, b[i], d[i]); } if (p[2] == 2) { i -= n; lst = -1; Query(1, 0, sz - 1, y[i]); if (lst != -1) { col[lst].insert(k[i]); } } } vector<bool> root(n, true); for (int i = 0; i < n; i++) { for (int j : g[i]) { root[j] = false; } } vector<int> res(n); vector<int> where(n); function<void(int)> Dfs = [&](int v) { where[v] = v; for (int u : g[v]) { Dfs(u); if (col[where[v]].size() < col[where[u]].size()) { swap(where[v], where[u]); } for (int c : col[where[u]]) { col[where[v]].insert(c); } col[where[u]].clear(); } res[v] = (int) col[where[v]].size(); }; for (int i = 0; i < n; i++) { if (root[i]) { Dfs(i); } } for (int i = 0; i < n; i++) { cout << res[i] << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...