제출 #719812

#제출 시각아이디문제언어결과실행 시간메모리
719812BoomydayFancy Fence (CEOI20_fancyfence)Pypy 3
100 / 100
272 ms42776 KiB

from collections import deque

MOD = 1000000007
inv2 = pow(2, MOD-2, MOD)

def calc_num(w, h):
    return w*(w+1)*h*(h+1)*inv2*inv2 % MOD


sc = 0
N = int(input())

h, w = [],[]

h += list(map(int, input().split())) +[0]
w += list(map(int, input().split())) +[0]

st = deque([[0,0]])
# H, W
for i in range(N+1):
    st.append([h[i], w[i]])
    while True:
        #print(st, sc)

        try:
            if st[-1][0] > st[-2][0]:
                break
            elif st[-1][0] == st[-2][0]:
                st[-2][1] += st[-1][1]
                st.pop()
            else:
                if min(st[-1][0], st[-3][0])== st[-3][0]:
                    sc += calc_num(st[-2][1], st[-2][0]) - calc_num(st[-2][1], st[-1][0])
                    sc %= MOD
                    st[-2][0] = st[-1][0]
                else:
                    sc += calc_num(st[-2][1], st[-2][0]) - calc_num(st[-2][1], st[-3][0])
                    sc %= MOD
                    strd = st.pop()
                    st[-2][1] += st[-1][1]
                    st.pop()
                    st.append(strd)

        except:
            break

print(sc)






#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...