1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <bits/stdc++.h> using namespace std;
const int MAXN = 2e5 + 4;
vector<int> ro[MAXN], co[MAXN]; struct Pot { int x, y, a; } p[MAXN]; int h,w,n, cn; int ans[MAXN * 4]; int du[MAXN * 4]; vector<vector<int>> G;
inline bool Cmp(const int & x, const int & y) { return p[x].a < p[y].a; }
void build(vector<int>& o) { sort(o.begin(), o.end(), Cmp); int l = 0, r = 0; int lastp = -1; for (int i = 1; i < o.size(); i++) { if (p[o[i]].a == p[o[i-1]].a) { r ++; if (lastp != -1) { G[o[i]].push_back(lastp); du[lastp] ++; } } else { lastp = ++cn; for (int j = l; j <= r; j++) { G[lastp].push_back(o[j]); du[o[j]] ++; } l = r = i; G[o[i]].push_back(lastp); du[lastp] ++; }
} }
int main() { scanf("%d%d%d",&h,&w,&n); G.resize(n * 4 + 5); cn = n; for (int i = 1; i <= n; i++) { scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].a); ro[p[i].x].push_back(i); co[p[i].y].push_back(i); } for (int i = 0; i < MAXN; i++) { if (!ro[i].empty()) { build(ro[i]); } if (!co[i].empty()) { build(co[i]); } } queue<int>q; for (int i = 1; i <= n; i++) { if (du[i] == 0) { q.push(i); ans[i] = 0; } } while (!q.empty()) { int x = q.front(); q.pop(); for (int to : G[x]) { du[to] --; if (du[to] <= 0) { ans[to] = ans[x] + 1; q.push(to); } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i] / 2); } return 0; }
|