Watching Fireworks is Fun (Codeforces 372C)
Tags: Math, data structures

A festival will be held in a town's main street. There are n sections in the main street. The sections are numbered 1 through n from left to right. The distance between each adjacent sections is 1.In the festival m fireworks will be launched. The i-th (1 ≤ i ≤ m) launching is on time ti at section ai. If you are at section x (1 ≤ x ≤ n) at the time of i-th launching, you'll gain happiness value bi - |ai - x| (note that the happiness value might be a negative value).You can move up to d length units in a unit time interval, but it's prohibited to go out of the main street. Also you can be in an arbitrary section at initial time moment (time equals to 1), and want to maximize the sum of happiness that can be gained from watching fireworks. Find the maximum total happiness.Note that two or more fireworks can be launched at the same time.

Input: The first line contains three integers n, m, d (1 ≤ n ≤ 150000; 1 ≤ m ≤ 300; 1 ≤ d ≤ n).Each of the next m lines contains integers ai, bi, ti (1 ≤ ai ≤ n; 1 ≤ bi ≤ 109; 1 ≤ ti ≤ 109). The i-th line contains description of the i-th launching.It is guaranteed that the condition ti ≤ ti + 1 (1 ≤ i < m) will be satisfied.

Output: Print a single integer — the maximum sum of happiness that you can gain from watching all the fireworks.Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.


Sample Inputs:
50 3 1
49 1 1
26 1 4
6 1 10

10 2 1
1 1000 4
9 1000 4

Sample Outputs:
-31

1992

Solution (Python):
class SortedList(list):

    def add(self, other):
        left = -1
        right = len(self)
        while right - left > 1:
            mid = right + left >> 1
            if other < self[mid]:
                right = mid
            else:
                left = mid
        super().insert(right, other)
INF = int(3e+18)

def solve_good(n, m, d, a, b, t):
    left = SortedList()
    left.append(-INF)
    right = SortedList()
    right.append(INF)
    lborder = -INF
    rborder = INF
    tprev = 0
    ans = 0
    for (ai, bi, ti) in zip(a, b, t):
        ans += bi
        dt = ti - tprev
        interval = dt * d
        tprev = ti
        lborder += interval
        rborder -= interval
        lefta = lborder + ai
        righta = rborder - (n - ai)
        if lefta < left[-1]:
            top = left.pop()
            ans -= abs(top - lefta)
            left.add(lefta)
            left.add(lefta)
            right.add(rborder - (n - abs(top - lborder)))
        elif righta > right[0]:
            top = right.pop(0)
            ans -= abs(top - righta)
            right.add(righta)
            right.add(righta)
            left.add(lborder + n - abs(top - rborder))
        else:
            left.add(lefta)
            right.add(righta)
    return ans
(n, m, d) = [int(elem) for elem in input().split()]
(a, b, t) = ([], [], [])
for i in range(m):
    (ai, bi, ti) = [int(elem) for elem in input().split()]
    a.append(ai)
    b.append(bi)
    t.append(ti)
print(solve_good(n, m, d, a, b, t))

Solution (Python):
from collections import deque

def rollingmax(x, y, r, a):
    k = 2 * r + 1
    d = deque()
    lx = len(x)
    for i in range(lx + r):
        if i < lx:
            while d and d[-1][1] <= x[i]:
                d.pop()
            d.append((i, x[i]))
        while d and d[0][0] <= i - k:
            d.popleft()
        if i >= r:
            y[i - r] = d[0][1] - abs(i - r - a)
(n, m, d) = [int(x) for x in input().split()]
(a, ball, t0) = [int(x) for x in input().split()]
f = [-abs(i - a) for i in range(1, n + 1)]
g = [0] * n
for _ in range(m - 1):
    (a, b, t) = [int(x) for x in input().split()]
    ball += b
    r = min(n - 1, (t - t0) * d)
    t0 = t
    rollingmax(f, g, r, a - 1)
    (f, g) = (g, f)
print(max(f) + ball)

Solution (Python):
from collections import deque
(n, m, v) = map(int, input().split())
(x, t, b, bt, dp, mi, mi2, mi3, dpmin, dp2) = ([0] * 300, [0] * 300, 0, 0, [[0] * 2 for i in range(150001)], 0, 100000000000000, 10000000000000, 0, [0] * 150001)
d = deque()
for i in range(m):
    (x[i], b, t[i]) = map(int, input().split())
    bt += b
for i2 in range(m - 1):
    if i2 == 0:
        for i in range(1, n + 1):
            dp[i][0] = abs(i - x[0])
            if mi2 > dp[i][0]:
                mi2 = dp[i][0]
    if m == 1:
        break
    if (t[i2 + 1] - t[i2]) * v >= n:
        mi3 = mi2
        mi2 = 1000000000000000000
        for i in range(1, n + 1):
            dp[i][0] = mi3 + abs(i - x[i2 + 1])
            if mi2 > dp[i][0]:
                mi2 = dp[i][0]
        continue
    mi2 = 1000000000000000000
    for i in range(1, n + 1 + (t[i2 + 1] - t[i2]) * v):
        if i <= n:
            while len(d) > 0 and dp[i][0] <= d[len(d) - 1][0]:
                d.pop()
            dp[i][1] = i + 2 * (t[i2 + 1] - t[i2]) * v + 1
            d.append(dp[i])
        if d[0][1] == i:
            d.popleft()
        if i - (t[i2 + 1] - t[i2]) * v >= 1:
            dp2[i - (t[i2 + 1] - t[i2]) * v] = d[0][0] + abs(x[i2 + 1] - (i - (t[i2 + 1] - t[i2]) * v))
    for i in range(1, n + 1):
        dp[i][0] = dp2[i]
        if dp2[i] < mi2:
            mi2 = dp2[i]
    d.clear()
for i in range(1, n + 1):
    if i == 1:
        mi = dp[i][0]
    if dp[i][0] < mi:
        mi = dp[i][0]
print(bt - mi)