Problem:


 惑星探査(Planetary Exploration) 

あなたを乗せた超時空移民船は長旅の末，ついに居住可能と思われる惑星を発見した．JOI 星と名付けられたその惑星は，その名の通り「ジャングル(Jungle)」，「海(Ocean)」，「氷(Ice)」の3 種類の地形が入り組んだ過酷な惑星である．簡単な調査により，居住予定地近辺の地図が作成された．居住予定地は南北M km, 東西N km の長方形の形をしており， 1 km 四方の正方形の区画に分けられている．区画は全部でMN 個あり，北からp 行目，西からq 列目の区画を(p, q) で表す．北西の角の区画が(1, 1) であり，南東の角の区画が(M, N) である．各区画の地形は，「ジャングル」，「海」，「氷」のいずれかであり，「ジャングル」はJ, 「海」はO, 「氷」はI の英字1 文字で表される.

さて，詳細な移住計画を立てるにあたり， K 箇所の長方形領域内に「ジャングル」，「海」，「氷」がそれぞれ何区画含まれるかを調べることにした．

課題

居住予定地の情報と，調査対象となる領域の情報が与えられたとき，それぞれの領域について， 「ジャングル」，「海」，「氷」が何区画含まれているかを求めるプログラムを作成せよ．

制限

1 ≤ M ≤ 1000    居住予定地の南北の長さ(km)

1 ≤ N ≤ 1000    居住予定地の東西の長さ(km)

1 ≤ K ≤ 100000    調査対象となる領域の個数

入力

標準入力から以下の入力を読み込め．

 1 行目には整数M, N が空白を区切りとして書かれており，居住予定地が南北にM km ，東西にN km の広さであることを表す．

 2 行目には整数K が書かれており，調査対象となる領域の個数を表す．

 続くM 行には居住予定地の情報が書かれている．i + 2 行目(1 ≤ i ≤ M) には，居住予定地の北からi行目に位置するN 区画の情報を表すJ，O，I からなるN 文字の文字列が書かれている．

 続くK 行には調査対象となる領域が書かれている．j + M + 2 行目(1 ≤ j ≤ K) には， j 番目の領域を表す正整数aj, bj, cj, dj が空白を区切りとして書かれている．(aj, bj) は調査領域の北西の角の区画を，(cj, dj) は調査領域の南東の角の区画を表す．ただし，aj, bj, cj, dj は，1 ≤ aj ≤ cj ≤ M, 1 ≤ bj ≤ dj ≤ Nを満たす．

出力

標準出力に調査の結果を表すK 行を出力せよ．出力のj 行目には， j 番目の調査領域に含まれる「ジャングル」(J) の区画数，「海」(O) の区画数，「氷」(I) の区画数を表す3 つの整数を，この順に空白を区切りとして書け．

採点基準

採点用データのうち，配点の30%分については，M ≤ 50 かつK ≤ 100 を満たす．配点の50%分については，M ≤ 50 を満たす．

入出力の例

入力例
  

4 7
4
JIOJOIJ
IOJOIJO
JOIJOOI
OOJJIJO
3 5 4 7
2 2 3 6
2 2 2 2
1 1 4 7

出力例
  

1 3 2
3 5 2
0 1 0
10 11 7

この入力例では， 2 番目の領域は上図のように「ジャングル」を3 区画，「海」を5 区画，「氷」を2 区画含む．

問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
4 7
4
JIOJOIJ
IOJOIJO
JOIJOOI
OOJJIJO
3 5 4 7
2 2 3 6
2 2 2 2
1 1 4 7


Sample output:
1 3 2
3 5 2
0 1 0
10 11 7

Submission status: Accepted

Solution code:
from itertools import accumulate
from operator import add
line, row = [int(x) for x in input().split()]
k = int(input())
L=[]
J=[[0]*(row+1) for _ in range(line+1)]
O=[[0]*(row+1) for _ in range(line+1)]
I=[[0]*(row+1) for _ in range(line+1)]

for _ in range(line):
    L.append(input())

for i in range(line):
    for j in range(row):
        if L[i][j] == "J":
            J[i+1][j+1] = 1
        elif L[i][j] == "O":
            O[i+1][j+1] = 1
        else:
            I[i+1][j+1] = 1

for i in range(len(J)):
    J[i] = list(accumulate(J[i]))
    O[i] = list(accumulate(O[i]))
    I[i] = list(accumulate(I[i]))

for i in range(1,len(J)):
    J[i] = list(map(add, J[i], J[i-1]))
    O[i] = list(map(add, O[i], O[i-1]))
    I[i] = list(map(add, I[i], I[i-1]))

def fcheck(M, a, b, c, d):
    return M[c][d] + M[a][b] - M[c][b] - M[a][d]

for _ in range(k):
    a, b, c, d = [int(x)-1 for x in input().split()]
    j = o = i = 0
    c +=1
    d +=1
    print( fcheck(J,a,b,c,d), fcheck(O,a,b,c,d), fcheck(I, a, b, c,d))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
j=[[0 for i in range(m+1)] for j in range(n+1)]
o=[[0 for i in range(m+1)] for j in range(n+1)]
i=[[0 for i in range(m+1)] for j in range(n+1)]
field=[]
roop=int(input())
for k in range(n):
    field.append(input())
for k in range(n):
    j_c,o_c,i_c=0,0,0
    for l in range(m):
        if field[k][l]=="J":
                j_c+=1
        if field[k][l]=="O":
                o_c+=1
        if field[k][l]=="I":
                i_c+=1
        j[k+1][l+1]=j[k][l+1]+j_c
        o[k+1][l+1]=o[k][l+1]+o_c
        i[k+1][l+1]=i[k][l+1]+i_c
for k in range(roop):
    x1,y1,x2,y2=map(int,input().split())
    j_c=j[x2][y2]-j[x2][y1-1]-j[x1-1][y2]+j[x1-1][y1-1]
    o_c=o[x2][y2]-o[x2][y1-1]-o[x1-1][y2]+o[x1-1][y1-1]
    i_c=i[x2][y2]-i[x2][y1-1]-i[x1-1][y2]+i[x1-1][y1-1]
    print(j_c,o_c,i_c)

Submission status: Accepted

Solution code:
import sys

H, W = map(int, input().split())
K = int(input())
cumsum = [[0]*(W+1) for _ in [0]*(H+1)]
d1, d2 = 10**12, 10**6

for y, _up in enumerate(cumsum[:-1], start=1):
    acc = 0
    for x, (up, c) in enumerate(zip(_up[1:], sys.stdin.readline()), start=1):
        if c == "J":
            acc += d1
        elif c == "O":
            acc += d2
        else:
            acc += 1
        cumsum[y][x] = acc + up

ans = []
append = ans.append
for y1, x1, y2, x2 in (map(int, l.split()) for l in sys.stdin):
    x1 -= 1
    y1 -= 1
    v = cumsum[y2][x2] - cumsum[y1][x2] - cumsum[y2][x1] + cumsum[y1][x1]
    append("%d %d %d"%(v//d1, v%d1//d2, v%d2))

print(*ans, sep="\n")

Submission status: Runtime Error

Solution code:
M,N = map(int,raw_input().split())
K = int(raw_input())
A = [raw_input() for i in range(M)]
for roop in range(K):
    a,b,c,d = map(int,raw_input().split())
    j = o = i = 0
    for m in range(a-1,c):
        j += A[m][b-1:d].count("J")
        o += A[m][b-1:d].count("O")
        i += A[m][b-1:d].count("I")
    print j,o,i

Submission status: Runtime Error

Solution code:
M,N = map(int,raw_input().split())
K = int(raw_input())
A = [raw_input() for i in range(M)]
J,O,I = [[0]*(N+1) for i in range(M)],[[0]*(N+1) for i in range(M)],[[0]*(N+1) for i in range(M)]
for m in range(M):
    j = o = i = 0
    for n in range(N):
        if   A[m][n] == "J": j += 1
        elif A[m][n] == "O": o += 1
        elif A[m][n] == "I": i += 1
        J[m][n+1],O[m][n+1],I[m][n+1] = j,o,i

for roop in range(K):
    a,b,c,d = map(int,raw_input().split())
    j = sum(J[m][d] - J[m][b-1] for m in range(a-1,c))
    o = sum(O[m][d] - O[m][b-1] for m in range(a-1,c))
    i = sum(I[m][d] - I[m][b-1] for m in range(a-1,c))
    print j,o,i

Submission status: Runtime Error

Solution code:
M,N = map(int,raw_input().split())
K = int(raw_input())
A = [raw_input() for i in range(M)]
J,O,I = [[0]*(N+1) for i in range(M)],[[0]*(N+1) for i in range(M)],[[0]*(N+1) for i in range(M)]
for m in range(M):
    j = o = i = 0
    for n in range(N):
        if   A[m][n] == "J": j += 1
        elif A[m][n] == "O": o += 1
        elif A[m][n] == "I": i += 1
        J[m][n+1],O[m][n+1],I[m][n+1] = j,o,i

for roop in range(K):
    a,b,c,d = map(int,raw_input().split())
    try:
        j = sum(J[m][d] - J[m][b-1] for m in range(a-1,c))
        o = sum(O[m][d] - O[m][b-1] for m in range(a-1,c))
        i = sum(I[m][d] - I[m][b-1] for m in range(a-1,c))
    except:
        pass
    print j,o,i

Submission status: Runtime Error

Solution code:
w=input()
height=int(w[0])
wide=int(w[2])

pattern_num=int(input())
a=[]
for i in range(pattern_num):
    a.append(input())

for i in range(pattern_num):
    y=input()
    north=int(y[0])-1; west=int(y[2])-1
    south=int(y[4])-1; east=int(y[6])-1
    jungle=0;ocean=0;ice=0;

    for j in range(north,south+1):
        for k in range(west,east+1):
            if a[j][k]=="J":
                jungle+=1
            elif a[j][k]=="O":
                ocean+=1
            elif a[j][k]=="I":
                ice+=1
    print(jungle,ocean,ice)

Submission status: Runtime Error

Solution code:
w=input()
height=int(w[0])
wide=int(w[2])

pattern_num=int(input())
a=[]
for i in range(pattern_num):
    a.append(input())

for i in range(pattern_num):
    y=input()
    north=int(y[0])-1
    west=int(y[2])-1
    south=int(y[4])-1
    east=int(y[6])-1
    jungle=0
    ocean=0
    ice=0

    for j in range(north,south+1):
        for k in range(west,east+1):
            if a[j][k]=="J":
                jungle+=1
            elif a[j][k]=="O":
                ocean+=1
            elif a[j][k]=="I":
                ice+=1
    print(jungle,ocean,ice)

Submission status: Runtime Error

Solution code:
w=input()
height=int(w[0])
wide=int(w[2])

pattern_num=int(input())
a=[]
for i in range(pattern_num):
    a.append(input())

for i in range(pattern_num):
    y=input()
    north=int(y[0])-1
    west=int(y[2])-1
    south=int(y[4])-1
    east=int(y[6])-1
    jungle=0
    ocean=0
    ice=0

    for j in range(north,south+1):
        for k in range(west,east+1):
            if a[j][k]=="J":
                jungle+=1
            elif a[j][k]=="O":
                ocean+=1
            elif a[j][k]=="I":
                ice+=1
    #print(jungle,ocean,ice)
    #print(jungle,ocean,ice)
    #print(jungle,ocean,ice)
    #print(jungle,ocean,ice)

Submission status: Runtime Error

Solution code:
w=input()
height=int(w[0])
wide=int(w[2])

pattern_num=int(input())
a=[]
for i in range(pattern_num):
    a.append(input())

for i in range(pattern_num):
    y=input()
    north=int(y[0])-1
    west=int(y[2])-1
    south=int(y[4])-1
    east=int(y[6])-1
    jungle=0
    ocean=0
    ice=0

    for j in range(north,south+1):
        for k in range(west,east+1):
            if a[j][k] == "J":
                #jungle+=1
                jungle =jungle+1
            elif a[j][k] == "O":
                #ocean+=1
                ocean=ocean+1
            elif a[j][k] == "I":
                #ice+=1
                ice=ice+1

Submission status: Runtime Error

Solution code:
line =input()
w=line.split(" ")
height=int(w[0])
wide=int(w[1])

pattern_num=int(input())
a=[]
for i in range(pattern_num):
    a.append(input())

for i in range(pattern_num):
    y=input()
    north=int(y[0])-1
    west=int(y[2])-1
    south=int(y[4])-1
    east=int(y[6])-1
    jungle=0
    ocean=0
    ice=0

    for j in range(north,south+1):
        for k in range(west,east+1):
            if a[j][k] == "J":
                jungle+=1
            elif a[j][k] == "O":
                ocean+=1
            elif a[j][k] == "I":
                ice+=1

Submission status: Runtime Error

Solution code:
line =input()
w=line.split(" ")
height=int(w[0])
wide=int(w[1])

pattern_num=int(input())
a=[]
for i in range(pattern_num):
    a.append(input())

for i in range(pattern_num):
    aline =input()
    aw=aline.split(" ")
    north=int(aw[0])-1
    west=int(aw[1])-1
    south=int(aw[2])-1
    east=int(aw[3])-1
    jungle=0
    ocean=0
    ice=0

    for j in range(north,south+1):
        for k in range(west,east+1):
            if a[j][k] == "J":
                jungle+=1
            elif a[j][k] == "O":
                ocean+=1
            elif a[j][k] == "I":
                ice+=1

Submission status: Runtime Error

Solution code:
line =input()
w=line.split(" ")
height=int(w[0])
wide=int(w[1])

pattern_num=int(input())
a=[]
for i in range(height):
    a.append(input())

for i in range(pattern_num):
    aline =input()
    aw=aline.split(" ")
    north=int(aw[0])-1
    west=int(aw[1])-1
    south=int(aw[2])-1
    east=int(aw[3])-1
    jungle=0
    ocean=0
    ice=0

    for j in range(north,south+1):
        for k in range(west,east+1):
            if a[j][k] == "J":
                jungle+=1
            elif a[j][k] == "O":
                ocean+=1
            elif a[j][k] == "I":
                ice+=1
    print(jungle,ocean,ice)

Submission status: Runtime Error

Solution code:
# coding: utf-8
M, N = list(map(int, input().split()))
q = int(input())

Map = []
for i in range(M):
    Map.append(input)

def get_slide_sum(Map, kw):
    l = []
    for r in range(M):
        tmp = []
        for c in range(N):
            ll = 0 if c == 0 else tmp[c-1]
            ul = 0 if r == 0 or c == 0 else l[r-1][c-1]
            ur = 0 if r == 0 else l[r-1][c]
            count = ll + ur - ul
            if Map[r][c] == kw:
                count+=1
            tmp.append(count)
        l.append(tmp)
    return l

J = get_slide_sum(Map, 'J')
O = get_slide_sum(Map, 'O')
I = get_slide_sum(Map, 'I')

def get_sum(inp,L):
    a, b, c, d = list(map(int, inp.split()))
    ll = 0 if a == 1 else L[a-2][d-1]
    ul = 0 if a == 1 or b == 1 else L[a-2][b-2]
    ur = 0 if b == 1 else L[c-1][b-2]
    return L[c-1][d-1] - ll - ur + ul

for i in range(q):
    inp = input()
    print(get_sum(inp, J), get_sum(inp, O), get_sum(inp, I))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Here your code !
M, N = list(map(int, input().split()))
q = int(input())

Map = []
for i in range(M):
    Map.append(input)

def get_slide_sum(Map, kw):
    l = []
    for r in range(M):
        tmp = []
        for c in range(N):
            ll = 0 if c == 0 else tmp[c-1]
            ul = 0 if r == 0 or c == 0 else l[r-1][c-1]
            ur = 0 if r == 0 else l[r-1][c]
            count = ll + ur - ul
            if Map[r][c] == kw:
                count+=1
            tmp.append(count)
        l.append(tmp)
    return l

J = get_slide_sum(Map, 'J')
O = get_slide_sum(Map, 'O')
I = get_slide_sum(Map, 'I')

def get_sum(inp,L):
    a, b, c, d = list(map(int, inp.split()))
    ll = 0 if a == 1 else L[a-2][d-1]
    ul = 0 if a == 1 or b == 1 else L[a-2][b-2]
    ur = 0 if b == 1 else L[c-1][b-2]
    return L[c-1][d-1] - ll - ur + ul

for i in range(q):
    inp = input()
    print(get_sum(inp, J), get_sum(inp, O), get_sum(inp, I))

Submission status: Runtime Error

Solution code:
M, N = map(int, input().split())
K = int(input())
P = ['_' * (N + 1) for _ in range(M + 1)]
for mi in range(1, M + 1):
    P[mi] = '_' + input()  # for p in P:
    # print('p', p)
    J = [[0 for _ in range(N + 1)] for _ in range(M + 1)]
    O = [[0 for _ in range(N + 1)] for _ in range(M + 1)]
    I = [[0 for _ in range(N + 1)] for _ in range(M + 1)]
    for i in range(1, M + 1):
        ji, oi, ii = 0, 0, 0
        for j in range(1, N + 1):
            ji += P[i][j] == 'J'
            oi += P[i][j] == 'O'
            ii += P[i][j] == 'I'
            J[i][j] = J[i - 1][j] + ji
            O[i][j] = O[i - 1][j] + oi
            I[i][j] = I[i - 1][j] + ii
            # for j in J:
            # print('j', j)
            # for o in O:
            # print('o', o)
            # for i in I:
            # print('i', i)


def cum(mat, a, b, c, d):
    a -= 1
    b -= 1
    return mat[a][b] + mat[c][d] - mat[a][d] - mat[c][b]


for k in range(K):
    a, b, c, d = map(int, input().split())
    j = cum(J, a, b, c, d)
    o = cum(O, a, b, c, d)
    i = cum(I, a, b, c, d)
    print(j, o, i)

Submission status: Runtime Error

Solution code:
# AOJ 0560: Planetary Exploration
# Python3 2018.6.30 bal4u

tr = { 'J':0, 'O':1, 'I':2 }

s = [[[0 for c in range(1002)] for r in range(1002)] for k in range(3)]
m, n = map(int, input().split())
k = int(input())
for r in range(1, m+1):
	buf = input().strip()
	for c in range(1, n+1):
		for i in range(3): s[i][r][c] = s[i][r-1][c] + (i == tr[buf[c-1]])
for i in range(k):
	r1, c1, r2, c2 = map(int, input().split())
	ans = [0]*3
	for j in range(3):
		for c in range(c1, c2+1): ans[j] += s[j][r2][c] - s[j][r1-1][c];
	print(*ans)

Submission status: Runtime Error

Solution code:
M, N = map(int, raw_input().split())
K = input()

d = {}

for i in range(M):
    a = raw_input()
    for j in range(N):
        d[i+1, j+1] = a[j]

for _ in range(K):
    b = map(int, raw_input().split())
    D = {}
    D['J'] = D['O'] = D['I'] = 0
    for i in range(b[0], b[2]+1):
        for j in range(b[1], b[3]+1):
            D[d[i, j]] += 1
    print D['J'], D['O'], D['I']

Submission status: Runtime Error

Solution code:
sn_km, ew_km = map(int, raw_input().split())
seek_area = int(raw_input())
zone = sn_km*[ew_km*[0]]

def cnt_zone(idx, first, last, str_for_count):
    return int(str(zone[idx])[first:last].count(str_for_count))

for i in range(sn_km):
    zone[i] = raw_input()
    
for i in range(seek_area):
    J = O = I = 0
    a, b, c, d = map((lambda x, y: x+y), map(int, raw_input().split()), 4*[-1])
    for y in range(c - a + 1):
        J += cnt_zone(a+y, b, d+1, 'J')
        O += cnt_zone(a+y, b, d+1, 'O')
        I += cnt_zone(a+y, b, d+1, 'I')
    print "%d %d %d" % (J, O, I)