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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <bits/stdc++.h> #define fst first #define snd second #define re register using namespace std; typedef pair<int,int> pii; const int N = 1e6 + 10,inf = 1e9 + 10; int n,H[2]; int d[N],rt[N],dp[N]; int id,idx,h[N],ne[N],e[N],w[N]; vector<pii> v[2][N]; inline int read(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 1) + (r << 3) + (c ^ 48); c = getchar(); } return r * w; } inline void add(int a,int b,int c){ d[b]++; ne[idx] = h[a]; e[idx] = b; w[idx] = c; h[a] = idx++; } inline void f(int ty){ for (re int i = 1;i <= H[ty];i++){ sort(v[ty][i].begin(),v[ty][i].end()); int len = v[ty][i].size(); for (re int j = 0;j < len;j++){ int p = v[ty][i][j].snd; if (!j) rt[p] = ++id; else{ if (v[ty][i][j].fst == v[ty][i][j - 1].fst) rt[p] = rt[v[ty][i][j - 1].snd]; else rt[p] = ++id; } add(p,rt[p],0); } for (auto x:v[ty][i]){ auto it = upper_bound(v[ty][i].begin(),v[ty][i].end(),make_pair(x.fst,inf)); if (it == v[ty][i].end()) break; int a = rt[(*it).snd],b = x.snd; add(a,b,1); } } } inline void top_sort(){ queue<int> q; for (re int i = 1;i <= id;i++){ if (!d[i]){ dp[i] = 0; q.push(i); } } while (!q.empty()){ int t = q.front(); q.pop(); for (re int i = h[t];~i;i = ne[i]){ int j = e[i]; dp[j] = max(dp[j],dp[t] + w[i]); d[j]--; if (!d[j]) q.push(j); } } } int main(){ memset(h,-1,sizeof(h)); memset(dp,-1,sizeof(dp)); for (re int i = 0;i <= 1;i++) H[i] = read(); n = id = read(); for (re int i = 1;i <= n;i++){ int x,y,val; x = read(); y = read(); val = read(); v[0][x].push_back({val,i}); v[1][y].push_back({val,i}); } f(0); f(1); top_sort(); for (re int i = 1;i <= n;i++) printf("%d\n",dp[i]); return 0; }
|