๐Ÿ”— Problem Link

 

11659๋ฒˆ: ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 4

์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N๊ณผ ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ํšŸ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” N๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ˆ˜๋Š” 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์…‹์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ตฌ๊ฐ„ i์™€ j

www.acmicpc.net


โ”Thinking

  • ์ฃผ์–ด์ง„ N๊ฐœ์˜ ์ˆซ์ž๋“ค ์ค‘, i๋ฒˆ์งธ์—์„œ j๋ฒˆ์งธ ๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ’ปSolution

import sys
N, M = map(int, sys.stdin.readline().split())
numbers = list(map(int, sys.stdin.readline().split()))
numbers_sum = [0]
tmp_sum = 0

for i in numbers:
    tmp_sum += i
    numbers_sum.append(tmp_sum)

for i in range(M):
    start, end = map(int, sys.stdin.readline().split())
    print(numbers_sum[end] - numbers_sum[start-1])

๐Ÿ—๏ธkeypoint

  1. list slice๋ฅผ ํ†ตํ•œ sum()์€ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
  2. ๊ตฌ๊ฐ„ํ•ฉ์€ ์ฒ˜์Œ๋ถ€ํ„ฐ j๋ฒˆ์งธ๊นŒ์ง€์˜ ํ•ฉ์—์„œ ์ฒ˜์Œ๋ถ€ํ„ฐ i-1๋ฒˆ์งธ๊นŒ์ง€์˜ ํ•ฉ์„ ๋บธ ๊ฐ’์ด๋‹ค.

+ Recent posts