Problem:

勇者ビ太郎(Bitaro the Brave)

  勇者のビ太郎は，魔王と対峙することとなった．

  ビ太郎は，縦$H$ 行，横$W$ 列のマス目上に宝石(Jewel)，オーブ(Orb)，金塊(Ingot) を配置し，魔法を発動することによって魔王に攻撃をしようとしている．以下，マス目のうち上から$i$ 行目($1 \leq i \leq H$)，左から$j$ 列目($1 \leq j \leq W$) のマスを，マス($i, j$) と表す．

  ビ太郎は今，それぞれのマスにこれら3 種類のうち1 個を配置した．今から魔法を発動しようとしているが，この魔法の威力はマス目上の宝石，オーブ，金塊の配置によって決まる．具体的には，次の条件を満たす整数($i, j, k, l$) ($1 \leq i 

入出力例

入力例1

3 4
JOIJ
JIOO
IIII

出力例1

3

  
この入力例では，($i, j, k, l$) = (1, 1, 3, 2), (2, 1, 3, 3), (2, 1, 3, 4) の3 個の組が条件を満たすので，答えは3 である．
  

  

入力例2 

4 4
JJOO
JJOO
IIJO
IIIJ

出力例2

17

  
  

  情報オリンピック日本委員会作 『第18 回日本情報オリンピック(JOI 2018/2019) 本選』



Sample input:
3 4
JOIJ
JIOO
IIII


Sample output:
3

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

ans = 0
ci = [0 for _ in range(w)]
for i in range(h - 1, -1, -1):
    co = 0
    for j in range(w - 1, -1, -1):
        if s[i][j] == 'J':
            ans += co * ci[j]
        elif s[i][j] == 'O':
            co += 1
        elif s[i][j] == 'I':
            ci[j] += 1

print(ans)

Submission status: Accepted

Solution code:
h, w = [int(i) for i in input().split()]
squares = [list(input()) for _ in range(h)]

ans = 0
h_cnts = [0] * w
for i in reversed(range(0, h)):
    o_cnt = 0
    for j in reversed(range(0, w)):
        if squares[i][j] == 'I':
            h_cnts[j] += 1
        elif squares[i][j] == 'O':
            o_cnt += 1
        else:
            ans += h_cnts[j] * o_cnt
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
write = sys.stdout.write
H, W = map(int, readline().split())
S = [readline().strip() for i in range(H)]

ans = 0
D = [0]*W
for i in range(H-1, -1, -1):
    Si = S[i]
    cur = 0
    for j in range(W-1, -1, -1):
        c = Si[j]
        if c == 'J':
            ans += D[j]*cur
        elif c == 'O':
            cur += 1
        else:
            D[j] += 1
write("%d\n" % ans)

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    mp = [input() for _ in range(h)]
    
    i_cnt = [[0] * w for _ in range(h)]
    o_cnt = [[0] * w for _ in range(h)]
    
    for x in range(w):
        if mp[h - 1][x] == "I":
            i_cnt[h - 1][x] = 1
    
    for y in range(h):
        if mp[y][w - 1] == "O":
            o_cnt[y][w - 1] = 1
    
    ans = 0
    for y in range(h - 2, -1, -1):
        for x in range(w - 2, -1, -1):
            if mp[y][x] == "I":
                i_cnt[y][x] = i_cnt[y + 1][x] + 1
                o_cnt[y][x] = o_cnt[y][x + 1]
            elif mp[y][x] == "O":
                i_cnt[y][x] = i_cnt[y + 1][x]
                o_cnt[y][x] = o_cnt[y][x + 1] + 1
            else:
                i_cnt[y][x] = i_cnt[y + 1][x]
                o_cnt[y][x] = o_cnt[y][x + 1]
                ans += i_cnt[y][x] * o_cnt[y][x]
    
    print(ans)

main()

Submission status: Accepted

Solution code:
#!/usr/bin/python3

import array
from fractions import Fraction
import functools
import itertools
import math
import os
import sys


def main():
    H, W = read_ints()
    S = [inp() for _ in range(H)]
    print(solve(H, W, S))


def solve(H, W, S):
    o_table = [[0] * W for _ in range(H)]
    for y in range(H):
        c = 0
        for x in range(W - 1, -1, -1):
            if S[y][x] == 'O':
                c += 1
            o_table[y][x] = c

    i_table = [[0] * W for _ in range(H)]
    for x in range(W):
        c = 0
        for y in range(H - 1, -1, -1):
            if S[y][x] == 'I':
                c += 1
            i_table[y][x] = c

    ans = 0
    for y in range(H):
        for x in range(W):
            if S[y][x] == 'J':
                ans += o_table[y][x] * i_table[y][x]

    return ans


###############################################################################
# AUXILIARY FUNCTIONS

DEBUG = 'DEBUG' in os.environ


def inp():
    return sys.stdin.readline().rstrip()


def read_int():
    return int(inp())


def read_ints():
    return [int(e) for e in inp().split()]


def dprint(*value, sep=' ', end='\n'):
    if DEBUG:
        print(*value, sep=sep, end=end)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    mp = [input() for _ in range(h)]
    
    i_cnt = [[0] * w for _ in range(h)]
    o_cnt = [[0] * w for _ in range(h)]
    
    for x in range(w):
        if mp[h - 1][x] == "I":
            i_cnt[h - 1][x] = 1
    
    for y in range(h):
        if mp[y][w - 1] == "O":
            o_cnt[y][w - 1] = 1
    
    ans = 0
    for y in range(h - 2, -1, -1):
        for x in range(w - 2, -1, -1):
            if mp[y][x] == "I":
                i_cnt[y][x] = i_cnt[y + 1][x] + 1
                o_cnt[y][x] = o_cnt[y][x + 1]
            elif mp[y][x] == "O":
                i_cnt[y][x] = i_cnt[y + 1][x]
                o_cnt[y][x] = o_cnt[y][x + 1] + 1
            else:
                i_cnt[y][x] = i_cnt[y + 1][x]
                o_cnt[y][x] = o_cnt[y][x + 1]
                ans += i_cnt[y][x] * o_cnt[y][x]
    
    print(ans)

main()