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
|
若左子树的空格数大于等于要放入的位置,则搜索左子树,(说明可以插入) 否则,搜索右子树,(说明前面已经被插入了只能往后面退了)由于左边有一些空位置,只需要搜索右子树的部分空位置,使左右空位置之和为要搜索的位置。 */
#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define Lson(x) (x<<1) #define Rson(x) (x<<1|1) #define Mid(x, y) ((x + y) >> 1)
#define MAXN 200005
int que[MAXN];
struct Node{ int sum; };
Node node[MAXN << 2];
void push_up(int pos) { node[pos].sum = node[Lson(pos)].sum + node[Rson(pos)].sum; }
void build(int left, int right, int pos) { if (left == right) { node[pos].sum = 1; return; } int mid = Mid(left, right); build(left, mid, Lson(pos)); build(mid + 1, right, Rson(pos)); push_up(pos); }
void update(int index, int val, int left, int right, int pos) { if (left == right) { que[left] = val; node[pos].sum = 0; return; } int mid = Mid(left, right); if (node[Lson(pos)].sum >= index) update(index, val, left, mid, Lson(pos)); else update(index - node[Lson(pos)].sum, val, mid + 1, right, Rson(pos)); push_up(pos); }
int main() { int n, index[MAXN], val[MAXN], i;
while (scanf("%d", &n) != EOF) { build (1, n, 1); for (i = 1; i <= n; i++) scanf ("%d %d", &index[i], &val[i]); for (i = n; i > 0; i--) update(index[i] + 1, val[i], 1, n, 1); for (i = 1; i <= n; i++) printf ("%d ", que[i]); printf ("\n"); } return 0; }
|