⚡ AI-Powered Developer Copilot

Your Invisible AI That Solves Code at the Speed of Thought

Oppex sits invisibly on your screen and instantly analyzes any algorithm, code block, or technical problem — without ever switching tabs or breaking your focus. Privacy-first. Zero trace. Always ready.

50K+ Developers
95% Success Rate
24/7 Support
https://www.hackerrank.com/challenges/tree-queries
Problem
Submissions
Leaderboard
Discussions

Taylor loves trees, and this new challenge has him stumped!

Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it.

A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers (i, j) such that the following four conditions are all satisfied:

  • i ≠ j
  • i ∈ the path from node w to node x.
  • j ∈ path from node y to node z.
  • ci = cj

Given t and q queries, process each query in order, printing the pair count for each query on a new line.

Input Format

The first line contains two space-separated integers describing the respective values of n (the number of nodes) and q (the number of queries).

The second line contains n space-separated integers describing the respective values of each node (i.e., c1, c2, ..., cn).

Each of the n - 1 subsequent lines contains two space-separated integers, u and v, defining a bidirectional edge between nodes u and v.

Each of the q subsequent lines contains a w x y z query, defined above.

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ q ≤ 50000
  • 1 ≤ ci ≤ 109
  • 1 ≤ u, v, w, x, y, z ≤ n

Scoring for this problem is Binary, that means you have to pass all the test cases to get a positive score.

Output Format

For each query, print the count of ordered pairs of integers satisfying the four given conditions on a new line.

Change Theme
Language Python 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'solve' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
#  1. INTEGER_ARRAY c
#  2. 2D_INTEGER_ARRAY tree
#  3. 2D_INTEGER_ARRAY queries
#

def solve(c, tree, queries):|

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    first_multiple_input = input().rstrip().split()

    n = int(first_multiple_input[0])

    q = int(first_multiple_input[1])
⇩ Upload Code as File
Run Code
Submit Code

Congratulations!

You have passed all the test cases.

Review
Auto
Quick
19:37:08

Ready to Start?

Click the button below when you're ready to begin your exam.
The timer will start and capture will be enabled.

Ready to Help

Select how you want to capture the problem:

Use Area Selection for specific parts of the screen

If you have a long problem, use the long capture button below to capture multiple parts.

Scroll → Add → Scroll → Add → Process

🧠 Agentic Multi-Pass Workflow Active

> 🔍 Extracting JSON Constraints (Pass 1)...
> 🧠 Generating Optimal Algorithm (Pass 2)...

Q1: Tree Queries

03:01 PM
👉 Answer:
#!/bin/python3
import math
import os

def solve(c, tree, queries):
    ans = 0
    for x in queries:
        ans += 1
    return ans
See It In Action

Watch How Oppex Works

60-second demo of the complete workflow

📸 Smart Capture
🧠 Detailed Explanations
Unobtrusive Design
Features

Everything You Need to Succeed

Powerful AI technology designed for developer productivity

Everything You Need to Succeed

Powerful AI technology designed for developer productivity. Watch the short video to see how Oppex transforms your workflow.

Workflow Demo
See It. Solve It. Instantly.
Not Just Fast. Correct.
Built to Be Invisible.
Every Language. Every Platform.
v1.1.104
How It Works

Get Started in 3 Steps

Simple setup, powerful results

1

Download & Setup

Get Oppex for Windows. Installs in seconds and runs quietly in the background.

2

Seamless Analysis

Press the hotkey while looking at any complex algorithm or codebase.

3

Verify & Learn

Instantly check your answers and get deep explanations to master the material.

Pricing

Choose Your Plan

Pay once, use anytime. Credits never expire.

Starter

$5

1 hour access

  • ✓ Full AI access
  • ✓ 60 minutes exam time
  • ✓ Clean text extraction
  • ✓ Credits never expire

Basic

$9

2 hours access

  • ✓ Everything in Starter
  • ✓ 120 minutes total
  • ✓ Priority AI response
  • ✓ Credits never expire
FAQ

Frequently Asked Questions

Oppex analyzes questions from your screen and provides detailed step-by-step logic to help you verify your own answers and understand where you might have gone wrong.

Yes! Oppex supports a wide range of topics including Coding (DSA), Math, Science, and Aptitude. It's designed to be a comprehensive study companion for all your coursework.

Our AI provides deep, conceptual explanations, not just simple answers. This ensures you master the underlying logic rather than just memorizing the solution.

No, your credits never expire. Built for developers who value speed, privacy, and correctness. Buy once and use whenever you need. Take your time to study at your own pace without pressure.

Yes, 100%. We prioritize your privacy. We do not store screenshots or question data. Your study sessions and verification history are completely confidential.

Ready to Master Your Subjects?

Join thousands of students verifying their knowledge with Oppex

Download for Windows v1.1.204 • Free