๐ฐ ๋์ ํฉ ์ ํ์ ์ฃผ๋ก ์ด๋ ค์ด ๋์ด๋ ๋ค์ํ ์๊ณ ๋ฆฌ์ฆ ์ ํ์ ๋ต์ ๊ตฌํ๋ ๊ณผ์ ์์ ๋ง์ด ์ฌ์ฉ๋๋ค. ๋จ๋ ์ผ๋ก ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ๋ง ์ผ๋ถ ๋ฌธ์ ๋ฅผ ๊ณจ๋ผ ์ ๋ฆฌํด๋ด
โ 11441 ํฉ ๊ตฌํ๊ธฐ โ
import sys
input=sys.stdin.readline
N=int(input())
B=[]
a=0
for i in map(int,input().split()):
a+=i
B.append(a)
for _ in range(int(input())):
a,b=map(int,input().split())
if a==1:print(B[b-1])
else:print(B[b-1]-B[a-2])
๐ฐ ๋์ ํฉ์ ๊ตฌํ๊ธฐ ์ํ ๊ตฌ๊ฐ์ ๋ง๋ ๋ค, ๊ตฌ๊ฐ B์์ ์ํ๋ ๊ตฌ๊ฐ๋งํผ ๋ฐ๋ก indexing์ผ๋ก B[b-1] - B[a-2] ์ฐ์ฐํด์ฃผ๋ฉด ์๋ฃ!
โ 11659 ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ 4 โ
#indexing over slicing
N, M = map(int, input().split())
numbers = list(map(int, input().split()))
totals = []
answers = []
total = 0
for i in range(N):
total += numbers[i]
totals.append(total)
for _ in range(M):
i, j = map(int,input().split())
if i == 1:
answers.append(totals[j-1])
else:
answers.append(totals[j-1] - totals[i-2])
for answer in answers:
print(answer)
๐ฐ ๋์ ํฉ list๋ฅผ ๋ง๋ค ๋ for๋ฌธ ํ์์์ด itertools์ accumulate ํจ์๋ฅผ ์ฌ์ฉํด์๋ ๋ง๋ค ์ ์๋ค.
import sys
input = sys.stdin.readline
from itertools import accumulate
#indexing over slicing
N, M = map(int, input().split())
numbers = list(map(int, input().split()))
totals = list(accumulate(numbers))
answers = []
for _ in range(M):
i, j = map(int,input().split())
if i == 1:
answers.append(totals[j-1])
else:
answers.append(totals[j-1] - totals[i-2])
for answer in answers:
print(answer)
โ 17390 ์ด๊ฑด ๊ผญ ํ์ด์ผ ํด! โ
import sys
input=sys.stdin.readline
N,Q=map(int,input().split())
l=sorted(list(map(int,input().split())))
t=0
culsum=[0]
for i in l:
t+=i
culsum.append(t)
for k in range(Q):
L,R=map(int,input().split())
print(culsum[R]-culsum[L-1])
๐ฐ ์ ๋ ฌํ๊ณ ๊ตฌ๊ฐ ํฉ ํ๋ฉด ๋! itertools์ accumulate ์ฌ์ฉํ๋ฉด ์ข ๋ ์๊ฐ ๋จ์ถ๋จ
โ 11969 Breed Counting โ
import sys
input=sys.stdin.readline
N,Q=map(int,input().split())
cows=[int(input()) for _ in range(N)]
prefix_sum=[[0,0,0] for _ in range(N)]
for i in range(0,N):
if i ==0:prefix_sum[i][cows[i]-1] = 1
if cows[i]==1:
prefix_sum[i][0] = (prefix_sum[i-1][0]) + 1
prefix_sum[i][1] = prefix_sum[i-1][1]
prefix_sum[i][2] = prefix_sum[i-1][2]
elif cows[i]==2:
prefix_sum[i][0] = prefix_sum[i-1][0]
prefix_sum[i][1] = (prefix_sum[i-1][1]) + 1
prefix_sum[i][2] = prefix_sum[i-1][2]
else:
prefix_sum[i][0] = prefix_sum[i-1][0]
prefix_sum[i][1] = prefix_sum[i-1][1]
prefix_sum[i][2] = (prefix_sum[i-1][2]) + 1
for _ in range(Q):
s,e=map(int,input().split())
if s==1: print(*prefix_sum[e-1])
else:
l=[]
x=prefix_sum[e-1]
y=prefix_sum[s-2]
for a,b in zip(x,y):
l.append(a-b)
print(*l)
๐ฐ ์ฃผ์ด์ง cow ๊ฐ์ฒด ์์ index ์์น ๊ตฌ๊ฐ์ ์๊ณ ๋๋ฉด, ์ฃผ์ด์ง ๊ตฌ๊ฐ ๋ด์ cow 1, 2, 3 ๊ฐ์ฒด ์๋ฅผ ๊ฐ๊ฐ ๊ตฌํ๋ ๋ฌธ์
๐ฐ ๊ตฌ๊ฐ๋ณ ํฉ์ ๊ฐ์ฒด์ ๋ณ๋ก ๊ฐ๊ฐ ๊ตฌํ ๋ค, ๋์ ํฉ ๊ฐ๋ ์ ๋ง๊ฒ ํฐ ๋์ ํฉ - ์์ ๋์ ํฉ์ ๊ตฌํจ
โ 13900 ์์์์ ๊ณฑ์ ํฉ โ
import sys
input=sys.stdin.readline
N=int(input())
l=list(map(int,input().split()))
total=sum(l)
ans=0
for i in range(N-1):
total-=l[i]
ans+=l[i]*total
print(ans)
๐ฐ ์ ์ฒด ์์์์ mulsum์ ์์ ๊ฐ์ด ์ ์ฒด total์์ ์์ ์์๋ฅผ for๋ฌธ์ผ๋ก ๋๋ฆฌ๋ฉฐ ํ๊ฐ์ฉ ๋นผ๋๊ฐ๋ฉด์ ๊ณ์ ๋ํด๋๊ฐ๋ฉด ๋๋ค. ์ ์ฒด ๋์ ํฉ์ ๊ณ์ ๋นผ๋๊ฐ๋ฉฐ for๋ฌธ ํ ๋ฒ์ผ๋ก ๋ชจ๋ ๊ฒฝ์ฐ์ mulsum์ ๊ตฌํ ์ ์๋ ์ ์ฒด ์์์(๋ ๊ฐ ์์)์ ํฉ ๊ตฌํ๊ธฐ ๋ฌธ์ ์ ํ
'BOJ > ๐ฅ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ Math & Geometry Upper-Intermediate I - 5 Solvedโ (0) | 2023.01.26 |
---|---|
โ Coordinate Compression Upper-Intermediate - 3 Solvedโ (0) | 2023.01.24 |
โ PQ ์ค์๊ธ - 6๋ฌธ์ ()โ (0) | 2023.01.11 |
โ Stack & Queue & Deque Upper-Intermediate I - 11 Solvedโ (0) | 2023.01.08 |
โ Bitmasking Intermediate I - 3 Solvedโ (0) | 2023.01.05 |
๋๊ธ