Bahubali and Ausata

Once Rajmata is kidnapped by Bhallal Dev and Bahubali wants to free her but there is a problem. Rajmata is inside a room and to enter the room, Bahubali needs a secret code. There is a hint for the code and Bahubali is so frustrated that he can’t decode the hint so he asked you to help him.

Bhalla Dev gives Bahubali an array of size N and Q queries and for each query, he gives a range (Left to Right) to Bahubali.

As we know Bhallal Dev is a genius in mathematics so he defined a new keyword named AAUSATA.To find AAUSATA Bahubali has to find the mean value of the array between the given range.

Example:

Input:  n = 5, q = 3, a = [1,2,3,4,5],  queries={{1,3},{2,4},{2,5}}
Output: 1->2->4->5->NULL

Output:

2 3 3

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

long long arr[1000001];
long long tree[2000010];
void build(long long node,
           long long startlong long end)
{
    if (start == end)
    {
        tree[node] = arr[start];
    }
    else
    {
        long long mid = (start + end) / 2;
        build(2 * nodestartmid);
        build(2 * node + 1mid + 1end);
        tree[node] = tree[2 * node] + tree[2 * node + 1];
    }
}
long long query(long long node,
                long long startlong long end,
                long long llong long r)
{
    if (r < start || l > end)
        return 0;
    if (l <= start && r >= end)
        return tree[node];
    long long mid = (start + end) / 2;
    long long p1 = query(2 * nodestartmidlr);
    long long p2 = query(2 * node + 1mid + 1endlr);
    return p1 + p2;
}
int main()
{

    long long n = 5q = 3;

    long long a[n] = {12345};
    for (long long i = 1i <= ni++)
        arr[i] = a[i - 1];
    build(11n);

    vector<pair<long longlong long>> queries =
 {{13}, {24}, {25}};
    for (int i = 0i < qi++)
    {
        long long l = queries[i].firstr = queries[i].second;

        long long ans = query(11nlr);
        cout << ans / (r - l + 1<< "\n";
    }
    return 0;
}


No comments:

Post a Comment