平衡树板子题,虽然似乎有着高妙的树状数组搞法,但本着用用pb_ds这种bug库的存在,就没管了orz
#include <cstdio> #include <ext/pb_ds/assoc_container.hpp> #include <algorithm> const int N = 2e6 + 100; int n, m; __gnu_pbds::tree<int, __gnu_pbds::null_type, std::less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> pos; int val[N], used[N], ans[N]; int main() { scanf("%d%d", &n, &m); for (int i = m + 1; i <= n + m; ++i) pos.insert(i); for (int i = 1; i <= m; ++i) { int x, y; scanf("%d%d", &x, &y); auto p = pos.find_by_order(y - 1); pos.erase(p); int t = *p; if (!val[t]) { ans[t - m] = x; if (used[x]) return puts("-1"), 0; used[x] = true; val[m - i] = x; t = m - i; } else { if (val[t] != x) return puts("-1"), 0; val[m - i] = val[t]; t = m - i; } pos.insert(t); } int t = 1; for (int i = 1; i <= n; ++i) if (!ans[i]) { while (used[t]) ++t; ans[i] = t++; } for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); puts(""); return 0; }
时间: 2024-10-28 22:20:56