Mojtabas Trees and Arpas Queries <March HourStorm>
Practice
3.7 (3 votes)
Advanced data structures
Data structures
Fenwick tree
Medium
Problem
38% Success 680 Attempts 50 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Mojtaba has two trees, each of them has $$n$$ vertices. Arpa has $$q$$ queries, each in type $$v, u, x, y$$. Let $$s$$ be the set of vertices in the path from $$v$$ to $$u$$ in the first tree and $$p$$ be the set of vertices in the path from $$x$$ to $$y$$ in the second tree. Mojtaba has to calculate the size of $$s \cap p$$ for each query. Help him!

Input Format

The first line of input will contain two integers, $$n, q$$ ($$1 \le n, q \le 3 \cdot 10^5$$).

The next $$n-1$$ lines will contain edges of the first tree.

The next $$n-1$$ lines will contain edges of the second tree.

The next $$q$$ lines contain queries.

Output Format

For each query, let $$s$$ be the set of vertices in the path from $$v$$ to $$u$$ in the first tree and $$p$$ be the set of vertices in the path from $$x$$ to $$y$$ in the second tree, print the size of $$s \cap p$$ for each query.

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
36 votes
Tags:
Bit ManipulationGraphsMediumReadySegment Trees
Points:50
1 votes
Tags:
Advanced Data StructuresData StructuresFenwick treeLine Sweep TechniqueMediumSegment Treesoffline
Points:50
1 votes
Tags:
Medium