Editorial for Missing Blueprint


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
class SegTree:
    tree = []
    def __init__(self, data) -> None:
        self.tree = [0]*(4*len(data))
        self.build(data, 0, 0, len(data)-1)

    def build(self, data, v, tl, tr):
        if tl == tr:
            self.tree[v] = data[tl]
        else:
            tm = (tl+tr)//2
            self.build(data, 2*v+1, tl, tm)
            self.build(data, 2*v+2, tm+1, tr)
            self.tree[v] = self.tree[2*v+1] + self.tree[2*v+2]

    def update(self, v, tl, tr, pos, new_val):
        if tl == tr:
            self.tree[v] = new_val
        else:
            tm = (tl+tr)//2
            if pos <= tm:
                self.update(2*v+1, tl, tm, pos, new_val)
            else:
                self.update(2*v+2, tm+1, tr, pos, new_val)
            self.tree[v] = self.tree[2*v+1] + self.tree[2*v+2]

    def query(self, v, tl, tr, value):
        if tl == tr:
            return tl
        tm = (tl+tr)//2
        leftChildIndex = 2*v+1
        if(self.tree[leftChildIndex]<=value):
            return self.query(2*v+2, tm+1, tr, value-self.tree[leftChildIndex])
        return self.query(2*v+1, tl, tm, value)

t = int(input())
for _ in range(t):
    n, _ = list(map(int, input().split()))
    sp = list(map(int, input().split()))
    tree = SegTree([1]*n)
    # print(tree.tree)
    b = [0]*n
    for i in range(n-1, -1, -1):
        b[i] = tree.query(0, 0, n-1, sp[i])
        tree.update(0, 0, n-1, b[i], 0)
    # print(*b)
    bmap = {}
    for i in range(n):
        bmap[b[i]] = i
    sol = sorted(list(bmap.items()))
    flattenedB = [0]*n
    lastx = 0
    curval = 1
    for i,x in sol:
        if lastx>x:
            curval+=1
        lastx = x
        flattenedB[x] = curval
    print(*flattenedB)