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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| #include <bits/stdc++.h>
using namespace std; typedef pair<int, int> pii; const int N = 1e5 + 10;
struct node{ int x, tp, w, y, id; bool operator < (const node &p) const{ if(x == p.x){ if(tp != p.tp) return tp < p.tp; if(tp == p.tp){ return y > p.y; } } return x < p.x; } }a[N];
int ans[N], ok, pos[N], wi[N]; pii q[N]; vector<int> e[N];
void gao(int u, int l, int r, int w){ int son = 0; sort(e[u].begin(), e[u].end(), [&](int x, int y){ return q[x] < q[y]; }); for(int v : e[u]) son ^= wi[v]; if(son == w){ for(int i = l; i <= r; i++){ if(!ans[i]){ ans[i] = i; pos[i] = i; } } return; } if(l == r){ ok = 0; return; } int pt = -1; for(int i = l; i <= r; i++){ if(!ans[i]){ ans[i] = i; pos[i] = i; pt = i; } } if(pt == -1){ if(e[u].size() < 2){ ok = 0; return; } int mx = 0, mn = 1e6, u0 = e[u][0], u1 = e[u][1]; for(int i = q[u0].first; i <= q[u0].second; i++){ mx = max(mx, ans[i]); } for(int i = q[u1].first; i <= q[u1].second; i++){ mn = min(mn, ans[i]); } swap(ans[pos[mx]], ans[pos[mn]]); swap(pos[mx], pos[mn]); return; } for(int i = -1; i <= 1; i += 2){ if(pos[pt + i] >= l && pos[pt + i] <= r){ swap(ans[pos[pt + i]], ans[pos[pt]]); swap(pos[pt + i], pos[pt]); return; } } }
void solve(){ int n, m, tot = 0; ok = 1; cin >> n >> m; map<pii, int> mp; for(int i = 1, l, r, w; i <= m; i++){ cin >> l >> r >> w; wi[i] = w; if(!mp.count({l, r})){ mp[{l, r}] = w; q[i] = {l, r}; a[++tot] = {l, 0, w, r, i}; a[++tot] = {r, 1, w, l, i}; }else{ if(w != mp[{l, r}]) ok = 0; } } sort(a + 1, a + tot + 1); vector<node> stk; for(int i = 1; i <= tot; i++){ if(a[i].tp == 0){ stk.push_back(a[i]); }else{ node lt = stk.back(); stk.pop_back(); if(!stk.empty()) { int fa = stk.back().id; e[fa].push_back(lt.id); } gao(lt.id, lt.x, lt.y, lt.w); } } if(!ok){ cout << "-1\n"; return; } for(int i = 1; i <= n; i++){ if(!ans[i]) ans[i] = i; cout << ans[i] << " \n"[i == n]; } }
int main(){ int T = 1; ios::sync_with_stdio(false);
while(T--) solve(); return 0; }
|