树状数组练习_AcWing 260.买票
作者:
成理第一深情
,
2024-05-03 19:59:25
,
所有人可见
,
阅读 15
"""
思路参考:AcWing 244.谜一样的牛
从前往后看队列看不了,因为可能会被插队,但是从后往前推,队列是唯一确定的
二分:找到第一个x,使得query(x) == p[i]
"""
import sys
input = sys.stdin.readline
print = sys.stdout.write
def lowbit(x):
return x & -x
def change(x, c):
global tr, n
# print(str(n) + '\n')
while x <= n:
tr[x] += c
x += lowbit(x)
def query(x):
global tr
t = 0
while x:
t += tr[x]
x -= lowbit(x)
return t
flag = 1
while flag:
n = input().strip()
if n == '':
flag = 0
continue
n = int(n)
p = [0 for _ in range(n + 1)]
v = [0 for _ in range(n + 1)]
tr = [0 for _ in range(n + 1)] # 很坑,不能直接tr = [1 for _ in range(n + 1)],因为0这个位置不能初始化为1
for i in range(1, n + 1):
p[i], v[i] = map(int, input().strip().split())
p[i] += 1
change(i, 1)
# print(str(p) + '\n')
# print(str(v) + '\n')
ans = [0 for _ in range(n + 1)]
for i in range(n, 0, -1):
l, r = 1, n
while l < r:
mid = (l + r) // 2
if query(mid) >= p[i]:
r = mid
else:
l = mid + 1
ans[r] = v[i]
# print(str(r) + ' ' + str(v[i]) + '\n')
change(r, -1)
for i in range(1, n + 1):
print(str(ans[i]) + ' ')
print('\n')