QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#873923#3445. Numbers On a Treefernandes_queilaTL 82ms44408kbJava171.7kb2025-01-27 09:10:482025-01-27 09:10:50

Judging History

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

  • [2025-01-27 09:10:50]
  • 评测
  • 测评结果:TL
  • 用时:82ms
  • 内存:44408kb
  • [2025-01-27 09:10:48]
  • 提交

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] : "";

        BalancedTree balancedTree = new BalancedTree(); 
        balancedTree.constructAVL(height); 

        int result = balancedTree.searchNode(height, road);
        System.out.println(result);

        scanner.close(); 
    }
}

class TreeNode {
    int value;
    TreeNode left, right;

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

class BalancedTree {
    private TreeNode root;

    public void constructAVL(int height) {
        int totalNodes = (1 << (height + 1)) - 1;
        root = buildTree(1, totalNodes);
    }

    private TreeNode buildTree(int start, int end) {
        if (start > end) return null;
        int mid = (start + end) / 2;
        TreeNode node = new TreeNode(mid);
        node.left = buildTree(start, mid - 1);
        node.right = buildTree(mid + 1, end);
        return node;
    }

    public int searchNode(int height, String road) {
        int rootLabel = (1 << (height + 1)) - 1;

        if (road.isEmpty()) {
            return rootLabel;
        }

        int label = rootLabel;
        int local = 0;

        for (char move : road.toCharArray()) {
            if (Character.toLowerCase(move) == 'l') {
                local = 2 * local + 1;
            } else if (Character.toLowerCase(move) == 'r') {
                local = 2 * local + 2;
            }
        }

        label -= local;
        return label;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 79ms
memory: 44368kb

input:

3 LR

output:

11

result:

ok single line: '11'

Test #2:

score: 0
Accepted
time: 80ms
memory: 44140kb

input:

3 RRL

output:

2

result:

ok single line: '2'

Test #3:

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

input:

2

output:

7

result:

ok single line: '7'

Test #4:

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

input:

3 LRL

output:

6

result:

ok single line: '6'

Test #5:

score: -100
Time Limit Exceeded

input:

26 RRLLLLRRRL

output:


result: