<p>You are given a string <code>s</code> of lowercase English letters and a 2D integer array <code>shifts</code> where <code>shifts[i] = [start<sub>i</sub>, end<sub>i</sub>, direction<sub>i</sub>]</code>. For every <code>i</code>, <strong>shift</strong> the characters in <code>s</code> from the index <code>start<sub>i</sub></code> to the index <code>end<sub>i</sub></code> (<strong>inclusive</strong>) forward if <code>direction<sub>i</sub> = 1</code>, or shift the characters backward if <code>direction<sub>i</sub> = 0</code>.</p>

<p>Shifting a character <strong>forward</strong> means replacing it with the <strong>next</strong> letter in the alphabet (wrapping around so that <code>&#39;z&#39;</code> becomes <code>&#39;a&#39;</code>). Similarly, shifting a character <strong>backward</strong> means replacing it with the <strong>previous</strong> letter in the alphabet (wrapping around so that <code>&#39;a&#39;</code> becomes <code>&#39;z&#39;</code>).</p>

<p>Return <em>the final string after all such shifts to </em><code>s</code><em> are applied</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abc&quot;, shifts = [[0,1,0],[1,2,1],[0,2,1]]
<strong>Output:</strong> &quot;ace&quot;
<strong>Explanation:</strong> Firstly, shift the characters from index 0 to index 1 backward. Now s = &quot;zac&quot;.
Secondly, shift the characters from index 1 to index 2 forward. Now s = &quot;zbd&quot;.
Finally, shift the characters from index 0 to index 2 forward. Now s = &quot;ace&quot;.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;dztz&quot;, shifts = [[0,0,0],[1,1,1]]
<strong>Output:</strong> &quot;catz&quot;
<strong>Explanation:</strong> Firstly, shift the characters from index 0 to index 0 backward. Now s = &quot;cztz&quot;.
Finally, shift the characters from index 1 to index 1 forward. Now s = &quot;catz&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length, shifts.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>shifts[i].length == 3</code></li>
	<li><code>0 &lt;= start<sub>i</sub> &lt;= end<sub>i</sub> &lt; s.length</code></li>
	<li><code>0 &lt;= direction<sub>i</sub> &lt;= 1</code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

class BinaryIndexedTree:
    def __init__(self, string):
        self.tree = [0] * (len(string) + 1)
    def update(self, index, delta):
        n = len(self.tree)
        while index < n:
            self.tree[index] += delta
            index += index & -index
    def query(self, index):
        shifts = 0
        while index > 0:
            shifts += self.tree[index]
            index -= index & -index
        return shifts

    def get(self,):
        return self.tree[1:]
        
    
class Solution:
    def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
        bit = BinaryIndexedTree(s)

        for start, end, direction in shifts:
            delta = -1 if direction == 0 else 1
            bit.update(start +1, delta)
            bit.update(end + 2, -delta)

        ASCII_A, ASCII_Z = 97, 122

        res = ''
        for i, c in enumerate(s):
            shifts = bit.query(i + 1)

            sign = -1 if shifts < 0 else 1

            delta = sign * (abs(shifts) % 26)
            
            code = ord(c) + delta
            if code > ord('z'):
                code += ASCII_A - ASCII_Z - 1
            elif code < ord('a'):
                code += ASCII_Z - ASCII_A + 1
            res += chr(code)
        return res
            

1237