Question: You are given an array a of length n . It is given that the beauty ( l , r ) is defined as the
You are given an array a of length n
It is given that the beautyl r is defined as the maximum integer x that appears at least x times in the subarray from I to ror zero if no such integer exists
You are given q
queries where each query contains two integers I and r such that rn The answer to each query is the beautyl r
You have to find the answer to the qu queries in an online way. This means that for the ith query, add the sum of answers over all previous queries to both I and r then do the fol
l l mod n
rr mod n
If r then swapl r
Your task is to find the sum of answer to all q queries. Since the answer can be large, returnreturn it modulo
sample input:
sample output:
Explanation:
Here, n
a
keervrs
a t queries
sum ;
for query
I r
Ilsum
rrsum
is the maximum integer that appears atleast times in the subarray from I to r
Hence, answer for this query is
sum
for query
lr
llsum
rrsum
swap lrlr
is the maximum integer that appears atleast times in the subarray from I to
Hence, answer for this query is
sum
query
lr
llsum
rrsum
is the maximum integer that appears atleast times in the subarray from I to r
Hence, answer for this query is
sum
so our ansewer is sume
import sysystem
def solvenaqtqueries:
#write your code here
def main:
n intsysstdin.readlinestrip
a
for in rangen:
aappendintsysstdin.readlinestrip
q intsysstdin.readlinestrip
t intsysstdin.readlinestrip
queries
for in rangeq:
queries.appendlistmaplambda x: intx sysstdin.readlinestripsplit
result getansn a q t queries
printresult
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
