STRSUB - Editorial

Hello Chefs, will you please help me where am I going wrong in following solution :

    def ans(L,R):
        k = max(L,raf[R-1])-1
        return (sumFar[k]-sumFar[L-1]+(R-k)*(R+1)-(R*(R+1)//2)-(L*(L-1)//2))
        
    for _ in range(int(input())):
        n,k,q = map(int,input().split())
        s = input()
        far = []; raf = [-1]*n; sumFar = [0]; c0 = c1 = j = 0
        for i in range(n):
            while j<n and c0<k and c1<k:
                if s[j]=='1':
                    c1 += 1
                else:
                    c0 += 1
                j += 1
            far.append(j); sumFar.append(sumFar[i]+far[i])
            if s[i]=='1':
                c1 -= 1
            else:
                c0 -= 1
        for i in range(n-1,-1,-1):
            raf[far[i]-1] = i
        for i in range(n-1,-1,-1):
            if raf[i]==-1:
                raf[i] = raf[i+1]
        for _ in range(q):
            l,r = map(int,input().split())
            print(ans(l,r))

i cudnt solve strsub(COUNT SUBSTRINGS)
i.e. first 3 cases of the third subtask are giving WA
rest are correct
how to go abt it?
i even tried stress testing against few of my own casesā€¦seems to work fine for meā€¦
my code CodeChef: Practical coding for everyone
the only difference from the editorial is the way the author thought of RAF i.e. i store idx[R] is the largest k such that far[k] <= R wheras for the author its raf[k] is the smallest k such that far[k] > R

Please explain Line 50 which is ā€œres -= (ll)(L+R-2) * (R-L+1) / 2ā€ from the testerā€™s code. From where did (L+R-2) and (R-L+1) come?

sigma k+1 to R of a constant is R-k , so after solving sigma notation it come out as (R-k)*(R+1)

https://www.codechef.com/viewsolution/48340472

I did exactly what the editorial asked to do, but still getting WA. can someone help?

Why does having the same L and R index does not yield 1?