๋์ด๋ | ์ ๋ต๋ฅ |
โ โ โ โ | -% |
ํ๋ฆฌ๋ฏธ์ ์๊ณ ๋ฆฌ์ฆ ์ํด๋ฆฌ ๋นํ์๊ณ ์์ฆ3 3์ 5์ฃผ์ฐจ
goorm
๊ตฌ๋ฆ์ ํด๋ผ์ฐ๋ ๊ธฐ์ ์ ์ด์ฉํ์ฌ ๋๊ตฌ๋ ์ฝ๋ฉ์ ๋ฐฐ์ฐ๊ณ , ์ค๋ ฅ์ ํ๊ฐํ๊ณ , ์ํํธ์จ์ด๋ฅผ ๊ฐ๋ฐํ ์ ์๋ ํด๋ผ์ฐ๋ ์ํํธ์จ์ด ์ํ๊ณ์ ๋๋ค.
www.goorm.io
๋ฌธ์
์ํ์ฐฉ์ค
blog.naver.com
์ด ๋ธ๋ก๊ทธ๊ฐ ๋์์ด ๋ง์ด ๋๋ค. ์ฒ์์ ๋์ ํฉ์ผ๋ก ํ๋ค๊ฐ ๊ฐฑ์ ํ๋๋ฐ ์ด์ฐจํผ n*n ์๊ฐ์ด๊ณผ๋ผ์... ๊ฒฐ๊ตญ ๊ฒ์์ ํ๋ค. 'c++ ๋์ ํฉ ๊ฐฑ์ ' ์ด๋ฐ์์ผ๋ก ๊ฒ์ํ๋ค๊ฐ ์ฐพ์ ๋ธ๋ก๊ทธ์ธ๋ฐ ์ ๋ฆฌ๊ฐ ์ ๋์ด ์์ด์ ์ข์๋ค.
์๋ ์ฝ๋๋ indexed tree ๋ฅผ ์ด์ฉํด์ ํ์๊ณ '์์ง 93์ . 15๋ฒ ํ ์คํธ์ผ์ด์ค ํ๋ฆผ.' ์ํ์ด๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int ll;
int n, m, base;
ll idt[1 << 27];
void update(int pos, int num) {
idt[pos] -= num;
pos >>= 1; // pos /= 2;
while (pos) {
idt[pos] = idt[pos * 2] + idt[pos * 2 + 1];
pos /= 2;
}
}
ll sum(int st, int end) {
ll ret = 0;
while (st < end) {
if (st % 2 == 1) ret += idt[st], st++;
if (!(end % 2)) ret += idt[end], end--;
st >>= 1, end >>= 1;
}
if (st == end) ret += idt[st];
return ret;
}
int main() {
scanf("%d %d", &n, &m);
for (base = 1; base < n; base *= 2);
for (int i = base; i < base + n; i++) {
scanf("%d", &idt[i]);
}
for (int i = base - 1; i > 0; i--) {
idt[i] = idt[i * 2] + idt[i * 2 + 1];
}
while (m--) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (a == 1) {
printf("%lld\n", sum(base + b - 1, base + c - 1));
}
else {
update(base + b - 1, c);
}
}
return 0;
}
|
cs |
ํด๊ฒฐ
key point, indexed tree
์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int ll;
int n, m, base;
ll idt[1 << 25];
void update(int pos, int num) {
idt[pos] -= num;
pos >>= 1; // pos /= 2;
while (pos) {
idt[pos] = idt[pos * 2] + idt[pos * 2 + 1];
pos /= 2;
}
}
ll sum(int st, int end) {
ll ret = 0;
while (st < end) {
if (st % 2 == 1) ret += idt[st], st++;
if (!(end % 2)) ret += idt[end], end--;
st >>= 1, end >>= 1;
}
if (st == end) ret += idt[st];
return ret;
}
int main() {
scanf("%d %d", &n, &m);
for (base = 1; base < n; base *= 2);
for (int i = base; i < base + n; i++) {
scanf("%d", &idt[i]);
}
for (int i = base - 1; i > 0; i--) {
idt[i] = idt[i * 2] + idt[i * 2 + 1];
}
while (m--) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (a == 1) {
printf("%lld\n", sum(base + b - 1, base + c - 1));
}
else {
update(base + b - 1, c);
}
}
return 0;
}
|
cs |
ํ๊ธฐ
์ ๊ทธ๋ฅ ๋๋ฌด ๋ชจ๋ฅด๊ฒ ์ด์ ์ฝ๋ ์ฌ๋ ค๋๊ณ ์ง๋ฌธ์ ํ๋๋ ์ ์๋???์ด ํ ์คํธ์ผ์ด์ค ์ค๋ฅ๋ผ๊ณ ๋ต๋ณํด์ฃผ์ ์ ๊ทธ๋๋ก ๋ค์ ์ ์ถํ๋๊น ์ ๋ต๋ด๋ค!! ์ด๋ฐ์ ์ฒ์์ด๋ผ ๋ฏ์ค๊ณ ๊ธฐ๋ถ์ข๋ค ^^. ์์ฆ ํ๊ต ์ฌ์ด๋ฒ๊ฐ์ ๋ฃ๊ณ ๊ณผ์ ํ๋๋ผ ๋๋ฌด ์ค๋๋ง์ ๋ค์ด๊ฐ๋๋ฐ ์ข์ ์์์์ด์ ๋๋ฌด ์ข๋ค~~
'๐ฅ PS(Problem Solving) ๐ฅ > goorm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ตฌ๋ฆLEVEL] ํํ์ฝํ (0) | 2020.04.30 |
---|---|
[๊ตฌ๋ฆLEVEL] ์จ๋ผ์ธ ๊ฐ์ (0) | 2020.04.01 |
[๊ตฌ๋ฆLEVEL] ๋ง๊ฐ์ง ์๋ผํ ์คํ ๋ค์ค์ ์ฒด (0) | 2020.03.27 |
[๊ตฌ๋ฆLEVEL] ๋ฒฝ ํต๊ณผํ๊ธฐ (0) | 2020.03.25 |
[๊ตฌ๋ฆLEVEL] ๋ฏธ๋ก์ฐพ๊ธฐ (0) | 2020.03.24 |
๋๊ธ