You are given an array of n numbers and q queries. For each query you have to print the floor of the expected value(mean) of the subarray from L to R.
Input:
First line contains two integers N and Q denoting number of array elements and number of queries.
Next line contains N space seperated integers denoting array elements.
Next Q lines contain two integers L and R(indices of the array).
Output:
print a single integer denoting the answer.
Constraints :
1<= N ,Q,L,R <= 10^6
1<= Array elements <= 10^9
SAMPLE INPUT
5 3
1 2 3 4 5
1 3
2 4
2 5
SAMPLE OUTPUT
2
3
3
In C -
#include <stdio.h>
void s(long long *x)
{
register char c = getchar_unlocked();
*x = 0;
while((c<48)||(c>57))c = getchar_unlocked();
while((c>47)&&(c<58))
{
*x = (int)((((*x)<<1) + ((*x)<<3)) + c - 48);
c = getchar_unlocked();
}
}
void p(long long n)
{
if(n == 0)
{
putchar_unlocked('0');
putchar_unlocked('\n');
}
else if(n == -1)
{
putchar_unlocked('-');
putchar_unlocked('1');
putchar_unlocked('\n');
}
else
{
char f[11];
f[10] = '\n';
int i = 9;
while(n)
{
f[i--] = n % 10 + '0';
n /= 10;
}
while(f[i] != '\n')
putchar_unlocked(f[++i]);
}
}
int main()
{
long long n,q,l,r,i,a[1000005];
s(&n);
s(&q);
for(s(a+1),i=2;i<n+1;i++)
{
s(a+i);
*(a+i)+=*(a+i-1);
}
for(;q >0;q--)
{
s(&l);
s(&r);
p((*(a+r)-*(a+l-1))/(r-l+1));
}
return 0;
}
In C++ -
#include <iostream>
#include <cstdio>
inline void fastRead_long(long *a)
{
register char c=0;
while (c<33) c=getchar_unlocked();
*a=0;
while (c>33)
{
*a=*a*10+c-'0';
c=getchar_unlocked();
}
}
/*int main()
{
int n;
char s[16];
fastRead_int(&n);
fastRead_string(s);
return 0;
}*/
int main()
{
long * B = NULL;
long A,N,Q,L,R,i,j,k,mean=0;
//scanf("%ld%ld",&N,&Q);
fastRead_long(&N);
fastRead_long(&Q);
B = new long[N];
for(i=1;i<=N;i++){
//scanf("%ld",&A);
fastRead_long(&A);
B[i]=B[i-1]+A;
}
for(j=1;j<=Q;j++)
{
//scanf("%ld%ld",&L,&R);
fastRead_long(&L);
fastRead_long(&R);
mean=B[R]-B[L-1];
printf("%ld\n",mean/(R-L+1));
mean=0;
}
return 0;
}
In Java -
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
FastReader fr = new FastReader();
StringBuilder sb = new StringBuilder();
int n = fr.nextInt();
int q = fr.nextInt();
long[] arr = new long[n];
arr[0] = fr.nextInt();
for(int i = 1; i < n; ++i){
arr[i] = arr[i - 1] + fr.nextInt();
}
int l, r, count;
long sum;
while(q > 0){
l = fr.nextInt() - 1;
r = fr.nextInt() - 1;
count = r - l + 1;
if(l == 0){
sum = arr[r];
}else{
sum = arr[r] - arr[l - 1];
}
sb.append(sum/count);
if(q != 1){
sb.append("\n");
}
--q;
}
System.out.print(sb.toString());
fr.close();
}
}
class FastReader{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public FastReader(){
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastReader(String file_name){
try{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
catch(IOException e){
e.printStackTrace();
}
}
public String readLine(){
StringBuilder sb = new StringBuilder();
byte c;
while((c = read()) != -1){
if(c == '\n'){
break;
}
sb.append((char)c);
}
return sb.toString();
}
public String readLine(int size){
byte[] buf = new byte[size];
int cnt = 0, c;
while((c = read()) != -1){
if(c == '\n'){
break;
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt(){
int n = 0;
boolean neg = false;
int c;
while((c = read()) <= ' ');
neg = c == '-';
if(neg){
c = read();
}
do{
n = n * 10 + c - '0';
}
while((c = read()) >= '0' && c <= '9');
if(c == 13){
read();
}
return neg ? -n : n;
}
public long nextLong(){
long n = 0;
boolean neg = false;
int c;
while((c = read()) <= ' ');
neg = c == '-';
if(neg){
c = read();
}
do{
n = n * 10 + c - '0';
}
while((c = read()) >= '0' && c <= '9');
if(c == 13){
read();
}
return neg ? -n : n;
}
public double nextDouble(){
double n = 0, div = 1;
boolean neg = false;
int c;
while((c = read()) <= ' ');
neg = c == '-';
if(neg){
c = read();
}
do{
n = n * 10 + c - '0';
}
while((c = read()) >= '0' && c <= '9');
if(c == '.'){
while((c = read()) >= '0' && c <= '9'){
n += (c - '0') / (div *= 10);
}
}
if(c == 13){
read();
}
return neg ? -n : n;
}
private void fillBuffer(){
try{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if(bytesRead == -1){
buffer[0] = -1;
}
}
catch(IOException e){
e.printStackTrace();
}
}
public byte read(){
if(bufferPointer == bytesRead){
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close(){
try{
if(din == null){
return;
}
else{
din.close();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
By - Jagnath
In Python -
def play():
from sys import stdin, stdout
n,q=map(int,stdin.readline().split())
a=[0]
for x in stdin.readline().split():
a.append(int(x)+a[-1])
res=""
for Inp in stdin.readlines():
l,r=map(int,Inp.split())
n=r-l+1
S=a[r]-a[l-1]
res+=str(S//n)+"\n"
stdout.write(res)
if __name__ == "__main__":
play()
By - Rajat Jain
Input:
First line contains two integers N and Q denoting number of array elements and number of queries.
Next line contains N space seperated integers denoting array elements.
Next Q lines contain two integers L and R(indices of the array).
Output:
print a single integer denoting the answer.
Constraints :
1<= N ,Q,L,R <= 10^6
1<= Array elements <= 10^9
SAMPLE INPUT
5 3
1 2 3 4 5
1 3
2 4
2 5
SAMPLE OUTPUT
2
3
3
In C -
#include <stdio.h>
void s(long long *x)
{
register char c = getchar_unlocked();
*x = 0;
while((c<48)||(c>57))c = getchar_unlocked();
while((c>47)&&(c<58))
{
*x = (int)((((*x)<<1) + ((*x)<<3)) + c - 48);
c = getchar_unlocked();
}
}
void p(long long n)
{
if(n == 0)
{
putchar_unlocked('0');
putchar_unlocked('\n');
}
else if(n == -1)
{
putchar_unlocked('-');
putchar_unlocked('1');
putchar_unlocked('\n');
}
else
{
char f[11];
f[10] = '\n';
int i = 9;
while(n)
{
f[i--] = n % 10 + '0';
n /= 10;
}
while(f[i] != '\n')
putchar_unlocked(f[++i]);
}
}
int main()
{
long long n,q,l,r,i,a[1000005];
s(&n);
s(&q);
for(s(a+1),i=2;i<n+1;i++)
{
s(a+i);
*(a+i)+=*(a+i-1);
}
for(;q >0;q--)
{
s(&l);
s(&r);
p((*(a+r)-*(a+l-1))/(r-l+1));
}
return 0;
}
In C++ -
#include <iostream>
#include <cstdio>
inline void fastRead_long(long *a)
{
register char c=0;
while (c<33) c=getchar_unlocked();
*a=0;
while (c>33)
{
*a=*a*10+c-'0';
c=getchar_unlocked();
}
}
/*int main()
{
int n;
char s[16];
fastRead_int(&n);
fastRead_string(s);
return 0;
}*/
int main()
{
long * B = NULL;
long A,N,Q,L,R,i,j,k,mean=0;
//scanf("%ld%ld",&N,&Q);
fastRead_long(&N);
fastRead_long(&Q);
B = new long[N];
for(i=1;i<=N;i++){
//scanf("%ld",&A);
fastRead_long(&A);
B[i]=B[i-1]+A;
}
for(j=1;j<=Q;j++)
{
//scanf("%ld%ld",&L,&R);
fastRead_long(&L);
fastRead_long(&R);
mean=B[R]-B[L-1];
printf("%ld\n",mean/(R-L+1));
mean=0;
}
return 0;
}
In Java -
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
FastReader fr = new FastReader();
StringBuilder sb = new StringBuilder();
int n = fr.nextInt();
int q = fr.nextInt();
long[] arr = new long[n];
arr[0] = fr.nextInt();
for(int i = 1; i < n; ++i){
arr[i] = arr[i - 1] + fr.nextInt();
}
int l, r, count;
long sum;
while(q > 0){
l = fr.nextInt() - 1;
r = fr.nextInt() - 1;
count = r - l + 1;
if(l == 0){
sum = arr[r];
}else{
sum = arr[r] - arr[l - 1];
}
sb.append(sum/count);
if(q != 1){
sb.append("\n");
}
--q;
}
System.out.print(sb.toString());
fr.close();
}
}
class FastReader{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public FastReader(){
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastReader(String file_name){
try{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
catch(IOException e){
e.printStackTrace();
}
}
public String readLine(){
StringBuilder sb = new StringBuilder();
byte c;
while((c = read()) != -1){
if(c == '\n'){
break;
}
sb.append((char)c);
}
return sb.toString();
}
public String readLine(int size){
byte[] buf = new byte[size];
int cnt = 0, c;
while((c = read()) != -1){
if(c == '\n'){
break;
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt(){
int n = 0;
boolean neg = false;
int c;
while((c = read()) <= ' ');
neg = c == '-';
if(neg){
c = read();
}
do{
n = n * 10 + c - '0';
}
while((c = read()) >= '0' && c <= '9');
if(c == 13){
read();
}
return neg ? -n : n;
}
public long nextLong(){
long n = 0;
boolean neg = false;
int c;
while((c = read()) <= ' ');
neg = c == '-';
if(neg){
c = read();
}
do{
n = n * 10 + c - '0';
}
while((c = read()) >= '0' && c <= '9');
if(c == 13){
read();
}
return neg ? -n : n;
}
public double nextDouble(){
double n = 0, div = 1;
boolean neg = false;
int c;
while((c = read()) <= ' ');
neg = c == '-';
if(neg){
c = read();
}
do{
n = n * 10 + c - '0';
}
while((c = read()) >= '0' && c <= '9');
if(c == '.'){
while((c = read()) >= '0' && c <= '9'){
n += (c - '0') / (div *= 10);
}
}
if(c == 13){
read();
}
return neg ? -n : n;
}
private void fillBuffer(){
try{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if(bytesRead == -1){
buffer[0] = -1;
}
}
catch(IOException e){
e.printStackTrace();
}
}
public byte read(){
if(bufferPointer == bytesRead){
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close(){
try{
if(din == null){
return;
}
else{
din.close();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
By - Jagnath
In Python -
def play():
from sys import stdin, stdout
n,q=map(int,stdin.readline().split())
a=[0]
for x in stdin.readline().split():
a.append(int(x)+a[-1])
res=""
for Inp in stdin.readlines():
l,r=map(int,Inp.split())
n=r-l+1
S=a[r]-a[l-1]
res+=str(S//n)+"\n"
stdout.write(res)
if __name__ == "__main__":
play()
By - Rajat Jain
Post a Comment