Question

In: Computer Science

Jojo is given N integers, A1, A2, ..., AN by his teacher. His teacher also give...

Jojo is given N integers, A1, A2, ..., AN by his teacher. His teacher also give him 2M integers, L1,L2,...,LM and R1,R2,...,RM. For each i from 1 to M, his teacher asked him to calculate the sum of odd index numbers from index Li to Ri. For example if Li = 3 and Ri = 7, then he has to calculate the value of (A3 + A5 + A7). Help him by making the program to calculate it quickly!

Format Input

The first line consist of two integers, N and M. The second line consist of N integers, A1, A2, ..., AN . The next M lines consist of two integers, Li and Ri.

Format Output

Output M lines, the answer for Li and Ri, where i is an integer from 1 to M. Constraints

• 1 ≤ N, M ≤ 105 • 1≤Ai ≤102
• 1≤Li ≤Ri ≤N

Sample Input 1 (standard input)

53
13 7 1 5 9 11
22
25

Sample Output 1 (standard output)

13 0 10

please use c/c++ language

Solutions

Expert Solution

If you like the solution please give it a thumbs up. And if you have any query please let me know in the comments.

Solution :-

C++ Code :-

#include <iostream>
using namespace std;
  
int main() {
    int n,m;
    cin>>n>>m;

    int a[n+1];
    a[0] = 0;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    
    int sum[n];
    sum[0] = a[0];
    /* sum array is kind of prefix array, sum[i] stores sum of all odd
     index numbers upto ith index */
    for(int i=1;i<=n;i++)
    {
        // if index is odd then only add
        if(i%2 == 1)
            sum[i] = sum[i-1] + a[i];
        else 
            sum[i] = sum[i-1];
    }

    // now simply result for any query (l,r) will sum(r)-sum(l) --> (taking sum upto r index and then excluding upto l)
    for(int i=0;i<m;i++)
    {
        int l,r;
        cin>>l>>r;

        cout<<sum[r]-sum[l-1]<<" ";
    }

}

Sample input :-

6 4
4 12 9 5 31 8
1 4
1 6
1 1
3 6

Sample Output :-

13 44 4 40 

Related Solutions

Let a1 ≥ a2, . . . , an be a sequence of positive integers whose...
Let a1 ≥ a2, . . . , an be a sequence of positive integers whose sum is 2n − 2. Prove that there exists a tree T on n vertices whose vertices have degrees a1, a2, . . . , an. Sketch of solution: Prove that there exist i and j such that ai = 1 and aj ≥ 2. Remove ai, subtract 1 from aj and induct on n.
Question in graph theory: 1. Let (a1,a2,a3,...an) be a sequence of integers. Given that the sum...
Question in graph theory: 1. Let (a1,a2,a3,...an) be a sequence of integers. Given that the sum of all integers = 2(n-1) Write an algorithm that, starting with a sequence (a1,a2,a3,...an) of positive integers, either constructs a tree with this degree sequence or concludes that none is possible.
Prove: If a1 = b1 mod n and a2 = b2 mod n then (1) a1...
Prove: If a1 = b1 mod n and a2 = b2 mod n then (1) a1 + a2 = b1 + b2 mod n, (2) a1 − a2 = b1 − b2 mod n, and (3) a1a2 = b1b2 mod n.
Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces...
Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces as output a matrix ?={???}M={mij} where ???mij is the minim term in the sequence of integers ??,??+1,…,??ai,ai+1,…,aj for ?≥?j≥i and ???=0mij=0 otherwise. for i := 1 to n for j := 1+1 to n for k:= i+1 to j m[i][j] := min(m[i][j], a[k]) end for end for end for return m a.) Show that this algorithm uses ?(?3)O(n3) comparisons to compute the matrix M....
1. Give a direct proof that if n is an odd integers, then n3 is also...
1. Give a direct proof that if n is an odd integers, then n3 is also an odd integer. 2. Give a proof by contradiction that the square of any positive single digit decimal integer cannot have more than two decimal digits.
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . ,...
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . , an which are known to be bounded by n 2 (so ai ≤ n 2 , for every i = 1, . . . , n. Use the idea of Radix Sort (discussed in class and presented in Section 8.3 in the textbook). Illustrate your algorithm by showing on paper similar to Fig. 8.3, page 198 in the textbook (make sure you indicate clearly...
Suppose f is a degree n polynomial where f(a1) = b1, f(a2) = b2, · ·...
Suppose f is a degree n polynomial where f(a1) = b1, f(a2) = b2, · · · f(an+1) = bn+1 where a1, a2, · · · an+1 are n + 1 distinct values. We will define a new polynomial g(x) = b1(x − a2)(x − a3)· · ·(x − an+1) / (a1 − a2)(a1 − a3)· · ·(a1 − an+1) + b2(x − a1)(x − a3)(x − a4)· · ·(x − an+1) / (a2 − a1)(a2 − a3)(a2 − a4)·...
Given that K a1 =5.9 x 10 −3 and K a2 =6.0x10 −6, calculate the pH...
Given that K a1 =5.9 x 10 −3 and K a2 =6.0x10 −6, calculate the pH after titrating 70 mL of 0.10 M H2SO3 with 50 mL of 0.10 M KOH. Consider the titration of 30 mL of 0.10 M H2CO3 with 50 mL of 0.10 LiOH. What would the pH be if K a1 =6.0×10 −3  and K a2 =5.9×10 −7?
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
Every teacher at a high school is given a n-digit code, e.g. 530...297 (n digits in...
Every teacher at a high school is given a n-digit code, e.g. 530...297 (n digits in total). (1) How many different codes are there? (2) How many codes read the same backward and forward? (Consider the cases where n is odd or even.) (3) How many codes contain odd digits only? (4) How many codes contain at least one even digit? (5) Consider the case where n is 6. How many codes have distinct digits? (That is, no digit appears...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT