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
| #include<bits/stdc++.h>
using namespace std;
template<typename T>void read(T&x){x=0;int fl=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') fl=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}x*=fl;} template<typename T,typename...Args>inline void read(T&t,Args&...args){read(t);read(args...);}
typedef long long LL; typedef vector<int> vi; typedef pair<int, int> pii; #define mp(x, y) make_pair(x, y) #define pb(x) emplace_back(x)
const int N = 200005;
int n, pos[N], ans[N], f[N]; struct Node{ int l, r, id; bool operator < (const Node &A) const{ return l == A.l ? r < A.r : l < A.l; } }a[N];
set<pii> s;
struct segTree{ int l, r, mx, idx; }tr[N<<2]; #define lid id<<1 #define rid id<<1|1 #define mid ((tr[id].l + tr[id].r) >> 1) inline void pushup(int id){ if(tr[lid].mx > tr[rid].mx){ tr[id].mx = tr[lid].mx; tr[id].idx = tr[lid].idx; } else if(tr[rid].mx >= tr[lid].mx){ tr[id].mx = tr[rid].mx; tr[id].idx = tr[rid].idx; } } void build(int id, int l, int r){ tr[id].l = l, tr[id].r = r; tr[id].mx = tr[id].idx = 0; if(l == r) return; build(lid, l, mid); build(rid, mid+1, r); pushup(id); } void modify(int id, int pos, int val){ if(tr[id].l == tr[id].r){ tr[id].mx = val; tr[id].idx = tr[id].l; return; } if(pos <= mid) modify(lid, pos, val); else modify(rid, pos, val); pushup(id); } pii query(int id, int l, int r){ if(l > r) return mp(-1, -1); if(tr[id].l == l && tr[id].r == r) return mp(tr[id].mx, tr[id].idx); if(r <= mid) return query(lid, l, r); else if(l > mid) return query(rid, l, r); else{ pii resl = query(lid, l, mid); pii resr = query(rid, mid+1, r); if(resl.first >= resr.first) return resl; else return resr; } }
int main(){ read(n); for(int i = 1; i <= n; i++){ read(a[i].l, a[i].r); a[i].id = i; } sort(a+1, a+n+1); int now = 0; for(int i = 1; i <= n; i++){ while(now < n && a[now+1].l <= i){ now++; s.emplace(mp(a[now].r, now)); } while(!s.empty() && s.begin() -> first < i) s.erase(s.begin()); pos[i] = s.begin() -> second; f[s.begin() -> second] = i; s.erase(s.begin()); } for(int i = 1; i <= n; i++) ans[a[pos[i]].id] = i; bool ok = true; now = 0; int mark1 = 0, mark2 = 0; build(1, 1, n); for(int i = 1; i <= n; i++) modify(1, f[i], a[i].r); for(int i = 1; i <= n; i++){ pii res = query(1, a[i].l, f[i]-1); if(res.first >= f[i]){ ok = false; mark1 = a[pos[res.second]].id; mark2 = a[i].id; break; } } if(ok){ puts("YES"); for(int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; } else{ puts("NO"); for(int i = 1; i <= n; i++) printf("%d ", ans[i]); puts(""); swap(ans[mark1], ans[mark2]); for(int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; } }
|