[๐Ÿ™ ์•Œ๊ณ ๋ฆฌ์ฆ˜] ๋ฐฑ์ค€ 11659 - ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ4 / Python

dsfasdยท2022๋…„ 10์›” 19์ผ
0

https://www.acmicpc.net/problem/11659

๋ฌธ์ œ

์ˆ˜ N๊ฐœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, i๋ฒˆ์งธ ์ˆ˜๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆ˜๊นŒ์ง€ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ฝ”๋“œ

# 11659 ๊ตฌ๊ฐ„ํ•ฉ ๊ตฌํ•˜๊ธฐ
import sys
input =  sys.stdin.readline

N,M = map(int,input().split())
numbers = list( map(int,input().split()))

#ํ•ฉ๋ฐฐ์—ด์„ ๊ตฌํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ํฌ๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
sumNumbers = [0] #์ด ๋ถ€๋ถ„ ํ™•์ธ
temp = 0
for i in numbers:
    temp = temp + i
    sumNumbers.append(temp)

# ์ „์ฒดํ•ฉ ๋ฐฐ์—ด์—์„œ ๋’ค์˜์ˆ˜ - ์•ž์˜์ˆ˜ +1 ํ•œ ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜
for j in range(0,M):
    p,b = map(int,input().split())
    ans = sumNumbers[b] - sumNumbers[p-1]
    print(ans)


ํ’€์ด

ํ•ฉ๋ฐฐ์—ด์„ ๋ฏธ๋ฆฌ ๊ตฌํ•ด๋‘๊ณ  ํ’€๋ฉด ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ํฌ๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
ํ•ฉ๋ฐฐ์—ด์˜ ๊ณต์‹์€ S[i] = S[i-1]+a[i] ์ด๋‹ค.

ํ•ฉ๋ฐฐ์—ด์€ sumNumbers๋กœ ๊ตฌํ•ด๋‘์—ˆ๊ณ 
๊ตฌ๊ฐ„ ํ•ฉ ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด์šฉํ•œ๋‹ค.
S[4] = A[0]+A[1]+A[2]+A[3]+A[4]
S[4] - S[3] = A[4]


profile
๊ธฐ๋ก์„ ์ •๋ฆฌํ•˜๋Š” ๊ณต๊ฐ„!

0๊ฐœ์˜ ๋Œ“๊ธ€