Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Areas on the Cross-Section Diagram

  Your task is to simulate a flood damage.

  For a given cross-section diagram, reports areas of flooded sections.

  Assume that rain is falling endlessly in the region and the water overflowing from the region is falling in the sea at the both sides.
For example, for the above cross-section diagram, the rain will create floods which have areas of 4, 2, 1, 19 and 9 respectively.

Input

  A string, which represents slopes and flatlands by '/', '\' and '_' respectively, is given in a line. For example, the region of the above example is given by a string "\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\".

output

  Report the areas of floods in the following format:

$A$

$k$ $L_1$ $L_2$ ... $L_k$

  In the first line, print the total area $A$ of created floods.

  In the second line, print the number of floods $k$ and areas $L_i (i = 1, 2, ..., k)$ for each flood from the left side of the cross-section diagram. Print a space character before $L_i$.

Constraints

$1 \leq$ length of the string $\leq 20,000$

Sample Input 1

\\//

Sample Output 1

4
1 4

Sample Input 2

\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\

Sample Output 2

35
5 4 2 1 19 9



Sample input:
\\//


Sample output:
4
1 4

Submission status: Accepted

Solution code:
from collections import deque

section = raw_input( )
depth = 0
area = 0
depthArr = deque( )
areaArr = deque( )

for i, ground in enumerate( section ):
	if "\\" == ground:
		depth += 1
		depthArr.append( i )
	elif 0 < depth and "/" == ground:
		if len( depthArr ):
			j = depthArr.pop( )
			pool =  i - j
			area += pool

			while len( areaArr ):
				preJ, prePool = areaArr.pop( )
				if j <= preJ:
					pool += prePool
				else:
					areaArr.append( ( preJ, prePool ) )
					break
			areaArr.append( ( j, pool ) )

print( area )
output = []
output.append( len( areaArr ) )
for arr in areaArr:
	output.append( arr[1] )
print( " ".join( map( str, output ) ) )

Submission status: Accepted

Solution code:
from collections import deque

section = input( )
depth = 0
area = 0
depthArr = deque( )
areaArr = deque( )

for i, ground in enumerate( section ):
	if "\\" == ground:
		depth += 1
		depthArr.append( i )
	elif 0 < depth and "/" == ground:
		if len( depthArr ):
			j = depthArr.pop( )
			pool =  i - j
			area += pool

			while len( areaArr ):
				preJ, prePool = areaArr.pop( )
				if j <= preJ:
					pool += prePool
				else:
					areaArr.append( ( preJ, prePool ) )
					break
			areaArr.append( ( j, pool ) )

print( area )
output = []
output.append( len( areaArr ) )
for arr in areaArr:
	output.append( arr[1] )
print( " ".join( map( str, output ) ) )

Submission status: Accepted

Solution code:
from collections import deque

section = raw_input( )
area = 0
depthPosArr = deque( )
depthPosPoolArr = deque( )

for ipos, ground in enumerate( section ):
	if "\\" == ground:
		depthPosArr.append( ipos )
	elif "/" == ground:
		if len( depthPosArr ):
			jpos = depthPosArr.pop( )
			pool =  ipos - jpos
			area += pool

			while len( depthPosPoolArr ):
				preJPos, prePool = depthPosPoolArr.pop( )
				if jpos <= preJPos:
					pool += prePool
				else:
					depthPosPoolArr.append( ( preJPos, prePool ) )
					break
			depthPosPoolArr.append( ( jpos, pool ) )

print( area )
output = []
output.append( len( depthPosPoolArr ) )
for arr in depthPosPoolArr:
	output.append( arr[1] )
print( " ".join( map( str, output ) ) )

Submission status: Accepted

Solution code:
from collections import deque

section = input( )
area = 0
depthPosArr = deque( )
depthPosPoolArr = deque( )

for ipos, ground in enumerate( section ):
	if "\\" == ground:
		depthPosArr.append( ipos )
	elif "/" == ground:
		if len( depthPosArr ):
			jpos = depthPosArr.pop( )
			pool =  ipos - jpos
			area += pool

			while len( depthPosPoolArr ):
				preJPos, prePool = depthPosPoolArr.pop( )
				if jpos <= preJPos:
					pool += prePool
				else:
					depthPosPoolArr.append( ( preJPos, prePool ) )
					break
			depthPosPoolArr.append( ( jpos, pool ) )

print( area )
output = deque( )
output.append( len( depthPosPoolArr ) )
for arr in depthPosPoolArr:
	output.append( arr[1] )
print( " ".join( map( str, output ) ) )

Submission status: Accepted

Solution code:
visited = {0: -1}
height = 0
pools = []
for i, c in enumerate(input()):
    if c == '\\':
        height -= 1
    elif c == '/':
        height += 1
        if height in visited:
            width = i - visited[height]
            sm = 0
            while pools and pools[-1][0] > visited[height]:
                x, l = pools.pop()
                sm += l
            pools.append((i, sm + width - 1))
    visited[height] = i

print(sum(l for x, l in pools))
if pools:
    print(len(pools), ' '.join(str(l) for x, l in pools))
else:
    print(0)

Submission status: Accepted

Solution code:
cross_section = input()
visited = {0: -1}
height = 0
pools = []
for i, c in enumerate(cross_section):
    if c == '\\':
        height -= 1
    elif c == '/':
        height += 1
        if height in visited:
            width = i - visited[height]
            sm = 0
            while pools and pools[-1][0] > visited[height]:
                _, volume = pools.pop()
                sm += volume
            pools.append((i, sm + width - 1))
    visited[height] = i

print(sum(v for _, v in pools))
print(len(pools), *(v for _, v in pools))

Submission status: Accepted

Solution code:
s = input()
s1, s2 = [], []

ans = 0
for i in range(len(s)):
    if s[i] == "\\":
        s1.append(i)
    if s[i] == "/" and s1:
        j = s1.pop()
        a = i - j
        ans += a
        while s2 and s2[-1][0] > j:
            a += s2.pop()[1]
        s2.append([j, a])
print(ans)
print(len(s2), *(a for j, a in s2))

Submission status: Accepted

Solution code:
X = raw_input()
S1 = []
S2 = []
total = 0
for i, x in enumerate(X):
    if x == '\\':
        S1.append(i)
    elif x == '/' and len(S1) > 0:
        left = S1.pop()
        s = i - left
        total += s
        while len(S2) > 0:
            prev = S2.pop()
            if prev[0] < left:
                S2.append(prev)
                break
            else:
                s += prev[1]
        S2.append((left, s))
print total
result = [len(S2)]
for i in S2:
    result.append(i[1])
print ' '.join(map(str, result))

Submission status: Accepted

Solution code:
import collections

S1 = collections.deque()
s_a = 0

S2 = collections.deque()

for i, j in enumerate(input()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            diff = i - left_edge
            s_a += diff
            new_puddle_a = diff
            while True:
                if S2:
                    if S2[-1][0] > left_edge:
                        new_puddle_a += S2[-1][1]
                        S2.pop()
                    else:
                        break
                else:
                    break
            new_puddle = (left_edge, new_puddle_a)
            S2.append(new_puddle)
        else:
            pass


print(s_a)

num_of_puddles = len(S2)
puddles = str(num_of_puddles)

for i in range(num_of_puddles):
    puddle = S2.popleft()
    puddles += ' {0}'.format(str(puddle[1]))
else:
    print(puddles)

Submission status: Accepted

Solution code:
import collections

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(input()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while True:
                if S2:
                    if S2[-1] > left_edge:
                        S2.pop()
                        new_puddle += S3.pop()
                    else:
                        break
                else:
                    break
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *list(S3))

Submission status: Accepted

Solution code:
import collections

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(input()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while True:
                if S2:
                    if S2[-1] > left_edge:
                        S2.pop()
                        new_puddle += S3.pop()
                    else:
                        break
                else:
                    break
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Submission status: Accepted

Solution code:
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while True:
                if S2:
                    if S2[-1] > left_edge:
                        S2.pop()
                        new_puddle += S3.pop()
                    else:
                        break
                else:
                    break
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Submission status: Accepted

Solution code:
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while S2 and (S2[-1] > left_edge):
                if S2[-1] > left_edge:
                    S2.pop()
                    new_puddle += S3.pop()
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Submission status: Accepted

Solution code:
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while S2 and (S2[-1] > left_edge):
                S2.pop()
                new_puddle += S3.pop()
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Submission status: Accepted

Solution code:
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while S2 and (S2[-1][0] > left_edge):
                new_puddle += S2[-1][1]
                S2.pop()
            S2.append((left_edge, new_puddle))
        else:
            pass


print(sum(j for i, j in S2))

print(len(S2), *(j for i, j in S2))

Submission status: Accepted

Solution code:
# encoding: utf-8


def calc_area(startIndex, endIndex, terrain):
    level = terrain[startIndex]
    area = 0
    for i in xrange(startIndex, endIndex):
        area += (level - terrain[i]) + 0.5 * (terrain[i] - terrain[i + 1])
    return int(area)


def check_area(terrain):
    i = 0
    startIndex = 0
    endIndex = 0
    while i < len(terrain) - 1:
        if terrain[i + 1] - terrain[i] < 0:
            startIndex = i
            break
        i += 1
    for j in xrange(i + 1, len(terrain)):
        if terrain[i] == terrain[j]:
            endIndex = j
            return startIndex, endIndex
    else:
        return 0, 0


def main():
    string = list(raw_input())
    terrain = []
    height = 0
    for c in string:
        terrain.append(height)
        if c == "\\":
            height -= 1
        elif c == "/":
            height += 1
    terrain.append(height)

    waterAreaRight = []
    while True:
        i, j = check_area(terrain)
        if i == j:
            break
        waterAreaRight.append(calc_area(i, j, terrain))
        terrain = terrain[j:]

    terrain = terrain[::-1]
    waterAreaLeft = []
    while True:
        i, j = check_area(terrain)
        if i == j:
            break
        waterAreaLeft.append(calc_area(i, j, terrain))
        terrain = terrain[j:]
    waterAreaRight.extend(waterAreaLeft[::-1])

    print sum(waterAreaRight)
    print len(waterAreaRight),
    if len(waterAreaRight) > 0:
        print " ".join(map(str, waterAreaRight))

main()

Submission status: Accepted

Solution code:
# coding: utf-8
from collections import deque
input_line = input().strip()
stack1, stack2 = deque(), deque()
for index, i in enumerate(input_line):
    if i == "\\":
        stack1.append(index)
    elif i == "/" and len(stack1) != 0:
        a = stack1.pop()
        #池のマージ
        if len(stack2) != 0 and abs(stack2[-1][0] - a) == 1:
            stack2.append([a, index - a + stack2.pop()[1]])
        else:
            stack2.append([a, index - a])
        #さらにマージ
        while len(stack2) >= 2 and stack2[-1][0] < stack2[-2][0]:
            stack2.append([stack2[-1][0], stack2.pop()[1] + stack2.pop()[1]])
ans = [i[1] for i in stack2]
print(sum(ans))
ans.insert(0, len(ans))
print(*ans)

Submission status: Accepted

Solution code:
def marge_ponds(lx, area_of_pond):
    global ponds
    if ponds:
        lp = ponds.pop()
        if lp[0] > lx:
            return marge_ponds(lx, area_of_pond + lp[1])
        else:
            ponds.append(lp)
    return area_of_pond


terrains = input().strip()
x, last_x, ponds = 0, [], []

for terrain in terrains:
    if terrain == '\\':
        last_x.append(x)
    elif terrain == '/':
        if last_x:
            lx = last_x.pop()
            area_of_pond = marge_ponds(lx, x - lx)
            ponds.append((lx, area_of_pond))
    x += 1

print(sum(pond[1] for pond in ponds))
ponds.insert(0, (0, len(ponds)))
print(' '.join(map(str, [pond[1] for pond in ponds])))

Submission status: Accepted

Solution code:
from collections import deque


def marge_ponds(lx, area_of_pond):
    global ponds
    if ponds and ponds[-1][0] > lx:
        return marge_ponds(lx, area_of_pond + ponds.pop()[1])
    return area_of_pond


terrains = input().strip()
x, last_x, ponds = 0, deque(), deque()

for terrain in terrains:
    if terrain == '\\':
        last_x.append(x)
    elif terrain == '/':
        if last_x:
            lx = last_x.pop()
            ponds.append((lx, marge_ponds(lx, x - lx)))
    x += 1

print(sum(pond[1] for pond in ponds))
ponds.appendleft((0, len(ponds)))
print(' '.join(map(str, [pond[1] for pond in ponds])))

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S1 = deque([])
    S2 = deque([])
    Sum = 0
    S = raw_input()
    for i,j in enumerate(S):
        if j == '\\': S1.append(i)
        elif j == '/' and len(S1) > 0 :
            k = S1.pop()
            Sum += i - k
            a = i - k
            while len(S2)>0 and S2[len(S2)-1][0] > k:
                a += S2.pop()[1]
            S2.append([k,a])

    ans = []
    while len(S2)>0:
        ans.append(S2.pop()[1])
    ans = ans[::-1]
    print sum(ans)
    print len(ans),
    for i in xrange(len(ans)):
        print ans[i],
    print
    return 0

main()

Submission status: Accepted

Solution code:
line = input()
stack, edges, pools = [], [], []

for i in range(len(line)):
    if line[i] == '\\':
        stack.append(i)
    elif line[i] == '/' and stack:
        j = stack.pop()
        tmp = i - j
        while edges and edges[-1] > j:
            edges.pop()
            tmp += pools.pop()
        edges.append(j)
        pools.append(tmp)

print(sum(pools))
print(len(pools), *pools)

Submission status: Accepted

Solution code:
def calc(list):
    #print list
    d = 0
    a = 0.0
    for c in list:
        if c == "\\":
            d += 1
            a += d - 1 + 0.5
        elif c == "_":
            a += d
        else:
            a += d - 1 + 0.5
            d -= 1
    return a

if __name__ == "__main__":
    line = raw_input()
    areas = []
    mode = False
    d = 0
    edge = 0
    for i in range(len(line)):
        #print i, line[i], d, edge, mode
        c = line[i]
        if mode:
            if c == "\\":
                d += 1
            elif c == "/":
                d -= 1
                if d == 0:
                    areas.append(calc(line[edge:i + 1]))
                    mode = False
        else:
            if c == "\\":
                d += 1
                edge = i
                mode = True
            elif c == "_":
                edge = i
            else:
                edge = i

    last = len(areas)
    high = edge
    if d != 0:
        d = 0
        edge = len(line) - 1
        mode = False
        for i in range(len(line) - 1 , high - 1, -1):
            #print i, line[i], d, edge, mode
            c = line[i]
            if mode:
                if c == "/":
                    d += 1
                elif c == "\\":
                    d -= 1
                    if d == 0:
                        #print i, edge
                        areas.insert(last, calc(line[i:edge + 1]))
                        mode = False
            else:
                if c == "/":
                    d += 1
                    edge = i
                    mode = True
                elif c == "_":
                    edge = i
                else:
                    edge = i

    if len(areas) == 0:
        print 0
        print 0
    else:
        areas = map(int, areas)
        print sum(areas)
        print len(areas),
        for i in range(len(areas) - 1):
            print areas[i],
        print areas[len(areas) - 1]

Submission status: Accepted

Solution code:
def main():
    a = 0
    i = 0
    s = []
    u = []
    l = raw_input()
    for c in l:
        if c == '\\':
            s.append(i)
        elif c == '/' and len(s) > 0:
            j = s.pop()
            t = i - j
            a += t
            while len(u) > 0 and u[-1][0] > j:
                t += u.pop()[1]
            u.append((j, t))
        i += 1
    u.insert(0, (0, len(u)))

    print(a)
    print(' '.join(map(lambda x: str(x[1]), u)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

l = input()
S1, S2 = [], []

sum = 0
n = len(l)
for i in range(n):
	if l[i] == "\\":
		S1.append(i)
	elif l[i] == "/" and S1:
		j = S1.pop()
		a = i - j
		sum += a

		while S2 and S2[-1][0] > j:
			a += S2.pop()[1]
		S2.append([j, a])

print(sum)
print(len(S2), *(a for j, a in S2))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

if __name__ == '__main__':

	l = input()
	S1, S2 = [], []
	
	sum = 0
	n = len(l)
	for i in range(n):
		if l[i] == "\\":
			S1.append(i)
		elif l[i] == "/" and S1:
			j = S1.pop()
			a = i - j
			sum += a
	
			while S2 and S2[-1][0] > j:
				a += S2.pop()[1]
			S2.append([j, a])
	
	print(sum)
	print(len(S2), *(a for j, a in S2))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

line = map(str, raw_input())
place = 0
area = 0
stack1 = []
stack2 = []
for i in line:
    if i == '\\':
        stack1.append(place)
    elif i == '/':
        if len(stack1) != 0:
            j = stack1.pop()
            s = place-j
            area += s
            while len(stack2) != 0:
                if stack2[-1][0] <= j:
                    break
                tmp = stack2.pop()
                s += tmp[1]
            stack2.append([j, s])
    place += 1
print area
temp = []
for a in stack2:
    temp.append(a[1])
if len(stack2) > 0:
    print len(stack2),
    print " ".join(map(str, temp))
else:
    print len(stack2)

Submission status: Accepted

Solution code:
from collections import deque

s = input()

d = deque()
l = deque()


def calc_area(j, i):
    return i - j


for i, c in enumerate(s):
    if c == "\\":
        d.append(i)
    if c == "/":
        if len(d) == 0:
            continue

        j = d.pop()

        s = calc_area(j, i)
        while len(l) > 0:
            _j, _i, _s = l.pop()
            if j < _j:
                s += _s
            else:
                l.append((_j, _i, _s))
                break

        l.append((j, i, s))

ans = str(len(l))
sum_area = 0
for j, i, s in l:
    ans += " " + str(s)
    sum_area += s

print(sum_area)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == '__main__':
    s = input()
    d = deque()
    l = deque()

    for i, c in enumerate(s):
        if c == "\\":
            d.append(i)
        if c == "/":
            if len(d) == 0:
                continue

            j = d.pop()

            s = i - j
            while len(l) > 0:
                _j, _i, _s = l.pop()
                if j < _j:
                    s += _s
                else:
                    l.append((_j, _i, _s))
                    break

            l.append((j, i, s))

    # sum all areas
    ans = str(len(l))
    sum_area = 0
    for j, i, s in l:
        ans += " " + str(s)
        sum_area += s

    # print answer
    print(sum_area)
    print(ans)

Submission status: Accepted

Solution code:
from collections import deque

def scan(que, ps, way):
    Li = 0
    pp = ps
    while len(que) > 0:
        if way==0:
            p = que.popleft()
        else:
            p = que.pop()
        if p < ps:
            Li += ps - p
        else:
            break
        pp = p
    return Li

if __name__=='__main__':
    D = input()
    que = deque()
    p = 0
    for c in D:
        que.append(p)
        if   c == "\\" : p += -1
        elif c == "/"  : p +=  1
    que.append(p)
    
    Ll = [] 
    Lr = [] 
    ls = que.popleft()
    if len(que) > 0: rs = que.pop()
    while len(que) > 0:
        while len(que) > 0:
            if que[0] < ls:
                break
            else:
                ls = que.popleft()
        while len(que) > 0:
            if que[-1] < rs:
                break
            else:
                rs = que.pop()
        if len(que) > 0:
            if ls < rs: Ll.append(scan(que,ls,0))
            else      : Lr.insert(0,scan(que,rs,1))
    L = Ll + Lr
    if len(L) > 0:
        print(sum(L))
        print(len(L), *L)
    else:
        print(0)
        print(0)

Submission status: Accepted

Solution code:
DOWN = []
EDGE = []
POOL = []

for i, c in enumerate(input()):
    if c == '\\':
        DOWN.append(i)
    elif c == '/':
        if DOWN:
            e = DOWN.pop()
            Li = i - e # Calcurate 1 line.  \______/ 
            while EDGE: # Merge connected pools.
                if EDGE[-1] > e:
                    EDGE.pop()
                    Li += POOL.pop()
                else:
                    break
            EDGE.append(e)
            POOL.append(Li)

print(sum(POOL))
print(len(POOL), *POOL)

Submission status: Accepted

Solution code:
# input
section = raw_input()

# map the height of the section
height = 0
heights = [0]
for p in xrange( len(section) ):
    if   section[p] == '/':
        height += 1
    elif section[p] == '\\':
        height -= 1
    heights.append(height)

# search the pools
pools = []
water = []
p = 0
while p < len(section):
    if section[p] == '\\':
        try:
            pr = heights[p+1:].index( heights[p] )
            pools.append([p, p+1+pr])
            p = p+1+pr
        except:
            p+=1
    else:
        p+=1

# count
for pool in pools:
    tp = section[pool[0]:pool[1]]
    depth = 0
    s = 0
    for p in xrange( len(tp) ):
        if   tp[p] == '/':
            depth -= 1
        elif tp[p] == '\\':
            depth += 1
            s += 2*(depth-1)+1
        else:
            s += depth
    water.append(s)

# print
print sum(water)
if len(water) == 0:
    print len(water)
else:
    print len(water), " ".join(map(str, water))

Submission status: Accepted

Solution code:
S = raw_input()
S1, S2 = [], []
 
ans = pool = 0
for i in xrange(len(S)):
    if S[i] == "/" and len(S1) > 0:
        j = S1.pop()
        ans += i - j
        a = i - j
        while (len(S2) > 0 and S2[-1][0] > j):
            a += S2.pop()[1]
        S2.append([j, a])
    if S[i] == "\\":
        S1.append(i)
print ans
if len(S2) > 0:
    print len(S2), " ".join(map(str, [a for j, a in S2]))
else:
    print 0

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_D
sample_input = list(range(3))
# sample_input[0] = '''\\//'''
# sample_input[1] = '''\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\'''
# sample_input[2] = ''''''

sample_input[0] = '''\\\\//'''
sample_input[1] = '''\\\\///\\_/\\/\\\\\\\\/_/\\\\///__\\\\\\_\\\\/_\\/_/\\'''
sample_input[2] = ''''''

give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        
# main
# a list of tuples (h, l, r)
# h = the hight
# l, r = max of heights of points which is left (right) or equal to this point
terrain_data = []

str_input = input()
list_input = list(str_input)

height = 0
left_max_height = 0
terrain_data.append([height,left_max_height, None])
for path in list_input:
    if path == '/':
        height += 1
    elif path == '\\':
        height -= 1
    left_max_height = max(left_max_height, height)
    terrain_data.append([height,left_max_height, None])

right_max_height = None # dealt as negative infinity
for index in range(len(terrain_data)-1, -1, -1):
    height = terrain_data[index][0]
    if right_max_height is None:
        right_max_height = height
    else:
        right_max_height = max(right_max_height, height)
    terrain_data[index][2] = right_max_height

pool_left = -1
pool_right = -1
area = 0
areas = []
prev_depth = 0
for data in terrain_data:
    depth = min(data[1], data[2]) - data[0]
    area += (prev_depth + depth)/2
    if depth == 0:
        if pool_left < pool_right:
            areas.append(int(area))
            area = 0
        pool_right += 1
        pool_left = pool_right
    else:
        pool_right += 1
    prev_depth = depth

print(sum(areas))
output = str(len(areas)) + ' '
for a in areas:
    output += str(a) + ' '
print(output[:-1])

Submission status: Accepted

Solution code:
from collections import deque
area = input()
S1 = deque()
S2 = deque()
total = 0

for i, v in enumerate(area):
    if v == '\\':
        S1.append(i)
    elif v == '/' and S1:
        j = S1.pop()
        subtotal = i - j
        total += i - j
        while S2 and S2[-1][0] > j:
            subtotal += S2.pop()[1]
        S2.append([j, subtotal])

print(total)
print(len(S2), *(subtotal for j, subtotal in S2))

Submission status: Accepted

Solution code:
class Stack:
    def __init__(self):
        self.stack = []

    def push(self, value):
        self.stack.append(value)

    def pop(self):
        return self.stack.pop(-1)

    @property
    def isEmpty(self):
        if len(self.stack) == 0:
            return True
        else:
            return False

def main():
    s = input()
    stack_grad = Stack()
    stack_vol = Stack()
    sum_vol = 0

    for i, c in enumerate(s):
        if c == '\\':
            stack_grad.push(i)
        elif c == '/':
            if not stack_grad.isEmpty:
                previ = stack_grad.pop()
                vol = i - previ
                sum_vol += vol
                while not stack_vol.isEmpty:
                    tpl = stack_vol.pop()
                    if previ < tpl[0]:
                        vol += tpl[1]
                    else:
                        stack_vol.push(tpl)
                        break
                stack_vol.push((previ, vol))

    print(sum_vol)
    stack_vol.stack.reverse()
    out = str(len(stack_vol.stack))
    while not stack_vol.isEmpty:
        out += ' {}'.format(stack_vol.pop()[1])
    print(out)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

s1 = []
s2 = []
area = []

for i, c in enumerate(sys.stdin.readline()[:-1]):
    if c == '\\':
        s1.append(i)
    elif c == '/' and len(s1) > 0:
        j = s1.pop()
        a = i - j
        while s2:
            if s2[-1] > j:
                s2.pop()
                a += area.pop()
            else:
                break
        s2.append(j)
        area.append(a)

print(sum(area))
print(len(area), *area)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

I = list(input())

A = 0
L = []
LH = []
k = 0
while len(I) > 0:
    i = I.pop()

    if i == '/':
        d = 1
        a = 0.5
        tmp_I = ['/']

        # print("[{}] d: {}".format(i, d))

        while len(I) > 0:
            i = I.pop()

            tmp_I.append(i)

            if i == '/':
                d += 1
                a += 0.5 + d - 1
            elif i == '\\':
                a += 0.5 + d - 1
                d -= 1
                if d == 0:
                    A += a
                    L.append(a)
                    k += 1
                    tmp_I = []
                    break
            elif i == '_':
                a += d

            # print("[{}] d: {}".format(i, d))

        # print("{}".format(tmp_I))

        if len(tmp_I) > 0:
            d = a = 0
            while len(tmp_I) > 0:
                i = tmp_I.pop()
                # print("{}".format(i))
                if i == '\\':
                    d += 1
                    a += 0.5 + d - 1
                elif i == '_':
                    a += d
                elif i == '/' and d > 0:
                    a += 0.5 + d - 1
                    d -= 1

                    if d == 0:
                        # print("Add tmp: {} ({})".format(a, tmp_I))
                        A += a
                        LH.append(a)
                        k += 1
                        a = 0

print(int(A))
sys.stdout.write(str(k))
for l in LH:
    sys.stdout.write(" {}".format(int(l)))
while len(L) > 0:
    sys.stdout.write(" {}".format(int(L.pop())))
sys.stdout.write('\n')

Submission status: Accepted

Solution code:
import sys
from collections import deque

data = list(sys.stdin.readline()[:-1])

ponds = deque()
result = deque()
index = 0
amount = 0
lefs = deque()

for i in data:
    if i == "\\" or i == "_":
        ponds.append([i, index])
    else:
        count = 0
        while 1 <= len(ponds):
            pond, ind = ponds.pop()
            if pond == "\\":
                count += (index - ind)
                amount += count
                pre_pond = 0
                pre_left = 0
                while 0 < len(lefs) and ind < lefs[-1]:
                    pre_left = lefs.pop()
                    pre_pond += result.pop()
                lefs.append(ind)
                result.append(count + pre_pond)
                break
    index += 1

print(amount)
if 0 == len(result):
    print(0)
else:
    print(len(result), ' '.join(map(str, result)))

Submission status: Accepted

Solution code:
s = input()
s1, s2 = [], []

ans = 0
for i in range(len(s)):
    if s[i] == '\\':
        s1.append(i)
    if s[i] == '/' and s1:
        j = s1.pop()
        a = i - j
        ans += a
        while s2 and s2[-1][0] > j:
            a += s2.pop()[1]
        s2.append([j, a])
print(ans)
print(len(s2), *(a for j, a in s2))