Beautiful pair of nodes
Practice
5 (1 votes)
Advanced data structures
Data structures
Depth first search
Fenwick tree
Fenwick tree
Hard
Open
Treap
Problem
23% Success 698 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given a rooted tree having n nodes, rooted at node 1. Every node i has two values associated with itself , \(A[i]\) and \(B[i]\). In this tree you have to count total pairs of nodes which are beautiful. A pair of nodes i , j is beautiful if i is ancestor of node j and \(A[i] \lt A[j]\) and \(B[i] < B[j]\).
Node i is called an ancestor of some node j, if node i is the parent of node j, or it is the ancestor of parent of node j. The root of a tree has no ancestors.
Input
First line contains a number n as input. Next \(n - 1\) lines contain two integers u , v denoting that there is an edge between the nodes u and v. Next line contains n space separated integers that denote the \(A[i]\) value for each node. Similarly next line contains n space separated integers that denote the \(B[i]\) value for each node.

Output
In the output you have to print total good pairs in the tree.

Constraints
\(1 \le N \le 10^5\)
\(1 \le A[i] \le 10^9\)
\(1 \le B[i] \le 10^9\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
AlgorithmsApprovedBit ManipulationFenwick treeHardOpenTrees
Points:50
2 votes
Tags:
Data StructuresBinary indexed treeDatastructuresAdvanced Data StructuresFenwick (Binary Indexed) Trees
Points:50
16 votes
Tags:
HardLoopsTrees