Is This a Long Challenge | CodeChef

Can anybody help me with this, I’m getting time limit exceeded error always when I try to solve. I’m an amateur btw XD.

here’s my code in python 3.6

try:
t = int(input())
except:
t = 0
MOD = 1000000007
for i in range(t):
n = int(input())
s = input()
s_list = list(s)
q = int(input())
queries = []

for j in range(q):
    queries.append(list(input().rstrip().split()))
    

for j in queries:
    start  = int(j[1]) - 1 
    end = int(j[2]) - 1
    k = start
    if j[0] == '1':
        while k <= end:
            s_list[k] = j[3]
            k+=1
    else:
        while k <= end:
            temp_p = int(j[3])
            temp_q = int(j[4])
            if s_list[k] == 'A':
                # temp_p = int(j[3])
                # temp_q = int(j[4])
                j[3] = (temp_p%MOD - temp_q%MOD)%MOD
                j[4] = (temp_p%MOD + temp_q%MOD)%MOD
            else:
                # temp_p = int(j[3])
                # temp_q = int(j[4])
                j[3] = (temp_p%MOD + temp_q%MOD)%MOD
                j[4] = (temp_q%MOD - temp_p%MOD)%MOD
                
            k+=1
            
        print(j[3], j[4])

Brute force is not going to work, You need to use a segment tree of matrices with lazy propogations. I would recommend you to try easier problems first.
Or if you are confident, I would advise you to read up on segment trees.

Oh, Thank you! I’ll try easier problems first and also read up on segment trees.

BTW can you guide me how can go about Data structures and algorithms? or sites i can refer and study

https://cp-algorithms.com/

It can be done also by simple array iteration by only observing the pattern.

I remember trying, I saw some figment of a pattern, but the updating causes problems.

Thanks a lot, it quite helps me in learning everything required for Competitive programming.