Buy Tickets 线段树

来源:互联网 发布:sqlserver身份验证 编辑:程序博客网 时间:2024/06/07 14:09
因为后来的人插队会影响先来的人的位置
所以从后往前处理
最后一个人的位置是可以直接确定的,就是r[i]+1
因为对于前一个人来说,前一个人加入队列是后一个人是不存在的,所以在处理完后一个人后
要把后一个人占的位置给删去
#include <iostream>#include <algorithm>#include <string>#include <map>using namespace std;const int MAXN = 200005;typedef long long ll;struct Node {ll l, r, val;//val代表[l, r]区间剩余的数(位子)ll mid() { return (l + r) >> 1; }} Tree[MAXN * 4];void push_up(int rt){Tree[rt].val = Tree[rt << 1].val + Tree[rt << 1 | 1].val;}void buildTree(int rt, int l, int r){Tree[rt].l = l;Tree[rt].val = r - l + 1;Tree[rt].r = r;if (l == r){return;}ll m = Tree[rt].mid();buildTree(rt << 1, l, m);buildTree(rt << 1 | 1, m + 1, r);}ll ans;void query(int rt, int n){if (Tree[rt].l == Tree[rt].r){ans = Tree[rt].l;--Tree[rt].val;return;}ll m = Tree[rt].mid();if (n > Tree[rt << 1].val)query(rt << 1 | 1, n - Tree[rt << 1].val);elsequery(rt << 1, n);push_up(rt);}int arr[MAXN];int r[MAXN];int pos[MAXN];int main(void){freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);ios::sync_with_stdio(false);cin.tie(0);int n;while (cin >> n){buildTree(1, 1, n);for (int i = 0; i < n; ++i)cin >> r[i] >> arr[i];for (int i = n - 1; i >= 0; --i){query(1, r[i] + 1);pos[ans] = arr[i];}for (int i = 1; i <= n; ++i){if (i > 1)cout << " ";cout << pos[i];}cout << endl;}return 0;}