Mahmoud and Ehab and the function (Codeforces 862E)
Tags: Others, sortings

Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array a of length n and array b of length m. He introduced a function f(j) which is defined for integers j, which satisfy 0 ≤ j ≤ m - n. Suppose, ci = ai - bi + j. Then f(j) = |c1 - c2 + c3 - c4... cn|. More formally, . Dr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid j. They found it a bit easy, so Dr. Evil made their task harder. He will give them q update queries. During each update they should add an integer xi to all elements in a in range [li;ri] i.e. they should add xi to ali, ali + 1, ... , ari and then they should calculate the minimum value of f(j) for all valid j.Please help Mahmoud and Ehab.

Input: The first line contains three integers n, m and q (1 ≤ n ≤ m ≤ 105, 1 ≤ q ≤ 105) — number of elements in a, number of elements in b and number of queries, respectively.The second line contains n integers a1, a2, ..., an. ( - 109 ≤ ai ≤ 109) — elements of a.The third line contains m integers b1, b2, ..., bm. ( - 109 ≤ bi ≤ 109) — elements of b.Then q lines follow describing the queries. Each of them contains three integers li ri xi (1 ≤ li ≤ ri ≤ n,  - 109 ≤ x ≤ 109) — range to be updated and added value.

Output: The first line should contain the minimum value of the function f before any update.Then output q lines, the i-th of them should contain the minimum value of the function f after performing the i-th update .

Note: For the first example before any updates it's optimal to choose j = 0, f(0) = |(1 - 1) - (2 - 2) + (3 - 3) - (4 - 4) + (5 - 5)| = |0| = 0.After the first update a becomes {11, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(11 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6) = |9| = 9.After the second update a becomes {2, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(2 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6)| = |0| = 0.After the third update a becomes {1, 1, 2, 3, 4} and it's optimal to choose j = 0, f(0) = |(1 - 1) - (1 - 2) + (2 - 3) - (3 - 4) + (4 - 5)| = |0| = 0.

Sample Inputs:
5 6 3
1 2 3 4 5
1 2 3 4 5 6
1 1 10
1 1 -9
1 5 -1

Sample Outputs:
0
9
0
0

Solution (Python):
import sys, bisect

def read():
    return map(int, sys.stdin.readline().split())
(n, m, q) = read()
(a, b) = ([*read()], [*read()])
fa = sum((-a[i] if i & 1 else a[i] for i in range(n)))
fb = [sum((-b[i] if i & 1 else b[i] for i in range(n)))]
for j in range(1, m - n + 1):
    fb.append(-fb[-1] + b[j - 1] + (b[n - 1 + j] if n & 1 else -b[n - 1 + j]))
fb.sort()

def z():
    idx = bisect.bisect_right(fb, fa)
    print(min(abs(fb[idx % len(fb)] - fa), abs(fa - fb[idx - 1])))
z()
for _ in range(q):
    (l, r, v) = read()
    fa += (l % 2 + r % 2 - 1) * v
    z()

Solution (Python):
import bisect
(l, m, q) = map(int, input().split())
s = list(map(int, input().split()))
p = list(map(int, input().split()))
t = 0
for (i, n) in enumerate(s):
    if i % 2 == 1:
        t -= n
    else:
        t += n
f = set()
k = 0
for (i, n) in enumerate(p):
    if i < l:
        if i % 2 == 0:
            k += n
        else:
            k -= n
        if i == l - 1:
            f.add(k)
    else:
        if l % 2 == 1:
            k = -k + p[i - l] + p[i]
        else:
            k = -k + p[i - l] - p[i]
        f.add(k)
f = sorted(f)

def func(t):
    idx = bisect.bisect_left(f, t)
    if idx == 0:
        return f[0] - t
    elif idx == len(f):
        return t - f[-1]
    else:
        return min(t - f[idx - 1], f[idx] - t)
print(func(t))
for _ in range(q):
    (i, j, x) = map(int, input().split())
    if i % 2 == 0:
        if j % 2 == 0:
            t -= x
    if i % 2 == 1:
        if j % 2 == 1:
            t += x
    print(func(t))

Solution (Python):
def read():
    return list(map(int, input().split(' ')))
(n, m, q) = read()
aa = read()
bb = read()
reqs = [read() for _ in range(q)]
asum = 0
bsum = 0
for (i, (a, b)) in enumerate(zip(aa, bb)):
    asum += a if i % 2 == 0 else -a
    bsum += b if i % 2 == 0 else -b
bpos = [bsum]
for i in range(len(aa), len(bb)):
    b = bb[i]
    rempos = i - len(aa)
    bsum += b if i % 2 == 0 else -b
    bsum -= bb[rempos] if rempos % 2 == 0 else -bb[rempos]
    bpos += [bsum if rempos % 2 == 1 else -bsum]
bpos = sorted(set(bpos))

def closest(arr, value):
    l = 0
    r = len(arr)
    while l + 1 < r:
        m = (l + r) // 2
        if arr[m] <= value:
            l = m
        else:
            r = m
    res = arr[l]
    if l + 1 < len(arr) and abs(arr[l + 1] - value) < abs(arr[l] - value):
        res = arr[l + 1]
    return res
print(abs(asum - closest(bpos, asum)))
for req in reqs:
    (l, r, x) = req
    l -= 1
    if (r - l) % 2 != 0:
        asum += x if l % 2 == 0 else -x
    print(abs(asum - closest(bpos, asum)))