Factory Repairs (Codeforces 627B)
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
from math import *

def minp():
    return sys.stdin.readline().strip()

def mint():
    return int(minp())

def mints():
    return map(int, minp().split())

def add(a, x, v):
    while x < len(a):
        a[x] += v
        x |= x + 1

def get(a, x):
    r = 0
    while x >= 0:
        r += a[x]
        x = (x & x + 1) - 1
    return r
(n, k, a, b, q) = mints()
h1 = [0] * n
h2 = [0] * n
z = [0] * n
for i in range(q):
    t = tuple(mints())
    if t[0] == 1:
        p = z[t[1] - 1]
        pp = p + t[2]
        add(h1, t[1] - 1, min(a, pp) - min(a, p))
        add(h2, t[1] - 1, min(b, pp) - min(b, p))
        z[t[1] - 1] = pp
    else:
        print(get(h2, t[1] - 2) + get(h1, n - 1) - get(h1, t[1] + k - 2))

Solution (Python):
N = 4 * 10 ** 5 + 10
s = [[0] * N for i in range(2)]
(x, y) = ([0] * N, [0] * N)

def modify(q: int, x: int, v: int, n: int) -> None:
    while x <= n:
        s[q][x] += v
        x = (x | x - 1) + 1

def find_sum(q: int, x: int) -> int:
    ret = 0
    while x > 0:
        ret += s[q][x]
        x &= x - 1
    return ret
(n, k, a, b, q) = map(int, input().split())
for _ in range(q):
    ls = list(map(int, input().split()))
    if ls[0] == 1:
        (day, cnt) = ls[1:]
        new_x = min(b, cnt + x[day])
        new_y = min(a, cnt + y[day])
        modify(0, day, new_x - x[day], n)
        modify(1, day, new_y - y[day], n)
        (x[day], y[day]) = (new_x, new_y)
    else:
        fr = ls[1]
        to = fr + k - 1
        ans = find_sum(0, fr - 1)
        ans += find_sum(1, n) - find_sum(1, to)
        print(ans)

Solution (Python):
import os, sys, math, random, threading
from copy import deepcopy
from io import BytesIO, IOBase
from types import GeneratorType
from functools import lru_cache, reduce
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heapify, heappop, heappush
from typing import Generic, Iterable, Iterator, TypeVar, Union, List
import types
BUFSIZE = 4096

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
sys.stdin = IOWrapper(sys.stdin)
sys.stdout = IOWrapper(sys.stdout)
mod = int(1000000000.0 + 7)
inf = int(1e+20)
input = lambda : sys.stdin.readline().rstrip('\r\n')
MI = lambda : map(int, input().split())
li = lambda : list(MI())
ii = lambda : int(input())
py = lambda : print('YES')
pn = lambda : print('NO')
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
DIRS = [(0, 1), (1, 0), (0, -1), (-1, 0)]
DIRS8 = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]

class BIT1:
    """"""
    __slots__ = ('size', 'bit', 'tree')

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

    def add(self, index: int, delta: int) -> None:
        while index <= self.size:
            self.tree[index] += delta
            index += index & -index

    def _query(self, index: int) -> int:
        res = 0
        while index > 0:
            res += self.tree[index]
            index -= index & -index
        return res

    def query(self, left: int, right: int) -> int:
        return self._query(right) - self._query(left - 1)
(n, k, a, b, q) = li()
arr = [0] * (n + 1)
bcnt = BIT1(n + 5)
bsum = BIT1(n + 5)
acnt = BIT1(n + 5)
asum = BIT1(n + 5)
for i in range(1, n + 1):
    acnt.add(i, 1)
    bcnt.add(i, 1)
for _ in range(q):
    op = li()
    if op[0] == 1:
        (x, y) = op[1:]
        if arr[x] < a:
            acnt.add(x, -1)
            asum.add(x, -arr[x])
        if arr[x] < b:
            bcnt.add(x, -1)
            bsum.add(x, -arr[x])
        arr[x] += y
        if arr[x] < a:
            acnt.add(x, 1)
            asum.add(x, arr[x])
        if arr[x] < b:
            bcnt.add(x, 1)
            bsum.add(x, arr[x])
    else:
        x = op[1]
        ans = 0
        if x > 1:
            ans += bsum.query(1, x - 1) + (x - 1 - bcnt.query(1, x - 1)) * b
        if x + k <= n:
            ans += asum.query(x + k, n) + (n - (x + k) + 1 - acnt.query(x + k, n)) * a
        print(ans)