Curious Queries
Practice
3.8 (9 votes)
Segment trees
Advanced data structures
Data structures
Problem
80% Success 1841 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given an array \(A\) of length \(N\). You are asked \(Q\) queries. Each query is of the form \(L,R\), and the answer is the sum of \(A[i]\) for all \(i\) such that \(0 \leq i \leq L\) and \(A[ i ] > A[ R ]\).
Print an array \(B\) of length \(Q\) such that \(B[i]\) is the answer to the \(i^{th}\) query.
Input format
- The first line of input contains a single integer \(T\), which denotes the number of test cases.
- The first line of each test case contains two integers \(N\) and \(Q\), denoting the length of the array \(A\) and the number of queries, respectively.
- The second line of each test case contains \(N\) integers, denoting the array \(A\).
- The following \(Q\) lines of each test case contains two integers \(L,R\) denoting each query.
Output format
For each test case, print an array \(B\) of length \(Q\) separated by space such that \(B[i]\) is the answer to the \(i^{th}\) query in a new line.
Constraints
\(1 \leq T \leq 10 \\ 1 \leq N, Q \leq 10^5 \\ 1 \leq A[i] \leq 10^5 \\ 0 \leq L, R \lt 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
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
7 votes
Tags:
Advanced Data StructuresBinary SearchNumber TheorySegment TreesData StructuresMath
Points:50
3 votes
Tags:
AlgorithmsApprovedHardOpenSegment TreesTrees
Points:50
7 votes
Tags:
String SearchingSegment treeData StructuresSegment TreesString AlgorithmsAdvanced Data StructuresAlgorithms
Editorial
Login to unlock the editorial