Alice and Hairdresser (Codeforces 1055B)
Tags: Implement, dsu

Alice's hair is growing by leaps and bounds. Maybe the cause of it is the excess of vitamins, or maybe it is some black magic...To prevent this, Alice decided to go to the hairdresser. She wants for her hair length to be at most $$$l$$$ centimeters after haircut, where $$$l$$$ is her favorite number. Suppose, that the Alice's head is a straight line on which $$$n$$$ hairlines grow. Let's number them from $$$1$$$ to $$$n$$$. With one swing of the scissors the hairdresser can shorten all hairlines on any segment to the length $$$l$$$, given that all hairlines on that segment had length strictly greater than $$$l$$$. The hairdresser wants to complete his job as fast as possible, so he will make the least possible number of swings of scissors, since each swing of scissors takes one second.Alice hasn't decided yet when she would go to the hairdresser, so she asked you to calculate how much time the haircut would take depending on the time she would go to the hairdresser. In particular, you need to process queries of two types:  $$$0$$$ — Alice asks how much time the haircut would take if she would go to the hairdresser now.  $$$1$$$ $$$p$$$ $$$d$$$ — $$$p$$$-th hairline grows by $$$d$$$ centimeters. Note, that in the request $$$0$$$ Alice is interested in hypothetical scenario of taking a haircut now, so no hairlines change their length.

Input: The first line contains three integers $$$n$$$, $$$m$$$ and $$$l$$$ ($$$1 \le n, m \le 100\,000$$$, $$$1 \le l \le 10^9$$$) — the number of hairlines, the number of requests and the favorite number of Alice.The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$) — the initial lengths of all hairlines of Alice.Each of the following $$$m$$$ lines contains a request in the format described in the statement.The request description starts with an integer $$$t_i$$$. If $$$t_i = 0$$$, then you need to find the time the haircut would take. Otherwise, $$$t_i = 1$$$ and in this moment one hairline grows. The rest of the line than contains two more integers: $$$p_i$$$ and $$$d_i$$$ ($$$1 \le p_i \le n$$$, $$$1 \le d_i \le 10^9$$$) — the number of the hairline and the length it grows by.

Output: For each query of type $$$0$$$ print the time the haircut would take.

Note: Consider the first example:  Initially lengths of hairlines are equal to $$$1, 2, 3, 4$$$ and only $$$4$$$-th hairline is longer $$$l=3$$$, and hairdresser can cut it in $$$1$$$ second.  Then Alice's second hairline grows, the lengths of hairlines are now equal to $$$1, 5, 3, 4$$$  Now haircut takes two seonds: two swings are required: for the $$$4$$$-th hairline and for the $$$2$$$-nd.  Then Alice's first hairline grows, the lengths of hairlines are now equal to $$$4, 5, 3, 4$$$  The haircut still takes two seconds: with one swing hairdresser can cut $$$4$$$-th hairline and with one more swing cut the segment from $$$1$$$-st to $$$2$$$-nd hairline.  Then Alice's third hairline grows, the lengths of hairlines are now equal to $$$4, 5, 4, 4$$$  Now haircut takes only one second: with one swing it is possible to cut the segment from $$$1$$$-st hairline to the $$$4$$$-th. 

Sample Inputs:
4 7 3
1 2 3 4
0
1 2 3
0
1 1 3
0
1 3 1
0

Sample Outputs:
1
2
2
1

Solution (Python):
from sys import stdin, stdout

def main(input=stdin.readlines, print=stdout.write, map=map, int=int, str=str):
    (lines, answer) = (input(), [])
    (n, m, l) = map(int, lines[0].split())
    a = [-1] + list(map(int, lines[1].split())) + [-1]
    x = sum((1 for q in range(n + 1) if a[q] > l >= a[q - 1]))
    for q1 in lines[2:]:
        if q1 == '0\n':
            answer.append(str(x))
        else:
            (w2, w, w1) = map(int, q1.split())
            if a[w] + w1 > l >= a[w]:
                x += (a[w - 1] <= l) + (a[w + 1] <= l) - 1
            a[w] += w1
    print('\n'.join(answer))
main()

Solution (Python):
import sys

def main(map=map, int=int, str=str):
    lines = sys.stdin.readlines()
    (n, m, l) = map(int, lines[0].split())
    a = [-1]
    a += map(int, lines[1].split())
    a.append(-1)
    n += 2
    time = sum((1 for i in range(1, n - 1) if a[i] > l and a[i - 1] <= l))
    res = []
    for i in range(2, m + 2):
        if lines[i] == '0\n':
            res.append(str(time))
        else:
            (_, p, d) = map(int, lines[i].split())
            if a[p] <= l and a[p] + d > l:
                cond = (a[p - 1] <= l) + (a[p + 1] <= l)
                time += cond == 2
                time -= cond == 0
            a[p] += d
    sys.stdout.write('\n'.join(res))
main()

Solution (Python):
from sys import stdin, stdout

def main(input=stdin.readlines, print=stdout.write, map=map, int=int, str=str):
    (lines, answer) = (input(), [])
    (n, m, l) = map(int, lines[0].split())
    a = [-1] + list(map(int, lines[1].split())) + [-1]
    x = sum((1 for q in range(n + 1) if a[q] > l >= a[q - 1]))
    for q1 in lines[2:]:
        if q1 == '0\n':
            answer.append(str(x))
        else:
            (w2, w, w1) = map(int, q1.split())
            if a[w] + w1 > l >= a[w]:
                x += (a[w - 1] <= l) + (a[w + 1] <= l) - 1
            a[w] += w1
    print('\n'.join(answer))
main()