QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#873886#3445. Numbers On a Treefernandes_queilaTL 85ms44464kbJava173.1kb2025-01-27 05:09:332025-01-27 05:09:33

Judging History

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

  • [2025-01-27 05:09:33]
  • 评测
  • 测评结果:TL
  • 用时:85ms
  • 内存:44464kb
  • [2025-01-27 05:09:33]
  • 提交

answer

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine().trim();
        String[] parts = line.split(" ");

        int height = Integer.parseInt(parts[0]);
        String road = (parts.length > 1) ? parts[1] : "";

        AVLTree avlTree = new AVLTree();
        int numNodes = (1 << (height + 1)) - 1;

        for (int i = numNodes; i > 0; i--) {
            avlTree.insertRoot(i);
        }

        System.out.println(avlTree.findNodeLabel(height, road));
        scanner.close(); // Fecha o leitor de entrada
    }
}

class TreeNode {
    int value;
    TreeNode left, right;
    int height;

    TreeNode(int value) {
        this.value = value;
        this.height = 1;
        this.left = this.right = null;
    }
}

class AVLTree {
    private TreeNode root;

    public AVLTree() {
        this.root = null;
    }

    private int getHeight(TreeNode node) {
        if (node == null) return 0;
        return node.height;
    }

    private void updateHeight(TreeNode node) {
        if (node != null) {
            node.height = 1 + Math.max(getHeight(node.left), getHeight(node.right));
        }
    }

    private int getBalanceFactor(TreeNode node) {
        if (node == null) return 0;
        return getHeight(node.left) - getHeight(node.right);
    }

    private TreeNode rotateRight(TreeNode y) {
        TreeNode x = y.left;
        TreeNode T2 = x.right;

        x.right = y;
        y.left = T2;

        updateHeight(y);
        updateHeight(x);

        return x;
    }

    private TreeNode rotateLeft(TreeNode x) {
        TreeNode y = x.right;
        TreeNode T2 = y.left;

        y.left = x;
        x.right = T2;

        updateHeight(x);
        updateHeight(y);

        return y;
    }

    private TreeNode balance(TreeNode node) {
        if (node == null) return null;

        updateHeight(node);
        int balance = getBalanceFactor(node);

        if (balance > 1) {
            if (getBalanceFactor(node.left) < 0) {
                node.left = rotateLeft(node.left);
            }
            return rotateRight(node);
        }

        if (balance < -1) {
            if (getBalanceFactor(node.right) > 0) {
                node.right = rotateRight(node.right);
            }
            return rotateLeft(node);
        }

        return node;
    }

    private TreeNode insert(TreeNode node, int value) {
        if (node == null) return new TreeNode(value);

        if (value < node.value) {
            node.left = insert(node.left, value);
        } else {
            node.right = insert(node.right, value);
        }

        return balance(node);
    }

    public void insertRoot(int value) {
        root = insert(root, value);
    }

    public int findNodeLabel(int height, String path) {
        int cur = 1;
        for (char c : path.toCharArray()) {
            if (c == 'L' || c == 'l') {
                cur = cur * 2;
            } else if (c == 'R' || c == 'r') {
                cur = cur * 2 + 1;
            }
        }

        int totalNodes = (1 << (height + 1)) - 1;
        return totalNodes - cur + 1;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 80ms
memory: 44336kb

input:

3 LR

output:

11

result:

ok single line: '11'

Test #2:

score: 0
Accepted
time: 82ms
memory: 43992kb

input:

3 RRL

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 85ms
memory: 44464kb

input:

2

output:

7

result:

ok single line: '7'

Test #4:

score: 0
Accepted
time: 82ms
memory: 44052kb

input:

3 LRL

output:

6

result:

ok single line: '6'

Test #5:

score: -100
Time Limit Exceeded

input:

26 RRLLLLRRRL

output:


result: