Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3-Me-in-U #13

Merged
merged 1 commit into from
May 19, 2024
Merged

3-Me-in-U #13

merged 1 commit into from
May 19, 2024

Conversation

Me-in-U
Copy link
Collaborator

@Me-in-U Me-in-U commented Mar 27, 2024

πŸ”— 문제 링크

2042번 ꡬ간 ν•© κ΅¬ν•˜κΈ°

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

?

✨ μˆ˜λ„ μ½”λ“œ

μ—¬κΈ°μ„œ μ‚¬μš©ν•œ μ•Œκ³ λ¦¬μ¦˜(트리) Fenwick Tree, Binary Indexed Tree(BIT)

μ‹œκ°„ λ³΅μž‘λ„ O(logN)
κ³΅κ°„λ³΅μž‘λ„ O(N)

1차원, 2차원 ν˜Ήμ€ λ‹€μ°¨μ›μ˜ Query λ₯Ό μ²˜λ¦¬ν•˜λŠ”λ° μ‚¬μš© 될 수 μžˆλŠ” 맀우 효율적인 μžλ£Œκ΅¬μ‘°μž…λ‹ˆλ‹€.

Aλ°°μ—΄μ—λŠ” μˆ«μžκ°€ λ“€μ–΄κ°’λ‹ˆλ‹€.
treeλ°°μ—΄μ—λŠ” ꡬ간합 정보가 λ“€μ–΄κ°‘λ‹ˆλ‹€.
νŽœμœ…νŠΈλ¦¬μ—μ„œ ν™€μˆ˜λ²ˆμ§Έ 값은 μžκΈ°μžμ‹ μ΄κ³  μ΄μ™Έμ—λŠ” μžκΈ°μžμ‹ κΉŒμ§€μ˜ λ²”μœ„ 값이닀
image

νŽœμœ… 트리λ₯Ό κ΅¬ν˜„ν•˜κΈ° μœ„ν•΄μ„œ κ°’μ—μ„œ 0이 μ•„λ‹Œ μ΅œν•˜μœ„ λΉ„νŠΈλ₯Ό κ΅¬ν•΄μ•Όν•œλ‹€.
3 = (11)β‚‚ => (01)β‚‚
5 = (101)β‚‚ =>(001)β‚‚
6 = (110)β‚‚ => (010)β‚‚
8 = (1000)β‚‚ => (1000)β‚‚

이걸 κ΅¬ν•˜κΈ° μœ„ν•΄μ„œ 값을 i라고 ν•˜λ©΄
iβ‚‚ & -iβ‚‚ λΉ„νŠΈ and 연산을 ν•˜λ©΄ λœλ‹€.

예λ₯Ό λ“€μ–΄μ„œ i = 13(1101)β‚‚
(1101)β‚‚ & (0011)β‚‚ == (0001)β‚‚
μ΄λ ‡κ²Œ 13μ—μ„œ 첫번째둜 1인 λΉ„νŠΈλŠ” μ²«λ²ˆμ§Έμ— μžˆλŠ”κ±Έ μ•Œ 수 μžˆλ‹€.

Update()

값을 λ„£κ±°λ‚˜ μˆ˜μ •ν• λ•Œ μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ³΄κ² μŠ΅λ‹ˆλ‹€.
μΈλ±μŠ€κ°€ N보닀 μž‘κ±°λ‚˜ κ°™μ„λ•ŒκΉŒμ§€ μ΅œν•˜μœ„λΉ„νŠΈκ°€ 1인곳을 μ°Ύμ•„μ„œ λ”ν•΄μ€λ‹ˆλ‹€.
κ·Έλ ‡κ²Œ ν•˜λ©΄ 이 μˆ«μžκ°€ ν¬ν•¨λœ λΆ€λΆ„ν•© λΆ€λΆ„μ˜ 인덱슀λ₯Ό λ‹€ 찾을 수 있고 그곳의 값을 μˆ˜μ •ν•΄μ€λ‹ˆλ‹€.(였λ₯Έμͺ½μœΌλ‘œ κ°€λŠ” λŠλ‚Œ)

public static void update(int i, long diff) {
    while (i <= N) {
        BIT[i] += diff;
        i += (i & -i);
    }
}
// b == 3(인덱슀), c == 4(λ°”κΎΈλ €λŠ” 숫자)
// 3번 인데슀의 값을 4둜 λ°”κΎΈλŠ” μ½”λ“œ
long diff = c - A[b];
A[b] = c;
update(b, diff);

image

Iteration index index & -index
1 3(011)β‚‚ 001β‚‚
2 6(100)β‚‚ 100β‚‚
3 8(1000)β‚‚ 1000β‚‚
4 16(10000)β‚‚ 10000β‚‚
5 32(100000)β‚‚ -

λ‹€μŒκ³Ό 같은 index μˆœμ„œλŒ€λ‘œ μ°¨μ΄κ°’λ§ŒνΌ 값을 더해주면 λ©λ‹ˆλ‹€.

sum()

ꡬ간 합을 κ΅¬ν•˜λŠ” ν•¨μˆ˜
ꡬ간합은 μ²˜μŒλΆ€ν„° i번째 μΈλ±μŠ€κΉŒμ§€μ˜ 합을 ꡬ할 수 μžˆμŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œ νŠΉμ • ꡬ간을 κ΅¬ν•˜κΈ° μœ„ν•΄ μ•„λž˜μ™€ 같이 ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

// b~cκΉŒμ§€μ˜ λΆ€λΆ„ν•©
sum(c) - sum(b - 1)

ꡬ간합은 μ—…λ°μ΄νŠΈμ™€ λ°˜λŒ€λ‘œ
μΈλ±μŠ€κ°€ 0보닀 크면 μ΅œν•˜μœ„λΉ„νŠΈκ°€ 1인곳을 μ°Ύμ•„μ„œ λΉΌμ€λ‹ˆλ‹€(μ™Όμͺ½μœΌλ‘œ κ°€λŠ” λŠλ‚Œ)

public static long sum(int i) {
    long ans = 0;
    while (i > 0) {
        ans += BIT[i];
        i -= (i & -i);
    }
    return ans;
}

image

Iteration index index & -index sum
1 13(1101)β‚‚ 0001β‚‚ 2
2 12(1100)β‚‚ 0100β‚‚ 4
3 8(1000)β‚‚ 1000β‚‚ 17
4 0 - -

13번째 κΉŒμ§€μ˜ 합을 κ΅¬ν•˜λ©΄ μœ„μ™€κ°™μ΄ 17이 λ‚˜μ˜΅λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

2차원 뢀뢄합도 μ•Œ 수 μžˆλ‹€κ³  ν•©λ‹ˆλ‹€
image

Copy link
Collaborator

@erase-jeong erase-jeong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κ·Έλƒ₯ ν’€μ—ˆλ‹€κ°€ μ‹œκ°„μ΄ˆκ³Όλ– μ„œ μ™œμ§€? ν–ˆλŠ”λ° μ„€λͺ…보고 μ΄ν•΄ν–ˆμŠ΅λ‹ˆλ‹€. μˆ˜κ³ ν•˜μ…¨μ–΄μš”!

@9kyo-hwang 9kyo-hwang removed the request for review from SunYerim May 19, 2024 12:43
Copy link
Collaborator

@wjdheesp44 wjdheesp44 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

νŽœμœ… 트리 μ•Œκ³ λ¦¬μ¦˜μ„ μ΄ν•΄ν•˜λ©΄ μ½”λ“œ μžμ²΄λŠ” κ°„κ²°ν•˜κ²Œ κ΅¬μ„±λ˜λ„€μš”! μ•Œκ³ λ¦¬μ¦˜ 이해에 도움이 λ˜μ—ˆμŠ΅λ‹ˆλ‹€!

@Me-in-U Me-in-U merged commit 198dbc9 into main May 19, 2024
1 check passed
@Me-in-U Me-in-U deleted the 3-BOJ-ios branch May 19, 2024 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants