Greg and Array (Codeforces 295A)
Tags: Implement, data structures

Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.

Input: The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).The numbers in the lines are separated by single spaces.

Output: On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.


Sample Inputs:
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3

1 1 1
1
1 1 1
1 1

4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3

Sample Outputs:
9 18 17

2

5 18 31 20

Solution (Python):
from collections import defaultdict
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.buffer.readline())

def MI():
    return map(int, sys.stdin.buffer.readline().split())

def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def BI():
    return sys.stdin.buffer.readline().rstrip()

def SI():
    return sys.stdin.buffer.readline().rstrip().decode()

def li():
    return [int(i) for i in input().split()]

def lli(rows):
    return [li() for _ in range(rows)]

def si():
    return input()

def ii():
    return int(input())

def ins():
    return input().split()

def solve():
    (n, m, k) = LI()
    a = LI()
    op = []
    for _ in range(m):
        op.append(LI())
    cop = [0] * m
    for _ in range(k):
        (x, y) = LI()
        cop[x - 1] += 1
        if y < m:
            cop[y] -= 1
    for i in range(1, m):
        cop[i] += cop[i - 1]
    for i in range(m):
        op[i][2] *= cop[i]
    addi = [0] * n
    for i in range(m):
        addi[op[i][0] - 1] += op[i][2]
        if op[i][1] < n:
            addi[op[i][1]] -= op[i][2]
    for i in range(1, n):
        addi[i] += addi[i - 1]
    ans = [str(a[i] + addi[i]) for i in range(n)]
    print(' '.join(ans))

def main():
    solve()
if __name__ == '__main__':
    main()

Solution (Python):
from itertools import accumulate
import sys
import os
if os.path.exists('input.txt'):
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')
    sys.stderr = open('error.txt', 'w')
input = lambda : sys.stdin.readline().rstrip()
(n, m, k) = map(int, input().split())
lst = list(map(int, input().split()))
qu = []
gg = [0] * (n + 10)
h = [0] * (m + 1)
for i in range(m):
    qu.append(list(map(int, input().split())))
for i in range(k):
    (a, b) = map(int, input().split())
    h[a - 1] += 1
    h[b] -= 1
ah = list(accumulate(h))
for j in range(m):
    gg[qu[j][0] - 1] += qu[j][2] * ah[j]
    gg[qu[j][1]] -= qu[j][2] * ah[j]
f = list(accumulate(gg))
for i in range(n):
    lst[i] += f[i]
print(*lst)

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(1):
    (n, m, k) = map(int, input().split())
    a = list(map(int, input().split()))
    b = []
    for i in range(m):
        (l, r, d) = map(int, input().split())
        b.append([l, r, d])
    oper = [0] * (m + 1)
    for i in range(k):
        (x, y) = map(int, input().split())
        oper[x - 1] += 1
        oper[y] -= 1
    for i in range(1, m + 1):
        oper[i] += oper[i - 1]
    diff = [0] * (n + 1)
    for i in range(m):
        x = b[i][2] * oper[i]
        (l, r) = (b[i][0] - 1, b[i][1])
        diff[l] += x
        diff[r] -= x
    for i in range(n):
        x = a[i]
        if i > 0:
            diff[i] += diff[i - 1]
        x += diff[i]
        print(x, end=' ')