Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started