Factory Repairs (Codeforces 634C)
Tags: Others, data structures

A factory produces thimbles in bulk. Typically, it can produce up to a thimbles a day. However, some of the machinery is defective, so it can currently only produce b thimbles each day. The factory intends to choose a k-day period to do maintenance and construction; it cannot produce any thimbles during this time, but will be restored to its full production of a thimbles per day after the k days are complete.Initially, no orders are pending. The factory receives updates of the form di, ai, indicating that ai new orders have been placed for the di-th day. Each order requires a single thimble to be produced on precisely the specified day. The factory may opt to fill as many or as few of the orders in a single batch as it likes.As orders come in, the factory owner would like to know the maximum number of orders he will be able to fill if he starts repairs on a given day pi. Help the owner answer his questions.

Input: The first line contains five integers n, k, a, b, and q (1 ≤ k ≤ n ≤ 200 000, 1 ≤ b < a ≤ 10 000, 1 ≤ q ≤ 200 000) — the number of days, the length of the repair time, the production rates of the factory, and the number of updates, respectively.The next q lines contain the descriptions of the queries. Each query is of one of the following two forms:   1 di ai (1 ≤ di ≤ n, 1 ≤ ai ≤ 10 000), representing an update of ai orders on day di, or  2 pi (1 ≤ pi ≤ n - k + 1), representing a question: at the moment, how many orders could be filled if the factory decided to commence repairs on day pi? It's guaranteed that the input will contain at least one query of the second type.

Output: For each query of the second type, print a line containing a single integer — the maximum number of orders that the factory can fill over all n days.

Note: Consider the first sample.We produce up to 1 thimble a day currently and will produce up to 2 thimbles a day after repairs. Repairs take 2 days.For the first question, we are able to fill 1 order on day 1, no orders on days 2 and 3 since we are repairing, no orders on day 4 since no thimbles have been ordered for that day, and 2 orders for day 5 since we are limited to our production capacity, for a total of 3 orders filled.For the third question, we are able to fill 1 order on day 1, 1 order on day 2, and 2 orders on day 5, for a total of 4 orders.

Sample Inputs:
5 2 2 1 8
1 1 2
1 5 3
1 2 1
2 2
1 4 2
1 3 2
2 1
2 3

5 4 10 1 6
1 1 5
1 5 5
1 3 2
1 5 2
2 1
2 2

Sample Outputs:
3
6
4

7
1

Solution (Python):
import sys
input = sys.stdin.buffer.readline

class SegmentTree:

    def __init__(self, data, default=0, func=lambda a, b: a + b):
        """"""
        self._default = default
        self._func = func
        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()
        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = self._default

    def __getitem__(self, idx):
        return self.data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        self.data[idx] = value
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        """"""
        start += self._size
        stop += self._size
        res_left = res_right = self._default
        while start < stop:
            if start & 1:
                res_left = self._func(res_left, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res_right = self._func(self.data[stop], res_right)
            start >>= 1
            stop >>= 1
        return self._func(res_left, res_right)

    def __repr__(self):
        return 'SegmentTree({0})'.format(self.data)

def process(n, k, a, b, Queries):
    days = [0 for i in range(n + 1)]
    S_b = SegmentTree(data=days)
    S_a = SegmentTree(data=days)
    for query in Queries:
        if query[0] == 1:
            (di, ai) = (query[1], query[2])
            S_a[di] = min(S_a[di] + ai, a)
            S_b[di] = min(S_b[di] + ai, b)
        else:
            pi = query[1]
            entry = S_b.query(1, pi) + S_a.query(pi + k, n + 1)
            sys.stdout.write(f'{entry}\n')
(n, k, a, b, q) = [int(x) for x in input().split()]
Queries = []
for i in range(q):
    query = [int(x) for x in input().split()]
    Queries.append(query)
process(n, k, a, b, Queries)

Solution (Python):
class FenwickTree:

    def __init__(self, size):
        self.bit = [0] * (size + 1)

    def add(self, x, v):
        while x <= len(self.bit) - 1:
            self.bit[x] += v
            x += x & -x

    def sum(self, x):
        ret = 0
        while x > 0:
            ret += self.bit[x]
            x -= x & -x
        return ret

    def range_sum(self, l, r):
        return self.sum(r) - self.sum(l - 1)
(N, K, A, B, Q) = map(int, input().split())
capA = [0] * (N + 1)
capB = [0] * (N + 1)
treeA = FenwickTree(N)
treeB = FenwickTree(N)
for _ in range(Q):
    op_data = list(map(int, input().split()))
    op = op_data[0]
    if op == 1:
        (a, b) = (op_data[1], op_data[2])
        addA = min(A - capA[a], b)
        addB = min(B - capB[a], b)
        capA[a] += addA
        capB[a] += addB
        treeA.add(a, addA)
        treeB.add(a, addB)
    else:
        a = op_data[1]
        ans = treeB.range_sum(1, a - 1)
        ans += treeA.range_sum(a + K, N)
        print(ans)

Solution (Python):
(n, k, a, b, q) = [int(i) for i in input().split()]
q_lst = [[int(i) for i in input().split()] for _ in range(q)]

class SegmentTreeNode:

    def __init__(self, val, lbound, rbound, lchild=None, rchild=None):
        self.val = val
        self.lbound = lbound
        self.rbound = rbound
        self.lchild = lchild
        self.rchild = rchild

class SegmentTree:

    def __init__(self, arr):
        self.length = len(arr)
        self.root = self.build(0, self.length - 1, arr)
        self.vals = arr

    def feature_func(self, lval, rval):
        return [lval[0] + rval[0], lval[1] + rval[1]]

    def build(self, lbound, rbound, arr):
        if lbound == rbound:
            root = SegmentTreeNode(arr[lbound], lbound, rbound)
        else:
            mid = (lbound + rbound) // 2
            lchild = self.build(lbound, mid, arr)
            rchild = self.build(mid + 1, rbound, arr)
            val = self.feature_func(lchild.val, rchild.val)
            root = SegmentTreeNode(val, lbound, rbound, lchild, rchild)
        return root

    def update(self, idx, val):
        self.vals[idx] = val
        self._update(idx, val, self.root)
        return

    def _update(self, idx, val, root):
        if root.lbound == root.rbound:
            assert root.lbound == idx
            root.val = val
            return
        mid = (root.lbound + root.rbound) // 2
        if idx <= mid:
            self._update(idx, val, root.lchild)
        else:
            self._update(idx, val, root.rchild)
        root.val = self.feature_func(root.lchild.val, root.rchild.val)
        return

    def query(self, lb, rb):
        return self._query(lb, rb, self.root)

    def _query(self, lb, rb, root):
        if lb == root.lbound and rb == root.rbound:
            return root.val
        mid = (root.lbound + root.rbound) // 2
        if rb <= mid:
            return self._query(lb, rb, root.lchild)
        elif lb > mid:
            return self._query(lb, rb, root.rchild)
        else:
            lval = self._query(lb, mid, root.lchild)
            rval = self._query(mid + 1, rb, root.rchild)
            return self.feature_func(lval, rval)
st = SegmentTree([[0, 0] for _ in range(n)])
for i in q_lst:
    if i[0] == 1:
        st.update(i[1] - 1, [a if st.vals[i[1] - 1][0] == a else min(a, st.vals[i[1] - 1][0] + i[2]), b if st.vals[i[1] - 1][1] == b else min(b, st.vals[i[1] - 1][1] + i[2])])
    else:
        if i[1] < 2:
            ans_left = 0
        else:
            ans_left = st.query(0, i[1] - 2)[1]
        if i[1] > n - k:
            ans_right = 0
        else:
            ans_right = st.query(i[1] + k - 1, n - 1)[0]
        print(ans_left + ans_right)