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

S = input()


s1 = deque()
s2 = deque()
total = 0

for i, s in enumerate(S):
    if s == '\\':
        s1.append(i)
    elif s == '/':
        if not s1:
            continue
        j = s1.pop()
        total += i - j

        vol = i - j
        while s2 and s2[-1][0] >= j:
            vol += s2.pop()[1]

        s2.append((j, vol))

print(total)
print(len(s2), *[vol for idx, vol in s2])

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
S = input()

l1 = []
l2 = []
total_area = 0
for i, j in enumerate(S):
    if j == '\\':
        l1.append(i)
    elif j == '/' and l1:
        i_p = l1.pop()
        v = i -i_p
        total_area += v
    
        while l2 and l2[-1][0] > i_p:
            v += l2.pop()[1]
        l2.append([i_p, v])
        
ans = [str(len(l2))] + [str(k[1]) for k in l2]
print(total_area)
print(' '.join(ans))

Submission status: Accepted

Solution code:
S = list(input())
areas = 0
cnt = 0
stack_1 = []
stack_2 = []

for i in range(len(S)):
    if S[i] == '\\':
        stack_1.append(i)
    elif S[i] == '/' and len(stack_1) > 0:
        j = stack_1.pop()
        area = i - j
        areas += area
        while len(stack_2) > 0 and stack_2[-1][0] > j:
            area += stack_2.pop()[1]
        stack_2.append([j, area])

print(areas)
if stack_2:
    print(len(stack_2), *list(zip(*stack_2))[1])
else:
    print(0)

Submission status: Accepted

Solution code:
class Flood:
    """
    begin : int
        水たまりの始まる場所
    area : int
        水たまりの面積
    """
    def __init__(self, begin, area):
        self.begin = begin
        self.area = area

down_index_stack = []  # \の場所
flood_stack = []  # (水たまりの始まる場所, 面積)


for i, c in enumerate(input()):
    if c == '\\':
        down_index_stack.append(i)
    if c == '/':
        if len(down_index_stack) >= 1:  # 現在の/に対応する\が存在したら

            l = down_index_stack.pop()  # 現在の水たまりの始まる場所
            area = i - l  # 現在の水たまりの現在の高さの部分の面積はi-l

            # 現在の水たまりが最後の水たまりを内包している間は
            # (現在の水たまりの始まる場所が最後の水たまりの始まる場所より前にある間は)
            while len(flood_stack) >= 1 and l < flood_stack[-1].begin:
                # 最後の水たまりを現在の水たまりに取り込む
                last_flood = flood_stack.pop()
                area += last_flood.area

            flood_stack.append(Flood(l, area))  # 現在の水たまりを最後の水たまりにして終了


area_list = [flood.area for flood in flood_stack]
print(sum(area_list))
print(' '.join(list(map(str, [len(area_list)] + area_list))))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# ALDS1_3_D_Areas on the Cross-Section Diagram

from collections import deque

def make_h_list(slope):
    """
    入力データから高さのリストを作る
    """
    h_list =deque([0])
    for s in slope:
        d = 0
        if s == "\\":
            d = -1
        elif s == "/":
            d = 1
        h_list.append(h_list[-1] + d)
    return h_list

def trim_edge(h_list, left=True):
    """
    水溜りの境界を探す
    """
    if left:
        while h_list:
            h = h_list.popleft()
            if h not in h_list:
                continue
            if h_list[0] - h < 0:
                h_list.appendleft(h)
                return h_list
    else:
        while h_list:
            h = h_list.pop()
            if h not in h_list:
                continue
            if h_list[-1] - h < 0:
                h_list.append(h)
                return h_list
            
def make_w_list(h_list):
    """
    水たまりのリストを作る
    """
    w_list = []
    water = []
    h_list = trim_edge(h_list)
    while h_list:
        h = h_list.popleft()
        if not water:
            water.append(h)
        elif water[0] == h:
            water.append(h)
            w_list.append(water)
            water = []
            h_list.appendleft(h)
            h_list = trim_edge(h_list)
        else:
            water.append(h)
            if water[0] < water[-1]:
                water.pop(0)
    return w_list
                
def calc_area(water):
    """
    面積計算
    """
    h = water[0]
    s = 0
    for i in range(len(water) - 1):
        s += (water[i+1] - h) + (water[i+1] - water[i]) / 2
    return abs(int(s))


in_data = str(input())

h_list = make_h_list(in_data)
h_list = trim_edge(h_list, left=False)

if type(h_list) != None:
    w_list = make_w_list(h_list)
    a_list = [calc_area(water) for water in w_list]
else:
    a_list = []

print(sum(a_list))
print(len(a_list), *a_list)

Submission status: Accepted

Solution code:
S = input()
left, right, area = [], [], []
for i, s in enumerate(list(S)):
    if s == '\\':
        left.append(i)
    if s == '/' and left != []:
        j = left.pop()
        a = i - j
        while right != [] and j < right[-1]:
            right.pop()
            a += area.pop()
        right.append(j)
        area.append(a)
print(sum(area))
print(len(area), *area)

Submission status: Accepted

Solution code:
string = input()

s = []
total = 0
s2 = []
for i in range(len(string)):
    if string[i] == '\\':
        s.append(i)
    elif string[i] == '/' and len(s) > 0:
        j = s.pop() # j: 対応する '\' の位置
        area = i - j # 面積
        total += area
        while len(s2) > 0 and s2[-1][0] > j: # s2[-1][0] == s2.pop()[0]
            area += s2[-1][1]
            s2.pop()
        s2.append([j, area]) # j: 水たまりの左端 / tmp: その水たまりの面積

ans = []
for i in range(len(s2)):
    ans.append(str(s2[i][1]))

print(total)
if len(ans) == 0:
    print(len(ans))
else:
    print(len(ans), end = ' ')
    print(' '.join(ans))

Submission status: Accepted

Solution code:
from collections import deque

T = input()
A = deque()
d = 0
s = 0
for i, token in enumerate(T):
    if token == "\\":
        s += d + 0.5
        if not d:
            l = i
        d += 1
    elif token == "/":
        if not d:
            continue
        d -= 1
        s += d + 0.5
        if not d:
            A.append((l, s))
            s = 0
    else:
        s += d
B = deque()
d = 0
s = 0
for i in range(len(T) - 1, -1, -1):
    token = T[i]
    if token == "/":
        s += d + 0.5
        d += 1
    elif token == "\\":
        if not d:
            continue
        d -= 1
        s += d + 0.5
        if not d:
            B.appendleft((i, s))
            s = 0
    else:
        s += d
S = set(A) | set(B)
S = list(S)
S.sort()
S = [round(s[1]) for s in S]
print(sum(S))
print(len(S), *S)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
from collections import deque

class peke:
    def __init__(self,Start,End,S):
        self.start=Start
        self.end=End
        self.S=S
    

S1=deque()
S2=deque()
total=0
A=input()

for n in range(len(A)):
    if A[n]=='\\':
        S1.append(n)
    elif A[n]=='/' and S1:
        j=S1.pop()
        now=n-j
        total=total+now
        while S2:
            if S2[-1].start>j and S2[-1].end<n:
                now=now+S2[-1].S
                S2.pop()
            else:
                break
        S2.append(peke(j,n,now))

print(total)
print(len(S2),end ="")
while S2:
    print(" ",end="")
    print(S2[0].S,end="")
    S2.popleft()
print()

Submission status: Accepted

Solution code:
def main():
    S = input()
    stack1, stack2 = [], []
    
    sum = 0
    N = len(S)
    for i in range(N):
        if S[i] == "\\":
            stack1.append(i)
        elif S[i] == "/" and stack1:
            j = stack1.pop()
            tmp = i - j
            sum += tmp
            
            while stack2 and stack2[-1][0] > j:
                tmp += stack2.pop()[1]
            stack2.append([j, tmp])
            
    print(sum)
    print(len(stack2), *[a for j, a in stack2])
    

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from queue import LifoQueue

MAP = input()

que = LifoQueue()
res = LifoQueue()

for i, m in enumerate(MAP):
    if m=='\\':
        que.put(i)
    elif m=='/':
        if not que.empty():
            j = que.get(False)
            v = i - j
            t = (j, v)
            while not res.empty():
                pre = res.get(False)
                if (pre[0] > j):
                    t = (t[0], t[1] + pre[1])
                else:
                    res.put(pre)
                    res.put(t)
                    break
            else:
                res.put(t)

summaly = 0
lakes = []
while not res.empty():
    v = res.get()
    lakes.append(v[1])
    summaly += v[1]

print(summaly)
print(len(lakes), *(reversed(lakes)))

Submission status: Accepted

Solution code:
import sys
import math
import copy
from typing import Tuple, List, Union
input = sys.stdin.readline

class Area:
    def __init__(self, startidx: int, area: int):
        self.startidx = startidx
        self.area = area
    def __repr__(self):
        return "{}".format(self.area)


def main():
    edgeStack = []
    areaStack = []
    line = input()
    for idx, item in enumerate(line):
        if item is "\\":
            edgeStack.append(idx)
        elif item is "_":
            pass
        elif item is "/":
            if len(edgeStack) < 1:
                continue
            prevarea = 0
            previdx = edgeStack[-1]
            while (len(areaStack) > 0
                   and len(edgeStack) > 0
                   and areaStack[-1].startidx > edgeStack[-1]):
                area = areaStack.pop(-1)
                prevarea += area.area
            areaStack.append(Area(edgeStack.pop(-1), prevarea + idx - previdx))
    return areaStack


if __name__ == '__main__':
    areaStack = main()
    print(sum([area.area for area in areaStack]))
    if len(areaStack):
        print(len(areaStack), end=" ")
    else:
        print(len(areaStack), end="")
    print(" ".join([str(area) for area in areaStack]))

Submission status: Accepted

Solution code:
strs = input()
h = 0
heights = [0]
max_h = 0
k = []
sum = 0
for i, s in enumerate(strs, 1):
    if s=='\\':
        h -= 1
        heights.append(h)
    elif s=='/':
        h += 1
        heights.append(h)
        if h>max_h:
          max_h = h
          continue
        t = -2
        while t+i+1>=0:
            if heights[t]==h:
                sum += (-t-3)+1
                tmp = (-t-3)+1
                if len(k)>0:
                  c = len(k)
                  for ts, te, tm in k[::-1]:
                    c -= 1
                    if t+i+1<ts and te<i:
                        k.pop(c)
                        tmp += tm
                k.append([t+i+1, i, tmp])
                break
            else:
                t -= 1
                continue
    else:
        heights.append(h)
        continue
print(sum)
ans = [str(k[-1]) for k in k]
print(len(ans), *ans)

Submission status: Accepted

Solution code:
#alds13c
from collections import deque

d_stack=deque()
res_stack=deque()
s = input()

for i in range(len(s)):
    #print(d_stack,res_stack)
    if s[i]=="\\":
        d_stack.append(i)
    elif s[i]=="/":
        if len(d_stack)==0:
            continue
        left = d_stack.pop()
        area = i-left
        #res_stack.append((left,area))
        if len(res_stack)>0:
            flag=True
            #merge_candidate = []
            mergeareasum=0
            while flag:
                if len(res_stack)>0 and left<res_stack[-1][0]:
                    mc = res_stack.pop()
                    mergeareasum += mc[1]
                    #res_stack.append((left,under[1]+area))
                else:
                    flag = False
                    res_stack.append((left,area+mergeareasum))
        else:
                res_stack.append((left,area))

ans=0
v_devided=[]
for pair in res_stack:
    ans += pair[1]
    v_devided.append(str(pair[1]))
print(ans)
if len(v_devided)>0:
    print(len(v_devided)," ".join(v_devided))
else:
    print(0)

Submission status: Accepted

Solution code:
S = input()
st = [(0, 0, -1)]
cur = 0; ma = 0
for i, c in enumerate(S):
    if c == '/':
        cur += 1
        s = 0
        while st and st[-1][0] == cur-1 and cur <= ma:
            lev, su, j = st.pop()
            s += su
        if st and st[-1][0] == cur:
            s += i - st[-1][2] - 1
        ma = max(ma, cur)
        st.append((cur, s, i))
    elif c == '\\':
        cur -= 1
        st.append((cur, 0, i))
    elif c == '_':
        st.append((cur, 0, i))
ans = []
for lev, su, j in st:
    if su > 0:
        ans.append(su)
print(sum(ans))
print(len(ans), *ans)

Submission status: Accepted

Solution code:
class Pond:
    def __init__(self, down_x, area):
        super().__init__()
        self.down_x = down_x
        self.area = area


s = []
ponds = []
all_area = 0
x = 0
for c in input():
    if "\\" == c:
        s.append(x)
    elif "/" == c and s:
        down_x = s.pop()
        area = x - down_x
        all_area += area
        while ponds and ponds[-1].down_x > down_x:
            area += ponds[-1].area
            ponds.pop()
        ponds.append(Pond(down_x, area))
    x += 1
print(all_area)
if ponds:
    print(len(ponds), ' '.join(map(lambda pond: str(pond.area), ponds)))
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def main():
    t = input().strip()
    s = []
    # A = 0
    ans = []
    prevA = 0
    for i in range(len(t)):
        if t[i] == '\\':
            s.append(i)
        elif len(s) > 0 and t[i] == '/':
            p = s.pop()
            k = i - p
            ans.append((p, k))
            q = 0
            while len(ans) > 0:
                # print(ans)
                g, h = ans[-1]
                if g >= p:
                    q += h
                    ans.pop()
                else:
                    break
            # for j in range(len(ans) - len(s)):
            #     print('i: ', i)
            #     print('j: ', j)
            #     print('ans: ', ans)
            #     q += ans.pop()
                # print('q: ', q)
            if q != 0:
                ans.append((p, q))
        # elif len(s) > 0 and t[i] == '_':
        #     A += 1

    v = 0
    for i in range(len(ans)):
        v += ans[i][1]
    print(v)
    print(len(ans), end='')
    for i in range(len(ans)):
        print(' {}'.format(ans[i][1]), end='')
    print('')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

def area(map):
    total = 0
    s1 = deque()
    s2 = deque()
    for i in range(len(map)):
        if map[i] == "\\":
            s1.append(i)    # 右斜面の要素番号を格納
        elif len(s1) > 0 and map[i] == "/":
            old = s1.pop()  # 水面の高さ(仮)を取り出す
            tmp = 0
            while len(s2) > 0 and old < s2[-1][0]:
                tmp += s2.pop()[1]  # 水没地帯の面積を計算(横に輪切りにするイメージで下から計算)
            s2.append([old, tmp + i - old]) # s2[[水面の始点, 水没地帯の面積(水面以外の面積 + (水面の終点 - 水面の始点)]]

    # 合計の水没面積を計算
    for i in range(len(s2)):
        total += s2[i][1]
    print(total)

    if s2:
        print(len(s2), end=" ")
        for i in range(len(s2)):
            if i != len(s2)-1:
                print(s2[i][1], end = " ")
            else:
                print(s2[i][1])
    else:
        print(len(s2))


if "__main__" == __name__:
    map = input().rstrip()
    area(map)

Submission status: Accepted

Solution code:
def main():
    inputs = str(input())
    stack = []
    pond = [Pond(-1, 0, 0)]

    for i, string in enumerate(inputs):
        if string == "\\":
            stack.append(i)
        elif string == "/":
            try:
                down = stack.pop()
            except  IndexError as err:
                continue
            pond.append(Pond(down, i, i-down))
            while len(pond) > 1:
                newPond = pond.pop()
                prevPond = pond.pop()
                if prevPond.includedBy(newPond):
                    pond.append(prevPond.merge(newPond))
                else:
                    break
            pond.append(prevPond)
            pond.append(newPond)

    printPonds(pond)

def printPonds(pond):
    size = 0
    eachSize = []
    for p in pond:
        size += p.size
        eachSize.append(p.size)
    eachSize[0] = len(pond)-1
    print(size)
    print(*eachSize)

class Pond:
    def __init__(self, x1, x2, size):
        self.x1 = x1
        self.x2 = x2
        self.size = size

    def merge(self, newPond):
        self.x1 = newPond.x1
        self.x2 = newPond.x2
        self.size += newPond.size
        return self

    def includedBy(self, newPond):
        return self.x1 >= newPond.x1 and self.x2 <= newPond.x2

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
def calculate_area(lines):
    q = deque()
    partial_area = 0
    areas = []
    total_area = 0
    for i, line in enumerate(lines):
        if line == "\\":
            q.append(i)
        elif line == "/":
            if q:
                num = q.pop()
                partial_area = i - num
                total_area += partial_area
                for p_area in areas[::-1]:
                    if p_area[0] > num:
                        partial_area += areas.pop()[1]
                    else:
                        break
                areas.append([num, partial_area])
    areas_strigified = [str(v) for i, v in areas]
    print(total_area)
    if total_area:
        print("{} {}".format(len(areas), ' '.join(areas_strigified)))
    else:
        print(0)
                    

if __name__ == "__main__":
    data = input()
    calculate_area(data)

Submission status: Accepted

Solution code:
import math
import string
import itertools
import fractions
import heapq
import collections
import re
import array
import bisect
import sys
import random
import time
inf = 10**9


def main():
    s = input()

    stack = []
    flat_pools = []
    for (i, c) in enumerate(s):
        if c == '\\':
            stack.append(i)
        elif c == '/' and len(stack) > 0:
            flat_pools.append((stack.pop(), i))

    flat_pools.sort(key=lambda pool: pool[0])

    last_index = 0
    pools = []
    area = 0

    for pool in flat_pools:
        area += pool[1] - pool[0]
        if last_index <= pool[0]:
            pools.append(pool[1] - pool[0])
            last_index = pool[1]
        else:
            pools[-1] = pools[-1] + pool[1] - pool[0]

    pools.insert(0, len(pools))
    print(area)
    print(" ".join(map(str, pools)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#インプットdata
input_data = input()

#初期値
stack_list = list()
area_list = list()
area_sum = 0

#計算の実行
for i in range(len(input_data)):
    if input_data[i] == "\\":
        stack_list.append(i)
    elif len(stack_list) > 0 and input_data[i] == "/":
        temp_area = i - stack_list[-1]
        area_sum += temp_area
        if len(area_list) == 0 or area_list[-1][0] < stack_list[-1]:
            area_list.append([stack_list[-1], temp_area])
        else:
            area_list[-1][0] = stack_list[-1]
            area_list[-1][1] += temp_area
            temp_len = len(area_list)
            for j in range(temp_len-1, 0, -1):
                if area_list[j-1][0] > area_list[j][0]:
                    area_list[j-1][0] = area_list[j][0]
                    area_list[j-1][1] += area_list[j][1]
                    del area_list[j]
                else:
                    break
        del stack_list[-1]

#結果の表示
print(area_sum)
print(len(area_list), end="")
for i in range(len(area_list)):
    print(f" {area_list[i][1]}", end="")
print("")

Submission status: Accepted

Solution code:
down = []
edge = []
pool = []
for i,c in enumerate(input()):
    if c == "\\":
        down.append(i)
    elif c == "/" and down:
        left = down.pop()
        area = i - left
        
        while edge:
            if edge[-1] > left:
                edge.pop()
                area += pool.pop()
            else:
                break
        
        edge.append(left)
        pool.append(area)
print(sum(pool))
print(" ".join(map(str,[len(pool)]+pool)))

Submission status: Accepted

Solution code:
lst = list(input())
v_lakes = []
idx_stack = []
for i, c in enumerate(lst):
    if (c == '\\'):
        idx_stack.append(i)
    elif (idx_stack and c == '/'):
        # 対応する'\\'の位置
        j = idx_stack.pop()
        v = i - j
        while (v_lakes and v_lakes[-1][0] > j):
            v += v_lakes.pop()[1]
        v_lakes.append((j, v))
ans = [len(v_lakes)]
v = [v[1] for v in v_lakes]
ans.extend(v)

print(sum(v))
print(*ans)

Submission status: Accepted

Solution code:
#  Areas on the Cross-Section Diagram

class depth_Char:
    ch = ""
    depth = 0

    def __init__(self,char,depth):
        self.ch = char
        self.depth = depth

    def __str__(self):

        return f"{self.ch}:{self.depth}"


string = input()

depth = 0
d_list = []
for ch in string:
    if ch == "\\":
        depth -= 1
        d_list.append(depth_Char(ch,depth))

    elif ch == "_":
        d_list.append(depth_Char(ch,depth))

    elif ch == "/":
        d_list.append(depth_Char(ch,depth))
        depth += 1

# print(*d_list)

lake_list = []  #池のリストを作る
bslash_dict = dict()
for i,d_ch in enumerate(d_list):

    if d_ch.ch == "\\":
        bslash_dict[d_ch.depth] = i
    elif d_ch.ch == "/" and d_ch.depth in bslash_dict:
        j = bslash_dict[d_ch.depth]

        is_need_append = True
        # print(f"search:{[j,i]}")
        remove_list = []  # foreachループの中では削除せず、最後にまとめて削除する

        for lake in lake_list:
            if is_need_append and lake[0] < j and i < lake[1]:
                is_need_append = False

            if j < lake[0] and lake[1] < i:
                # print(f"remove:{lake}")
                remove_list.append(lake)
        
        for rem in remove_list:
            lake_list.remove(rem)

        if is_need_append:
            lake_list.append([j,i])
        

lake_area_list = []
for i,j in lake_list:
    lake = string[i:j+1]
    t_depth = 0
    area = 0
    for ch in lake:
        if ch == "\\":
            t_depth += 1
            area += t_depth
        elif ch == "_":
            area += t_depth
        elif ch == "/":
            t_depth -= 1
            area += t_depth
    
    lake_area_list.append(area)

print(sum(lake_area_list))
print(len(lake_area_list),*lake_area_list)

Submission status: Accepted

Solution code:
a = input().strip()
stack = []
area = 0
area_list = []

for i, slope in enumerate(a):
    if slope == '\\':
        stack.append(i)
    elif slope == '/' and len(stack) != 0:
        ip = stack.pop(len(stack)-1)
        area += (i - ip)
        a = i - ip
        while len(area_list) != 0 and area_list[len(area_list)-1][0] > ip:
            temp = area_list.pop(len(area_list)-1)
            a += temp[1]
        area_list.append([ip, a])
    
print(area)
print('{}'.format(len(area_list)), end='')
if len(area_list) != 0:
    print(' ', end='')
print(' '.join([str(i[1]) for i in area_list]))

Submission status: Accepted

Solution code:
s=[]
p=[]
a=i=0
for c in input():
     if"\\"==c:
         s+=[i]
     elif"/"==c and s:
         j=s.pop()
         t=i-j
         a+=t
         while p and p[-1][0]>j:
             t+=p[-1][1];
             p.pop()
         p+=[(j,t)]
     i+=1
print(a)
if p:
    print(len(p),*list(zip(*p))[1])
else:
    print(0)

Submission status: Accepted

Solution code:
downhill_stack = []
idx_area_stack = []
for idx, c in enumerate(input()):
    if c == '\\':
        downhill_stack.append(idx)
    elif c == '/' and len(downhill_stack) > 0:
        downhill_idx = downhill_stack.pop()
        current_level_area = idx - downhill_idx
        if len(idx_area_stack) == 0:
            idx_area_stack.append((downhill_idx, current_level_area))
            continue
        last_idx = idx_area_stack[-1][0]
        if downhill_idx < last_idx:
            prov_area = current_level_area
            while downhill_idx < last_idx:
                idx_area = idx_area_stack.pop()
                prov_area += idx_area[1]
                if len(idx_area_stack) == 0:
                    break
                last_idx = idx_area_stack[-1][0]
            prov_idx_area = (downhill_idx, prov_area)
            idx_area_stack.append(prov_idx_area)
        else:
            idx_area_stack.append((downhill_idx, current_level_area))
puddle_num = len(idx_area_stack)
areas = list(map(lambda x: x[1], idx_area_stack))
print(sum(areas))
if puddle_num > 0:
    print(puddle_num, " ".join(map(str, areas)))
else:
    print(puddle_num)

Submission status: Accepted

Solution code:
tochi=input()
s=[]
I=[]

sum=0
n=len(tochi)
for i in range(n):
    if tochi[i] == "\\":
        s.append(i)
    elif tochi[i] == "/" and s:
        j = s.pop()
        a = i - j
        sum += a
	
        while I and I[-1][0] > j:
            a += I.pop()[1]
        I.append([j, a])

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

Submission status: Accepted

Solution code:
N = input()
s1 = []
s2 = []
res = []
ans = []
cnt = 0
for i in range(len(N)):
    if N[i] == "\\":
        s1.append(i)
    elif N[i] == "/" and s1:
        idx = s1.pop()
        tmp = i - idx
        cnt += tmp

        while s2 and idx < s2[-1][0]:
            tmp += s2.pop()[1]
        s2.append((idx, tmp))

res.append(len(s2))
for i in range(len(s2)):
    ans.append(s2[i][1])
res.extend(ans)
print(cnt)
print(" ".join(map(str, res)))

Submission status: Accepted

Solution code:
import collections


def main():
    s = input()
    downs = collections.deque()
    pools = collections.deque()
    total_area = 0
    for now, char in enumerate(s):
        if char == "\\":
            downs.append(now)
        elif downs and char == "/":
            down = downs.pop()
            sub_area = now - down
            total_area += sub_area
            if not pools:
                pools.append((down, sub_area))
            else:
                while pools and down < pools[-1][0]:
                    sub_area += pools.pop()[1]
                pools.append((down, sub_area))
    print(total_area)
    pools = list(zip(*pools))[1] if pools else []
    print(len(pools), *pools)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
highest, high, h, mh = [0], [0], 0, 0
for i in range(len(s)):
    if s[-i-1] == '\\':     h += 1
    elif s[-i-1] == "/":    h -= 1
    
    if mh < h:  mh = h
    
    high.append(h)
    highest.append(mh)

high, highest = high[::-1], highest[::-1]
puddle, h = [], high[0]
v, vsum = 0, 0
for i in range(1, len(s) + 1):
    h = min(h, highest[i])
    if high[i] < h:
        if s[i-1] != "_":
            v -= 0.5
        v += h - min(high[i-1], high[i])
    elif v != 0:
        v = int(v + 0.5)
        puddle.append(v)
        vsum += v
        v, h = 0, high[i]
    else:
        h = high[i]
        
print(vsum)
if len(puddle) != 0:
    print(len(puddle), ' '.join(list(map(str, puddle))))
else:
    print(0)

Submission status: Accepted

Solution code:
s = 0   # プールの合計面積

# a は a[i] に i 番目の \ の初期位置からの距離を入れるためのスタック
# b は b[i] に i 番目のプールの左岸（\）の初期位置からの距離を入れるためのスタック
# d は d[i] に i 番目のプールの面積を入れるためのスタック
a = []
b = []
d = []

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

Submission status: Accepted

Solution code:
w = input()

h = [0]

for i in range(len(w)):
  if w[i] == "/":
    h.append(h[i]+1)
  elif w[i] == "_":
    h.append(h[i])
  else:
    h.append(h[i]-1)

L = []

lstart = []

for i in range(len(w)):
  if w[i] == "\\":
    lstart.append(i)
    L.append(0)
  elif w[i] == "/" and not lstart == []:
    j = lstart.pop()
    s = i-j
    for k in range(1, i-j):
      if L[i-k]>0:
        s += L.pop(i-k)
        L.append(0)
    L.append(s)
  else:
    L.append(0)

L = [i for i in L if i > 0]

print(sum(L))
L.insert(0,len(L))
print(" ".join(map(str, L)))

Submission status: Accepted

Solution code:
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))