Problem:
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

Solution (Python):
from enum import Enum
import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

class Info:
    def __init__(self,arg_start,arg_end,arg_S):
        self.start = arg_start
        self.end = arg_end
        self.S = arg_S


LOC = []
POOL = []

line = input()
loc = 0
sum_S = 0

for ch in line:

    if ch == '\\':

        LOC.append(loc)

    elif ch == '/':

        if len(LOC) == 0:
            continue

        tmp_start = int(LOC.pop())
        tmp_end = loc
        tmp_S = tmp_end-tmp_start;
        sum_S += tmp_S;

        
        while len(POOL) > 0:
            if POOL[-1].start > tmp_start and POOL[-1].end < tmp_end:
                tmp_S += POOL[-1].S
                POOL.pop()
            else:
                break

        POOL.append(Info(tmp_start,tmp_end,tmp_S))

    else:
        pass

    loc += 1

print("%d"%(sum_S))
print("%d"%(len(POOL)),end = "")

while len(POOL) > 0:
    print(" %d"%(POOL[0].S),end = "") 
    POOL.pop(0)

print()

Solution (Python):
s=j=0
a=[];b=[];d=[]
for i,x in enumerate(input()):
    if x=='\\':a+=[i]
    elif x=='/' and a:
        j=a.pop()
        c=i-j;s+=c
        while b and b[-1]>j:c+=d.pop();b.pop()
        b+=[j];d+=[c]
print(s)
print(len(b),*(d))

Solution (Python):
S = input()
m = 0
cur = 0
a = [(-1, 0, 0)]

for n, s in enumerate(S):

    if s == '/':
        area = 0
        cur += 1

        while a and a[-1][2] < cur <= m:
            area += a.pop()[1]

        if a and a[-1][2] == cur:
            area += (n - a[-1][0] - 1)
        a.append((n, area, cur))
        m = max(m, cur)

    else:
        if s == '\\': cur -= 1
        a.append((n, 0, cur))
l = [str(x[1]) for x in a if x[1] != 0]
print(sum([x[1] for x in a]))
if len(l):
    print(
        '{} {}'.format(
            len(l),
            ' '.join(l)
        )
    )
else:
    print(0)

Solution (Python):
s=j=0  
a =[] 
b =[] 
d =[] 

for i, x in enumerate(input()):
  if x == '\\':
    a += [i]
  elif x =='/' and a:
    j = a.pop()
    c = i-j
    s += c
    while b and b[-1] > j:
      c+=d.pop()
      b.pop()
    b +=[j]
    d +=[c]
print(s)
print(len(b), *(d))

Solution (Python):
terrain = list(input())

down_slopes = []
puddles = []
total_cross_section = 0

for (position, slope) in enumerate(terrain):
    if slope == '\\' :
        down_slopes.append(position)
    elif slope == '/' and down_slopes:
        last_position = down_slopes.pop()
        total_cross_section += position - last_position
        
        puddle_cross_section = 0
        while puddles:
            puddle = puddles.pop()
            if puddle[0] <= last_position:
                puddles.append(puddle)
                break
            puddle_cross_section += puddle[1]
        puddle_cross_section += position - last_position
        puddles.append([last_position,puddle_cross_section])
        
print(total_cross_section)
ans = [str(len(puddles))]
ans.extend( [str(puddle[1]) for puddle in puddles ] )
print(' '.join(ans ) )

Solution (Python):
from collections import deque

A = input()
s1 = deque()
c = 0
i = 1

left = None
s2 = deque()
for a in A:
    if a == '\\':
        s1.append(i)
    elif a == '/':
        if len(s1) > 0:
            j = s1.pop()
            
            area = i - j
            
            c += area
            while len(s2) > 0:
                t = s2.pop()
                if t[0] > j:
                    area += t[1]
                else:
                    s2.append(t)
                    break
            s2.append([j, area])
            
    else:
        pass
    i += 1
print(c)
print(len(s2), end='')
for t in s2:
    print(' ', end='')
    print(t[1], end='')
print()

Solution (Python):



def calc(s):
    shape = [ch for ch in s]

    left_x = []
    pool_area = []
    for x, sh in enumerate(shape):
        if sh == "\\":
            left_x.append(x)
        elif left_x and sh == "/":
            lx = left_x.pop()
            area = x - lx
            while pool_area and (pool_area[-1])[0] > lx:
                area += (pool_area.pop())[1]
            pool_area.append((lx, area))
        

    return [p[1] for p in pool_area]

def test():
    assert calc(r"\ ".strip()) == []
    assert calc(r"/ ".strip()) == []
    assert calc(r"\/ ".strip()) == [1]
    assert calc(r"\_/ ".strip()) == [2]
    assert calc(r"\// ".strip()) == [1]
    assert calc(r"\\/ ".strip()) == [1]
    assert calc(r"\\// ".strip()) == [4]
    assert calc(r"\\//\\// ".strip()) == [4, 4]
    assert calc(r"\\/\// ".strip()) == [7]
    assert calc(r"\\/_\// ".strip()) == [8]
    assert calc(r"\//\\/ ".strip()) == [1, 1]
    assert calc(r"\//_\\/ ".strip()) == [1, 1]
    assert calc(r"\//\\// ".strip()) == [1, 4]
    assert calc(r"\///\\\// ".strip()) == [1, 4]
    assert calc(r"\///\\\//\ ".strip()) == [1, 4]
    exit()




pool_area = calc(input())
print(sum(pool_area))
print(len(pool_area), *pool_area)

Solution (Python):
import sys

n = ''
n = str(input())

darr = []
sp = []
s = []
for i in range(len(n)):
    if n[i] == '\\':
        darr.append(i)
    elif n[i] == '/':
        if len(darr) == 0:
            continue
        pre = darr.pop() 
        tmps = i - pre
        while len(sp) > 0 and sp[-1] > pre:
            sp.pop()
            tmps += s.pop()
        sp.append(pre)
        s.append(tmps)

print(sum(s))
arr = []
arr.append(len(s))
for i in s:
    arr.append(int(i))
print(' '.join(map(str, arr)))

Solution (Python):
def print_area(stack):
    if len(stack) == 0:
        print(len(stack))
    else:
        print(len(stack), end=" ")
    for i,j in enumerate(stack):
        if i == len(stack)-1:
            print(j[1])
        else:
            print(j[1], end=" ")    

def calc_flood_area(in_data):
    stack1 = []
    stack2 = []
    total_area = 0
    for i,symbol in enumerate(in_data):
        if symbol == "\\":
            stack1.append(i)
        elif symbol == "_":
            pass
        elif symbol == "/" and stack1:
            j = stack1.pop()
            a = int(i)-int(j)
            total_area += a
            while stack2 and stack2[-1][0] > j:
                a += stack2[-1][1]
                stack2.pop()
            stack2.append((j, a))
                
    print(total_area)
    print_area(stack2)

in_data = input()
calc_flood_area(in_data)

Solution (Python):
class Pool:
    def __init__(self, pos, area):
        self.pos = pos
        self.area = area


data = input()

s1 = []
s2 = []
area_sum = 0
for i, v in enumerate(data):

    if v == '\\':
        s1.append(i)
    elif v == '/' and len(s1) > 0:
        j = s1.pop()
        area = i - j
        area_sum += area
        while len(s2) > 0 and s2[-1].pos > j:
            area += s2.pop().area
        s2.append(Pool(j, area))

print(area_sum)
print(len(s2), end='')
if len(s2):
    print(' ' + ' '.join([str(v.area) for v in s2]))
else:
    print()

Solution (Python):

s=input()
a=[]
d=[]
sum=0
for i,line in enumerate(s):
    if line == "\\":
        d.append(i)
    elif line == "/" and d:
        j=d.pop()
        a_tmp=i-j
        sum+=a_tmp
        while a and a[-1][0] > j:
            a_tmp += a.pop()[1]
        a.append([j,a_tmp])
 
print(sum)
print(len(a),*[s for i,s in a])

Solution (Python):
tmp=input()
l1=[]
l2=[]
Asum=0
Area=[]

for i in range(len(tmp)):
	if tmp[i] is "\\":
		l1.append(i)
	elif tmp[i] is "/" and len(l1) is not 0:
		a1=l1.pop()
		sum=i-a1
		Asum+=sum
		while len(l2)>0:
			if l2[-1]>a1:
				l2.pop()
				sum+=Area.pop()
			else:
				break

		Area.append(sum)
		l2.append(i)
print(Asum)
print(len(Area),*Area)

Solution (Python):

s=input()
stk=[]
data=[]
for i in range(len(s)):
    if s[i]=='\\':
        stk.append(i)
    elif s[i]=='/':
        if len(stk):
            data.append((stk.pop(),i))
print(sum(map(lambda x:x[1]-x[0],data)))
ans=[]
sv=-1
for p in data[::-1]:
    if sv==-1:
        sv=p
        ans.append(p[1]-p[0])
    elif sv[0]<p[0]<p[1]<sv[1]:
        ans[-1]+=p[1]-p[0]
    else:
        sv=p
        ans.append(p[1]-p[0])
print(len(ans[::-1]),end=' ' if len(ans[::-1])!=0 else '')
print(*ans[::-1])

Solution (Python):
from collections import deque
section = input()
depth = 0
area = 0
depthArr = deque()
areaArr = deque()
for i, drct in enumerate(section):
    if drct == '\\':
        depth += 1
        depthArr.append(i)
    
    elif 0 < depth and drct == '/':
        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(*output)