def solve(par):
    N, array, queries = par
    tree = SegmentTreeSum(N)
    tree.buildTree(array)
    result = ['']
    for q in queries:
        if q[0] == 'S':
            x = int(q[1]) - 1
            v = int(q[2])
            tree.update(x, v)
        if q[0] == 'M':
            x = int(q[1]) - 1
            y = int(q[2]) - 1
            result.append(tree.query(x, y))
    return '\n'.join(str(e) for e in result)
Example #2
0
def solve(par):
    N, array, queries = par
    tree = SegmentTreeSum(N)
    tree.buildTree(array)
    result = ['']
    for q in queries:
        if q[0] == 'S':
            x = int(q[1]) - 1
            v = int(q[2])
            tree.update(x, v)
        if q[0] == 'M':
            x = int(q[1]) - 1
            y = int(q[2]) - 1
            result.append(tree.query(x, y))
    return '\n'.join(str(e) for e in result)
def solve(par):
    M, Q, construct, queries = par
    bits = BitArray()
    for m in construct:
        for i in range(m[0]):
            bits.append('0b' + m[1])

    nodes = set()
    for q in queries:  # (i-1, i]
        nodes.add(q[1] - 1)
        nodes.add(q[2])
    nodes = list(nodes)
    nodes.sort()

    count = [0] * len(nodes)
    for i in range(1, len(nodes)):
        count[i] = bits[nodes[i - 1] + 1:nodes[i] + 1].count(1)
    size = [0] * len(nodes)
    for i in range(1, len(nodes)):
        size[i] = nodes[i] - nodes[i - 1]

    tree = SegmentTreeSum(len(nodes))
    tree.buildTree(count, 1, 0, len(nodes) - 1)

    results = []
    for q in queries:
        i1 = nodes.index(q[1] - 1)
        i2 = nodes.index(q[2])
        if q[0] == 'F':
            for i in range(i1 + 1, i2 + 1):
                count[i] = size[i]
                tree.update(i, size[i])
        if q[0] == 'E':
            for i in range(i1 + 1, i2 + 1):
                count[i] = 0
                tree.update(i, 0)
        if q[0] == 'I':
            for i in range(i1 + 1, i2 + 1):
                count[i] = size[i] - count[i]
                tree.update(i, count[i])
        if q[0] == 'S':
            results.append(tree.query(i1 + 1, i2))
    return '\n'.join(str(e) for e in results) + '\n'
Example #4
0
def solve(par):
    M, Q, construct, queries = par
    bits = BitArray()
    for m in construct:
        for i in range(m[0]):
            bits.append('0b' + m[1])

    nodes = set()
    for q in queries:  # (i-1, i]
        nodes.add(q[1] - 1)
        nodes.add(q[2])
    nodes = list(nodes)
    nodes.sort()

    count = [0] * len(nodes)
    for i in range(1, len(nodes)):
        count[i] = bits[nodes[i - 1] + 1:nodes[i] + 1].count(1)
    size = [0] * len(nodes)
    for i in range(1, len(nodes)):
        size[i] = nodes[i] - nodes[i - 1]

    tree = SegmentTreeSum(len(nodes))
    tree.buildTree(count, 1, 0, len(nodes) - 1)

    results = []
    for q in queries:
        i1 = nodes.index(q[1] - 1)
        i2 = nodes.index(q[2])
        if q[0] == 'F':
            for i in range(i1 + 1, i2 + 1):
                count[i] = size[i]
                tree.update(i, size[i])
        if q[0] == 'E':
            for i in range(i1 + 1, i2 + 1):
                count[i] = 0
                tree.update(i, 0)
        if q[0] == 'I':
            for i in range(i1 + 1, i2 + 1):
                count[i] = size[i] - count[i]
                tree.update(i, count[i])
        if q[0] == 'S':
            results.append(tree.query(i1 + 1, i2))
    return '\n'.join(str(e) for e in results) + '\n'