Distinct Integers in Range
Practice
4.1 (32 votes)
Advanced data structures
Bit manipulation
Data structures
Easy
Segment trees
Problem
77% Success 4127 Attempts 20 Points 2s Time Limit 1024MB Memory 1024 KB Max Code

You are given two arrays $$A$$ and $$B$$ each of length $$N$$. Now you are given $$Q$$ queries. In each query, you are given two pairs of ranges i.e. a range $$[a,b]$$ in the array $$A$$ and range $$[c,d]$$ in the array $$B$$. For each query, you need to count distinct elements in the array which is formed by combining elements of the both the ranges in the query.

Input
The first line contains an integer $$N$$ as input. 
Next line contains $$N$$ space separated integers that denote elements of the array $$A$$.
Next line contains $$N$$ space separated integers that denote elements of the array $$B$$.
Next line contains an integer $$Q$$ that denotes the total number of queries.
Next $$Q$$ lines contain $$4$$ integers each i.e. $$a, b, c$$ and $$d$$.

Output
For each query, you need to print the output in a new line. 

Constraints
$$1 \le N \le 10^5$$
$$1 \le A[i], B[i] \le 5000$$
$$1 \le Q \le 10^5$$
$$1 \le a \le b \le N$$
$$1 \le c \le d \le N$$

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:20
65 votes
Tags:
AlgorithmsApprovedEasyOpenTrees
Points:20
29 votes
Tags:
Advanced Data StructuresData StructuresEasySegment Trees
Points:20
16 votes
Tags:
Advanced Data StructuresData StructuresEasySegment Trees