Monoblock (Codeforces 1715C)
Tags: Math, combinatorics

Stanley has decided to buy a new desktop PC made by the company "Monoblock", and to solve captcha on their website, he needs to solve the following task.The awesomeness of an array is the minimum number of blocks of consecutive identical numbers in which the array could be split. For example, the awesomeness of an array   $$$[1, 1, 1]$$$ is $$$1$$$;  $$$[5, 7]$$$ is $$$2$$$, as it could be split into blocks $$$[5]$$$ and $$$[7]$$$;  $$$[1, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9]$$$ is 3, as it could be split into blocks $$$[1]$$$, $$$[7, 7, 7, 7, 7, 7, 7]$$$, and $$$[9, 9, 9, 9, 9, 9, 9, 9, 9]$$$. You are given an array $$$a$$$ of length $$$n$$$. There are $$$m$$$ queries of two integers $$$i$$$, $$$x$$$. A query $$$i$$$, $$$x$$$ means that from now on the $$$i$$$-th element of the array $$$a$$$ is equal to $$$x$$$.After each query print the sum of awesomeness values among all subsegments of array $$$a$$$. In other words, after each query you need to calculate $$$$$$\sum\limits_{l = 1}^n \sum\limits_{r = l}^n g(l, r),$$$$$$ where $$$g(l, r)$$$ is the awesomeness of the array $$$b = [a_l, a_{l + 1}, \ldots, a_r]$$$.

Input: In the first line you are given with two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^5$$$).The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array $$$a$$$.In the next $$$m$$$ lines you are given the descriptions of queries. Each line contains two integers $$$i$$$ and $$$x$$$ ($$$1 \leq i \leq n$$$, $$$1 \leq x \leq 10^9$$$).

Output: Print the answer to each query on a new line.

Note: After the first query $$$a$$$ is equal to $$$[1, 2, 2, 4, 5]$$$, and the answer is $$$29$$$ because we can split each of the subsegments the following way:   $$$[1; 1]$$$: $$$[1]$$$, 1 block;  $$$[1; 2]$$$: $$$[1] + [2]$$$, 2 blocks;  $$$[1; 3]$$$: $$$[1] + [2, 2]$$$, 2 blocks;  $$$[1; 4]$$$: $$$[1] + [2, 2] + [4]$$$, 3 blocks;  $$$[1; 5]$$$: $$$[1] + [2, 2] + [4] + [5]$$$, 4 blocks;  $$$[2; 2]$$$: $$$[2]$$$, 1 block;  $$$[2; 3]$$$: $$$[2, 2]$$$, 1 block;  $$$[2; 4]$$$: $$$[2, 2] + [4]$$$, 2 blocks;  $$$[2; 5]$$$: $$$[2, 2] + [4] + [5]$$$, 3 blocks;  $$$[3; 3]$$$: $$$[2]$$$, 1 block;  $$$[3; 4]$$$: $$$[2] + [4]$$$, 2 blocks;  $$$[3; 5]$$$: $$$[2] + [4] + [5]$$$, 3 blocks;  $$$[4; 4]$$$: $$$[4]$$$, 1 block;  $$$[4; 5]$$$: $$$[4] + [5]$$$, 2 blocks;  $$$[5; 5]$$$: $$$[5]$$$, 1 block;  which is $$$1 + 2 + 2 + 3 + 4 + 1 + 1 + 2 + 3 + 1 + 2 + 3 + 1 + 2 + 1 = 29$$$ in total.

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

Sample Outputs:
29
23
35
25
35

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

def solve():
    (n, m) = map(int, input().split())
    arr = list(map(int, input().split()))
    dp = ans = 1
    for i in range(1, n):
        dp += 1 if arr[i - 1] == arr[i] else i + 1
        ans += dp
    for _ in range(m):
        (i, x) = map(int, input().split())
        i -= 1
        if arr[i] == x:
            print(ans)
        else:
            if i > 0:
                if arr[i - 1] == arr[i]:
                    ans += i * (n - i)
                elif x == arr[i - 1]:
                    ans -= i * (n - i)
            if i < n - 1:
                if arr[i + 1] == arr[i]:
                    ans += (i + 1) * (n - i - 1)
                elif x == arr[i + 1]:
                    ans -= (i + 1) * (n - i - 1)
            print(ans)
            arr[i] = x
    return
solve()

Solution (Python):
line = input().strip('\n\r')
ss = line.split(' ')
(n, m) = (int(ss[0]), int(ss[1]))
line = input().strip('\n\r')
ss = line.split(' ')
A = [int(x) for x in ss]

def init(A):
    dp = 0
    ans = 0
    for i in range(0, n):
        dp += 1
        if A[i] != A[i - 1]:
            dp += i
        ans += dp
    return ans

def solve(A, M, s):
    idx = M[0] - 1
    left = idx
    right = n - idx - 1
    v = M[1]
    if A[idx] == v:
        return s
    ans = s
    if idx > 0:
        if A[idx] == A[idx - 1]:
            ans += left
            ans += left * right
        if v == A[idx - 1]:
            ans -= left
            ans -= left * right
    if idx < n - 1:
        if A[idx] == A[idx + 1]:
            ans += right
            ans += left * right
        if v == A[idx + 1]:
            ans -= right
            ans -= left * right
    A[idx] = v
    return ans
Q = init(A)
ans = []
for _ in range(m):
    line = input().strip('\n\r')
    ss = line.split(' ')
    M = [int(ss[0]), int(ss[1])]
    Q = solve(A, M, Q)
    ans.append(Q)
print('\n'.join([str(x) for x in ans]))

Solution (Python):
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
pre = [0] * (n - 1)
ans = n * (n + 1) // 2
res = []
for i in range(n - 1):
    if a[i] != a[i + 1]:
        pre[i] = (i + 1) * (n - i - 1)
        ans += pre[i]
for j in range(m):
    (i, x) = map(int, input().split())
    i -= 1
    if x != a[i]:
        if i > 0 and a[i] == a[i - 1] and (x != a[i]):
            pre[i - 1] = i * (n - i)
            ans += pre[i - 1]
        elif i > 0 and a[i] != a[i - 1] and (x == a[i - 1]):
            ans -= pre[i - 1]
            pre[i - 1] = 0
        if i < n - 1 and a[i] == a[i + 1] and (x != a[i + 1]):
            pre[i] = (i + 1) * (n - i - 1)
            ans += pre[i]
        elif i < n - 1 and a[i] != a[i + 1] and (x == a[i + 1]):
            ans -= pre[i]
            pre[i] = 0
        a[i] = x
    res.append(ans)
print('\n'.join([str(i) for i in res]))