#C9062. Sum of Depths of Puddles Within Specified Ranges

    ID: 53114 Type: Default 1000ms 256MiB

Sum of Depths of Puddles Within Specified Ranges

Sum of Depths of Puddles Within Specified Ranges

You are given a road with ( n ) puddles. Each puddle is characterized by its position and depth. You are also given ( q ) queries. Each query provides an interval ( [l, r] ). For each query, compute the sum of the depths of all puddles whose positions ( x ) satisfy ( l \le x \le r ).

The problem can be formalized using the formula:

[ S = \sum_{i=1}^{n} d_i \cdot \mathbf{1}{{l \le p_i \le r}}, ]

where ( p_i ) and ( d_i ) denote the position and depth of the ( i )-th puddle, respectively, and ( \mathbf{1}
{{l \le p_i \le r}} ) is an indicator function that is 1 if ( p_i ) is in the range ([l, r]) and 0 otherwise.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains an integer ( n ) (the number of puddles).
  • The second line contains ( n ) space-separated integers representing the positions of the puddles.
  • The third line contains ( n ) space-separated integers representing the depths of the puddles corresponding to the positions.
  • The fourth line contains an integer ( q ) (the number of queries).
  • The following ( q ) lines each contain two space-separated integers ( l ) and ( r ), representing the query range.

outputFormat

For each query, output a single line containing the sum of the depths of the puddles whose positions are within the given range [l, r]. The answers must be printed in the same order as the queries appear in the input.## sample

5
1 2 4 7 8
10 20 15 10 5
3
2 8
1 4
5 9
50

45 15

</p>