#!/usr/bin/env python
#coding: utf-8
#
# sm2 サムアップツール（Open usp Tukubai版）
# 
# designed by Nobuaki Tounaka
# written by Yoshio Katayama
#
# The MIT License
#
# Copyright (C) 2011 Universal Shell Programming Laboratory
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

_usage = "sm2 [+count] <k1> <k2> <s1> <s2> <file>"
_version = "Wed Apr  2 19:22:38 JST 2014"
_code = "Open usp Tukubai (LINUX+FREEBSD/PYTHON2.4/UTF-8)"
_keypat = r'\d+|NF(-\d+)?$'

import re
import os
import sys
from decimal import Decimal

def error(msg, *arg):
	print >> sys.stderr, 'Error[sm2] :', msg % arg
	sys.exit(1)

def usage():
	print >> sys.stderr, "Usage   :", _usage
	print >> sys.stderr, "Version :", _version
	print >> sys.stderr, "         ", _code
	sys.exit(1)

class FieldLine:
	def __init__(self, line, allow_z = False):
		self.__allow_zero = allow_z
		line = line.rstrip('\n')
		self.__fields = [ line ]
		self.__fields += [ x for x in line.split(' ') if x ]

	def size(self):
		return len(self.__fields) - 1

	def getFieldNum(self, key):
		if type(key) == type(0):
			return key
		if re.match(r'\d+$', key):
			key = int(key)
		elif key == 'NF':
			key = self.size()
		else:
			key = self.size() - int(key[3:])
			if key <= 0:
				error("NF-x の x が大きすぎます。")
		if key < 0:
			error("フィールド番号が負です。")
		if key == 0 and not self.__allow_zero:
			error("フィールド番号が０です。")
		if key > self.size():
			error("フィールド番号が大きすぎます。")
		return key

	def getField(self, s, e = None):
		s = self.getFieldNum(s)
		if e == None:
			e = s
		else:
			e = self.getFieldNum(e)
		if s <= e:
			return ' '.join(self.__fields[s : e + 1])
		else:
			t = self.__fields[e : s + 1]
			t.reverse()
			return ' '.join(t)

#
# 入力ファイルオープン
#
def open_file(n, mode = 'r'):
	if n >= len(sys.argv) or sys.argv[n] == '-':
		file = sys.stdin
	else:
		try:
			file = open(sys.argv[n], mode)
		except:
			error("ファイル %s をオープンできません。", sys.argv[n])
	return file

#
# フィールド値の取得
#
def getval(line, n):
	try:
		return Decimal(line.getField(n))
	except:
		error("数値変換できません。")

#
#メイン関数
#
if __name__ == '__main__':

	if len(sys.argv) < 5:
		usage()

	#
	# +count
	#
	if sys.argv[1] == '+count':
		count = True
		del sys.argv[1]
		if len(sys.argv) < 5:
			usage()
	else:
		count = False

	#
	# key=<key>
	#
	k1 = sys.argv[1]
	k2 = sys.argv[2]
	s1 = sys.argv[3]
	s2 = sys.argv[4]
	p = re.compile(_keypat)
	if not (p.match(k1) and p.match(k2) and p.match(s1) and p.match(s2)):
		usage()

	file = open_file(5)

	#
	# １行入力
	#
	line = file.readline()
	if not line:
		sys.exit(0)
	line = FieldLine(line, True)
	k1 = line.getFieldNum(k1)
	k2 = line.getFieldNum(k2)
	s1 = line.getFieldNum(s1)
	s2 = line.getFieldNum(s2)
	if k1 > k2 or s1 > s2 or k2 > s1 \
	  or s1 * s2 == 0 or k1 * k2 == 0 and k1 + k2 != 0:
		usage()

	#
	# 合計の初期化
	#
	sum = range(s2 + 1)
	for i in range(s1, s2 + 1):
		sum[i] = getval(line, i)
	cnt = 1
	if (k1):
		key = line.getField(k1, k2)

	#
	# メインループ
	#
	for line in file:
		line = FieldLine(line)
		if k1 == 0 or line.getField(k1, k2) == key:
			for i in range(s1, s2 + 1):
				sum[i] += getval(line, i)
			cnt += 1
			continue
		print key,
		if count: print cnt,
		for i in range(s1, s2):
			print sum[i],
		print sum[s2]
		for i in range(s1, s2 + 1):
			sum[i] = getval(line, i)
		cnt = 1
		key = line.getField(k1, k2)

	#
	# 残り合計の出力
	#
	if k1 != 0: print key,
	if count: print cnt,
	for i in range(s1, s2):
		print sum[i],
	print sum[s2]

	sys.exit(0)
