Travelling between cities
Practice
5 (2 votes)
Binary search
Dsu
Data structures
Dynamic programming
Medium
Merge sort
Segment trees
Problem
15% Success 2150 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given positions of $$N$$ cities situated on X axis. The position of the $$i^{th}$$ city is denoted by array value $$Location[i]$$.You have a bike which can travel atmost $$K$$ unit distance once the tank is full and each city has a fuel pump. You can assume that once the bike reaches a city its tank is again filled up completely.Now you have to answer $$Q$$ queries where each query is of the following form.

  • $$L\;R\;X$$ :- Find the number of cities lying in the index range \([L,R]\) that you can reach from the city at index $$X$$.

Constraints:

  • \(1 ≤ T ≤ 10\)
  • \(1 ≤ N,Q ≤ 50000\)
  • \(1 ≤ K ≤ 1000000\)
  • \(1 ≤ Location_i ≤ 1000000000\)
  • \(1 ≤ L ≤ R ≤ N\)
  • \(1 ≤ X ≤ N\)

Note :- Use fast i/o.

Format of the input file:
First line : $$T$$ i.e Number of testcases.
For each testcase :
First line : Three space separated integers $$N$$ , $$K$$ and $$Q$$.
Second line : $$N$$ space separated integers denoting the location of cities .
Next $$Q$$ lines : Three space separated $$L\;R\;X$$ integers denoting the query.

Format of the output file:
Output the answer to each query in a separate line.

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:30
8 votes
Tags:
ApprovedMathMediumOpenTrees
Points:30
330 votes
Tags:
AlgorithmsApprovedFenwick TreeMediumOpen
Points:30
18 votes
Tags:
Advanced Data StructuresData StructuresMediumSegment Trees