-
Notifications
You must be signed in to change notification settings - Fork 1
30-g0rnn #121
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
Conversation
kangrae-jo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ์ฒ์์ ๊ท ํธ๋ ๊ฐ์ ์๊ฐ์ ํ๋๋ฐ, 3์ด๋์ ์ ๋ ์ ๊ฐ ์ฒ์ฌ์ธ์ค ์์์ต๋๋ค.
1 ~ i ์ ๊ตฌ๊ฐํฉ์ ๋ฏธ๋ฆฌ ๊ตฌํด๋๋ ๋ฐฉ์๊ณผ,
๊ฐ ๋ณ๊ฒฝ์ ๊ฐ ์ธ๋ฑ์ค์ ์ ์ฉํ๋ ๊ฒ์ ์๊ฐ์ด๊ณผ๊ฐ ์๋ ์๊ฐ ์๋ ๊ฒ ๊ฐ๋ค์.
๊ทธ๋์ 20๋ถ ๊ณ ๋ฏผํ๋ค๊ฐ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ๋ฅผ ๊ณต๋ถํ๊ณ ์ค๊ธฐ๋ก ๋ง์๋จน์์ต๋๋ค.
๋ฐฐ์ด์์ ํธ๋ฆฌ๋ฅผ ๊ตฌํํ๋ ๋ฐฉ์์ ๋๋ฌด ์ค๋๋ง์ด๋ผ ์ด์ง ํ๋ค์๋ค์.
์ดํด๋ ๋๋๋ฐ,, ์ฝ๋ ์ง๊ธฐ๊ฐ ์ข ํ๋ ,,
๊ทธ์น๋ง ๋ง์ง๋ง์ ๋จ๊ฒจ์ฃผ์ BOJ ์์ ์ ๊ณตํ๋ ์๋ฃ๊ฐ ๋งค์ฐ ๋์์ด ๋์์ต๋๋ค.
๋์์ ๊ทธ๋ฆผ์ผ๋ก ํํํด์ค์ ๊ฐ์ ์ ์ก์ ์ ์์์ต๋๋ค.
์์ ํ์ด๋ฒ์ ๋ชฐ๋์ด์ ๊ทธ๋ฐ๊ฐ ๊ฐ์ด๋ ค์ ๋ค์.
๊ฐ์ฌํฉ๋๋ค.
๋ค์์ ๋์ค๋ฉด ๋ง์ถฐ๋ณด๋๋กํ๊ฒ ์ต๋๋ค.
ํ์ด๋ ๋์ผํฉ๋๋ค.
#include <iostream>
#include <vector>
using namespace std;
vector<long long> nums;
vector<long long> tree;
long long makeTree(int start, int end, int node) {
if (start == end) return tree[node] = nums[start];
int mid = (start + end) / 2;
return tree[node] = makeTree(start, mid, node * 2) +
makeTree(mid + 1, end, node * 2 + 1);
}
long long solution(int start, int end, int left, int right, int node) {
if (end < left || right < start) return 0;
if (left <= start && end <= right) return tree[node];
int mid = (start + end) / 2;
return solution(start, mid, left, right, node * 2) +
solution(mid + 1, end, left, right, node * 2 + 1);
}
void updateTree(int start, int end, int node, int index, long long diff) {
if (index < start || index > end) return;
tree[node] += diff;
if (start == end) return;
int mid = (start + end) / 2;
updateTree(start, mid, node * 2, index, diff);
updateTree(mid + 1, end, node * 2 + 1, index, diff);
}
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int N, Q;
cin >> N >> Q;
nums.assign(N + 1, 0);
tree.assign(4 * (N + 1), 0);
for (int i = 1; i <= N; i++) {
cin >> nums[i];
}
makeTree(1, N, 1);
while (Q--) {
int x, y, a, b;
cin >> x >> y >> a >> b;
if (x > y) swap(x, y);
cout << solution(1, N, x, y, 1) << '\n';
long long diff = b - nums[a];
updateTree(1, N, 1, a, diff);
nums[a] = b;
}
return 0;
}
9kyo-hwang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ๊ฐ ์ซ์ต๋๋ค ใ ์๋ ์ด๊ฑฐ ๋น์ทํ ํ์ ํธ๋ฆฌ ๋ฑ๋ฑ๋...
๊ทธ๋ฐ๋ฐ ๊ธฐ์ ์ฝํ ์๋ ๋น์ทํ ์ ํ์ด ํ ๋ฒ์ฉ ๋์ต๋๋ค. ๋ํ์ ์ธ๊ฒ 2022๋ ์นด์นด์ค ๋ธ๋ผ์ธ๋ ์ฑ์ฉ ์ฝ๋ฉํ ์คํธ 6๋ฒ ๋ฌธ์ ํ๊ดด๋์ง ์์ ๊ฑด๋ฌผ ์ฃ . ๋ฌผ๋ก ์ถ๋ ฅ์ ๋งจ ๋ง์ง๋ง์ ํ ๋ฒ๋ง ์๊ตฌํ๊ธฐ ๋๋ฌธ์ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ๋ฅผ ์ธ ํ์๋ ์์ด ๋์ ํฉ์ ์ฐ๋ฉด ๋์ฃ . ํ์ง๋ง ์ด ๊ธฐ๋ฒ์ ์๋ค๋ฉด ๋ฐ๋ก ์ฑ์ฑ ์จ๋ฒ๋ฆฌ๋ฉด ๋๊ธด ํฉ๋๋ค.
ํ์ง๋ง ์ ์ฌ์ ํ ๋๋ฌด๋ ์ซ๊ธฐ ๋๋ฌธ์, Sqrt Decomposition ๊ธฐ๋ฒ์ ์ฌ์ฉํ์ต๋๋ค. ์ฌ์ค ์์ ์ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ ๋ํ ๋ฌธ์ ์ธ ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ ์ด๊ฑฐ ํ ๋ ์งฐ๋ ์ฝ๋์ธ๋ฐ ๋ฌธ์ ๊ฐ ๋๊ฐ์์ ๊ทธ๋ฅ ๋ณต๋ถ ํ์ต๋๋ค ใ
... ๋์ ์์ ์ ํผ ๊ฑด ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ๋ ์๋๊ฐ ๋น์ทํ๋๋ฐ ์ด๋ฒ์๋ ์๊ฐ ํ์คํ ๋๋ฆฌ๋ค์. ์ญ์ log(N)๋ณด๋ค ๋๋ฆด ์ ๋ฐ์ ์๊ตฐ์...
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
using int64 = long long;
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int N, Q; cin >> N >> Q;
vector<int64> Sequence(N);
for(int64& Num : Sequence) cin >> Num;
int BlockSize = static_cast<int>(ceil(sqrt(N)));
int BlockCount = static_cast<int>(ceil(N / static_cast<float>(BlockSize)));
vector<int64> BlockSum(BlockCount, 0);
for(int i = 0; i < N; ++i)
{
BlockSum[i / BlockSize] += Sequence[i];
}
while(Q--)
{
int64 x, y, a, b; cin >> x >> y >> a >> b;
if(x > y) swap(x, y);
--x; --y; --a;
int64 Sum = 0;
int Begin = x / BlockSize;
int End = y / BlockSize;
for(int i = Begin; i <= End; ++i)
{
Sum += BlockSum[i];
}
for(int i = Begin * BlockSize; i < x; ++i)
{
Sum -= Sequence[i];
}
for(int i = y + 1; i < min(N, (End + 1) * BlockSize); ++i)
{
Sum -= Sequence[i];
}
cout << Sum << "\n";
int64 Prev = Sequence[a];
Sequence[a] = b;
int64 Diff = Prev - b;
BlockSum[a / BlockSize] -= Diff;
}
return 0;
}
kokeunho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ์์น ์์ ์ ๋ ฅ ๋ฒ์์ ์์งํ ์ผ๋ง ๊ณ ๋ฏผ๋ ์ํ์ต๋๋ค ใ ..
์ธ๊ทธ๋จผํธ ํธ๋ฆฌ๋ผ๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋๋ถ์ ๊ณต๋ถํด๋ณผ ์ ์์์ต๋๋ค.
์ฒจ๋ถํด์ฃผ์ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ ์ค๋ช
๋งํฌ๊ฐ ์ดํดํ๋๋ฐ ํฐ ๋์์ด ๋์ต๋๋ค!
์ ์ตํ ๋ฌธ์ ๊ฐ์ฌํฉ๋๋ค
์๊ณ ํ์
จ์ต๋๋ค~
Details
import java.util.*;
public class Main {
static int N, Q;
static long[] tree;
static int size;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
Q = sc.nextInt();
long[] arr = new long[N+1];
for (int i = 1; i <= N; i++) {
arr[i] = sc.nextLong();
}
int h = (int)Math.ceil(Math.log(N) / Math.log(2));
size = 1 << (h+1);
tree = new long[size];
init(arr, 1, N, 1);
for (int i = 0; i < Q; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
int a = sc.nextInt();
long b = sc.nextLong();
int left = Math.min(x, y);
int right = Math.max(x, y);
System.out.println(query(1, N, left, right, 1));
update(1, N, a, b, 1);
}
}
static void init(long[] arr, int start, int end, int node) {
if (start == end) {
tree[node] = arr[start];
} else {
init(arr, start, (start+end)/2, 2*node);
init(arr, (start+end)/2+1, end, 2*node+1);
tree[node] = tree[2*node] + tree[2*node+1];
}
}
static long query(int start, int end, int left, int right, int node) {
if (left > end || right < start) return 0;
if (left <= start && end <= right) return tree[node];
long lsum = query(start, (start+end)/2, left, right, 2*node);
long rsum = query((start+end)/2+1, end, left, right, 2*node+1);
return lsum + rsum;
}
static void update(int start, int end, int index, long value, int node) {
if (index < start || index > end) return;
if (start == end) {
tree[node] = value;
return;
}
update(start, (start+end)/2, index, value, 2*node);
update((start+end)/2+1, end, index, value, 2*node+1);
tree[node] = tree[2*node] + tree[2*node+1];
}
}
wnsmir
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ต์ฅํ ๋ฌธ์ ๋ค์
์ธ๊ทธ๋จผํธํธ๋ฆฌ๋ผ๋๊ฑธ ์ฒ์์์์ต๋๋ค.
์๊ทธ๋๋ ํธ๋ฆฌ๊ฐ ์ฝํ๋ฐ,,
๊ตฌ๊ฐ์ ํฉ์ ์ ์ฅํ๊ณ , ์์๋ค์ ์ ๋ฐ์ ํธ๋ฆฌ์ ํด๋นํ๋ ๊ตฌ๊ฐํฉ๋ค์ ์ ์ฅํด๋๋ฉด
๊ฒฐ๊ตญ ๋ฆฌํ๋
ธ๋๋ค์ ์ธ๋ฑ์ค์ ๋ง๊ฒ ์ ์๊ฐ๋ ๊บผ๊ณ ,
๊ตฌ๊ฐํฉ์ ๊ตฌํ ๋๋, ์ธ๋ฑ์ค์ ์์๋ฅผ ๋ฐ๊ฟ๋๋ ๊ฒฐ๊ตญ ํธ๋ฆฌ๊ฐ ์
๋ฐ์ดํธ๋๋๋ฐ ๊น์ง๋ ํธ๋ฆฌ์ ๋์ด ์ฆ logN๋งํผ์ ์๊ฐ์ด ์์๋๊ธฐ ๋๋ฌธ์ ํต๊ณผ๋์์ต๋๋ค.
์ข์์๊ณ ๋ฆฌ์ฆํ๋ ์์๊ฐ๋๋ค~
N, Q = map(int, input().split())
arr = list(map(int, input().split()))
size = 1
while size < N:
size <<= 1
tree = [0] * (2 * size)
for i in range(N):
tree[size + i] = arr[i]
for i in range(size - 1, 0, -1):
tree[i] = tree[2 * i] + tree[2 * i + 1]
def range_query(l, r):
l += size
r += size
s = 0
while l <= r:
if l & 1:
s += tree[l]
l += 1
if not (r & 1):
s += tree[r]
r -= 1
l //= 2
r //= 2
return s
def point_update(pos, val):
i = size + pos
tree[i] = val
i //= 2
while i:
tree[i] = tree[2 * i] + tree[2 * i + 1]
i //= 2
out = []
for _ in range(Q):
x, y, a, b = map(int, input().split())
if x > y:
x, y = y, x
out.append(str(range_query(x - 1, y - 1)))
point_update(a - 1, b)
print("\n".join(out))
์ญ์ ๋ชจ๋ ๋ฌธ์ ๋ฅผ ์ญ๋ ตํ์
จ๊ตฐ์.. |
๐ ๋ฌธ์ ๋งํฌ
์ปคํผ์2
โ๏ธ ์์๋ ์๊ฐ
1h
โจ ์๋ ์ฝ๋
30๋ถ ์๊ฐํ๋ค๊ฐ ์๊ฐ์ด๊ณผ๋ฅผ ํด๊ฒฐํ ์ ์์ด์ ๊ทธ๋ฅ ๋ต์ง ๋ดค์ต๋๋ค.
์ ๊ฐ ์ฒ์ ์๊ฐํ๋๊ฑด ๋์ ํฉ์ ์ฌ์ฉํ ๋ฐฉ๋ฒ์ผ๋ก, ๋ฐ๋์ด์ผํ
(idx, val)๋ฅผ ๋ฆฌ์คํธ์ ๋ด๊ณ ์ ๋ต์ ๊ณ์ฐํ ๋ ์ฐจ๊ฐํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ คํ์ต๋๋ค.์ด๋ค ์์๊ฐ 3 -> 1์ด ๋๋ฉด ๊ตฌ๊ฐํฉ์ -2๊ฐ ๊ฐ์ํ๋๊ฑฐ๋ ๋ฌธ์ ์๋ค๊ณ ์๊ฐํ์ต๋๋ค. ํ์ง๋ง ์๊ฐ ์ด๊ณผ๊ฐ ๋ ๊ฒ๋๋ค. ์ด๊ฒ๋ O(N^2)์ด๋๊น์. ๊ตณ์ด ๋ฐ์ง์๋ฉด N(N-1) / 2์ด๊ธด ํ์ง๋ง ์๋ฌดํผ ํต๊ณผํ์ง ๋ชปํฉ๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํ๊ธฐ ์ํด์ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ๋ฅผ ์ดํดํด์ผ ํฉ๋๋ค. ์์ ์ ํ๋ฒ ํ์ด๋ดค๋๊ฑฐ ๊ฐ์๋ฐ ์ ๋๋ก ๊ณต๋ถํ์ง ์์ ๋ค ๊น๋จน์์ต๋๋ค ^^;
์ธ๊ทธ๋จผํธ ํธ๋ฆฌ๋ ๊ตฌ๊ฐํฉ์ ํธ๋ฆฌํ์์ผ๋ก ์ ์ฅํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋งํฉ๋๋ค. ๊ตฌ๊ฐํฉ์ ๋น ๋ฅด๊ฒ ๊ณ์ฐํด์ผํ ๋ ์ฃผ๋ก ์ฌ์ฉ๋ฉ๋๋ค. ๊ตฌ์ฑ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๋ฆฌํ๊ฐ ์๋ ๋ ธ๋์์ ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ ์์์ ํฉ์ ์ ์ฅํ๊ธฐ์ ๊ตฌ๊ฐ ํฉ์ด ์์ฑ๋๋ ๊ฒ๋๋ค.

๊ตฌ๊ฐํฉ์ ์บ์ฑํด๋ ํธ๋ฆฌ๋ผ๊ณ ์ดํดํ๋ฉด ๋ฉ๋๋ค. ์ด ํธ๋ฆฌ๋ฅผ ์ ์ฅํ๊ธฐ ์ํด์ ๋ฐฐ์ด์ ์ฌ์ฉํฉ๋๋ค.
Segment Tree๋Full Binary Tree์ด๊ณ ๊ฐ์ฅ ๊น์ ๋ ธ๋์ ๊ฐ์ฅ ๊น์ง ์์ ๋ ธ๋์ ๊น์ด ์ฐจ์ด๋ 1๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๊ธฐ ๋๋ฌธ์ ๋๋ค.์ ๋
tree = [0] * (4 * n)๋ก 4๋ฐฐํ๋๋ฐ ์ด๋ฌ๋ฉด ์ฌ์ ๋ก์ด ๋ฐฐ์ดํฌ๊ธฐ๋ผ ํ๋ค์(gpt).์ข ๋ ์ ํํ

size๋ฅผ ์ํ๋ค๋ฉด2^(h+1) - 1์ ๊ตฌํ์๋ฉด ๋ฉ๋๋ค. ๋์ด๋๊ตฌ๊ฐํฉ์ ์ด๋ป๊ฒ ๊ตฌํ ๊น์? ์ด๊ฑด ์ ์ค๋ช ๋ ๊ธ์ ์ฒจ๋ถํ๊ฒ ์ต๋๋น.. ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ์ ๊ตฌํ์ ๋ํด ์ ์ค๋ช ํด๋์๋๋ผ๊ตฌ์.
์ฌ๊ธฐ ๊ธ์์
start์end๋node์ ์ ์ฅ๋ ๊ฐ์ ๋ฒ์์ ๋๋ค. (ex. 0~2 ๊ตฌ๊ฐํฉ => start = 0, end = 2) ํท๊ฐ๋ฆฌ์ง ๋ง์ธ์ (์ ์ฒ๋ผ)BOJ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ