QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#873889#3445. Numbers On a Treefernandes_queilaTL 12ms8832kbPython32.5kb2025-01-27 07:01:312025-01-27 07:01:32

Judging History

你现在查看的是最新测评结果

  • [2025-01-27 07:01:32]
  • 评测
  • 测评结果:TL
  • 用时:12ms
  • 内存:8832kb
  • [2025-01-27 07:01:31]
  • 提交

answer

class TreeNode:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None
        self.height = 1

class AVLTree:
    def __init__(self):
        self.root = None

    def get_height(self, node):
        if not node:
            return 0
        return node.height

    def update_height(self, node):
        if node:
            node.height = 1 + max(self.get_height(node.left), self.get_height(node.right))

    def balance_factor(self, node):
        if not node:
            return 0
        return self.get_height(node.left) - self.get_height(node.right)

    def rotate_right(self, y):
        x = y.left
        temp = x.right
        x.right = y
        y.left = temp
        self.update_height(y)
        self.update_height(x)
        return x

    def rotate_left(self, x):
        y = x.right
        temp = y.left
        y.left = x
        x.right = temp
        self.update_height(x)
        self.update_height(y)
        return y

    def balance(self, node):
        if not node:
            return node

        self.update_height(node)
        balance = self.balance_factor(node)

        if balance > 1:
            if self.balance_factor(node.left) < 0:
                node.left = self.rotate_left(node.left)
            return self.rotate_right(node)

        if balance < -1:
            if self.balance_factor(node.right) > 0:
                node.right = self.rotate_right(node.right)
            return self.rotate_left(node)

        return node

    def insert(self, node, value):
        if not node:
            return TreeNode(value)
        if value < node.value:
            node.left = self.insert(node.left, value)
        else:
            node.right = self.insert(node.right, value)
        return self.balance(node)

    def insert_root(self, value):
        self.root = self.insert(self.root, value)

    def construct_avl(self, height):
        total_nodes = (1 << (height + 1)) - 1
        for i in range(total_nodes, 0, -1):
            self.insert_root(i)

    def find_node_label(self, height, path):
        cur = 1
        for c in path:
            cur = cur * 2 + (c == 'R')
        total_nodes = (1 << (height + 1)) - 1
        return total_nodes - cur + 1

if __name__ == "__main__":
    import sys
    input = sys.stdin.read
    data = input().strip().split()

    height = int(data[0])  
    path = data[1] if len(data) > 1 else "" 

    avl_tree = AVLTree()
    avl_tree.construct_avl(height)
    result = avl_tree.find_node_label(height, path)
    print(result)

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 8832kb

input:

3 LR

output:

11

result:

ok single line: '11'

Test #2:

score: 0
Accepted
time: 12ms
memory: 8832kb

input:

3 RRL

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 10ms
memory: 8832kb

input:

2

output:

7

result:

ok single line: '7'

Test #4:

score: 0
Accepted
time: 10ms
memory: 8832kb

input:

3 LRL

output:

6

result:

ok single line: '6'

Test #5:

score: -100
Time Limit Exceeded

input:

26 RRLLLLRRRL

output:


result: