<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>백준 2042번 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/%EB%B0%B1%EC%A4%80-2042%EB%B2%88/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Fri, 15 Mar 2024 15:47:50 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://lycos7560.com/wp-content/uploads/2022/11/cropped-cropped-cropped-log-1-150x150-1-80x80.png</url>
	<title>백준 2042번 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>백준 2042번 (구간 합 구하기, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2042%eb%b2%88-%ea%b5%ac%ea%b0%84-%ed%95%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-c-baekjoon/36648/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2042%eb%b2%88-%ea%b5%ac%ea%b0%84-%ed%95%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-c-baekjoon/36648/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Wed, 30 Aug 2023 16:26:47 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2042]]></category>
		<category><![CDATA[2042번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Gold]]></category>
		<category><![CDATA[Gold1]]></category>
		<category><![CDATA[Segment Tree]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[골드]]></category>
		<category><![CDATA[골드1]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[구간 합 구하기]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2042]]></category>
		<category><![CDATA[백준 2042번]]></category>
		<category><![CDATA[세그먼트 트리]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36648</guid>

					<description><![CDATA[<p>백준 2042번 '구간 합 구하기' 문제에 대한 글입니다. '세그먼트 트리(Segment Tree)' 알고리즘을 이용하여 해결하였습니다. (This article is about Baekjoon Number 2042 'Summing the intervals' problem. It was solved using the 'Segment Tree' algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2042%eb%b2%88-%ea%b5%ac%ea%b0%84-%ed%95%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-c-baekjoon/36648/">백준 2042번 (구간 합 구하기, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-75779d84      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#구간-합-구하기" class="uagb-toc-link__trigger">구간 합 구하기</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#참고-자료" class="uagb-toc-link__trigger">참고 자료</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">구간 합 구하기 <img decoding="async" width="30" height="38" class="wp-image-37902" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_1.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_1.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_1-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p class="wp-block-paragraph"><a href="https://www.acmicpc.net/problem/2042" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2042</a></p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">86858</td><td class="has-text-align-left" data-align="left">20690</td><td class="has-text-align-left" data-align="left">10615</td><td class="has-text-align-left" data-align="left">24.949%</td></tr></tbody></table></figure>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p class="wp-block-paragraph">어떤 N개의 수가 주어져 있다. </p>



<p class="wp-block-paragraph">그런데 중간에 수의 변경이 빈번히 일어나고 그 중간에 어떤 부분의 합을 구하려 한다. </p>



<p class="wp-block-paragraph">만약에 1,2,3,4,5 라는 수가 있고, 3번째 수를 6으로 바꾸고 2번째부터 5번째까지 합을 구하라고 한다면 17을 출력하면 되는 것이다. </p>



<p class="wp-block-paragraph">그리고 그 상태에서 다섯 번째 수를 2로 바꾸고 3번째부터 5번째까지 합을 구하라고 한다면 12가 될 것이다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p class="wp-block-paragraph">첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)과 M(1 ≤ M ≤ 10,000), K(1 ≤ K ≤ 10,000) 가 주어진다. </p>



<p class="wp-block-paragraph">M은 수의 변경이 일어나는 횟수이고, K는 구간의 합을 구하는 횟수이다. </p>



<p class="wp-block-paragraph">그리고 둘째 줄부터 N+1번째 줄까지 N개의 수가 주어진다. </p>



<p class="wp-block-paragraph">그리고 N+2번째 줄부터 N+M+K+1번째 줄까지 세 개의 정수 a, b, c가 주어지는데, </p>



<p class="wp-block-paragraph">a가 1인 경우 b(1 ≤ b ≤ N)번째 수를 c로 바꾸고 </p>



<p class="wp-block-paragraph">a가 2인 경우에는 b(1 ≤ b ≤ N)번째 수부터 c(b ≤ c ≤ N)번째 수까지의 합을 구하여 출력하면 된다.</p>



<p class="wp-block-paragraph">입력으로 주어지는 모든 수는 -2<sup>63</sup>보다 크거나 같고, 2<sup>63</sup>-1보다 작거나 같은 정수이다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p class="wp-block-paragraph">첫째 줄부터 K줄에 걸쳐 구한 구간의 합을 출력한다. </p>



<p class="wp-block-paragraph">단, 정답은 -2<sup>63</sup>보다 크거나 같고, 2<sup>63</sup>-1보다 작거나 같은 정수이다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5 2 2
1
2
3
4
5
1 3 6
2 2 5
1 5 2
2 3 5</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">17
12</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 A</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 1 2
5000000000000000000
2 1 1
1 1 -5000000000000000000
2 1 1</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 A</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5000000000000000000
-5000000000000000000</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 B</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5 2 2
1
2
3
4
5
1 3 6
2 2 5
1 3 7
2 2 5</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 B</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">17
18</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<ul class="wp-block-list">
<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/79brue">79brue</a>,&nbsp;<a href="https://www.acmicpc.net/user/keunbum">keunbum</a>,&nbsp;<a href="https://www.acmicpc.net/user/Nyan101">Nyan101</a>,&nbsp;<a href="https://www.acmicpc.net/user/tncks0121">tncks0121</a></li>



<li>빠진 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/79brue">79brue</a>,&nbsp;<a href="https://www.acmicpc.net/user/djm03178">djm03178</a>,&nbsp;<a href="https://www.acmicpc.net/user/jh05013">jh05013</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/eric00513">eric00513</a>,&nbsp;<a href="https://www.acmicpc.net/user/klm03025">klm03025</a>,&nbsp;<a href="https://www.acmicpc.net/user/yuja">yuja</a></li>



<li>잘못된 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/WeissBlume">WeissBlume</a></li>
</ul>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/65" target="_blank" rel="noreferrer noopener">세그먼트 트리</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading has-large-font-size">참고 자료</h2>



<p class="wp-block-paragraph">세그먼트 트리를 이해하는데 아래의 강의와 링크에 많은 도움을 받았다.</p>



<p class="wp-block-paragraph"><a href="https://blog.garybricks.com/segment-tree-introduction-in-c" target="_blank" rel="noreferrer noopener">https://blog.garybricks.com/segment-tree-introduction-in-c</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio" style="max-width: 50%;">
  <div class="wp-block-embed__wrapper">
    <iframe width="1120" height="630" src="https://www.youtube.com/embed/1d9sqmuLy-o" frameborder="0" allowfullscreen></iframe>
  </div>
</figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<p class="wp-block-paragraph">세그먼트 트리(Segment Tree)를 이용하여 구간합을 구하는 문제</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
using namespace std;
typedef long long ll;
int _N, _M, _K;
int _SegSize, _Num;
ll* _Numbers;
ll* _SegmentTree;

void InitSegTree()
{
    _Num = 1;
    while (_Num &lt; _N)
        _Num *= 2;
    _SegSize = _Num * 2;
    _SegmentTree = new ll[_SegSize]();
    for (int i = _Num, j = 0; j &lt; _N; j++)
        _SegmentTree[i + j] = _Numbers[j + 1];
    for (int i = _SegSize / 2 - 1; i > 0; i--)
        _SegmentTree[i] = _SegmentTree[i * 2] + _SegmentTree[i * 2 + 1];
}

void Update(int _Index, long long Value)
{
    _Index += _SegSize / 2 - 1;
    _SegmentTree[_Index] = Value;
    while (_Index != 0) {
        _Index /= 2;
        _SegmentTree[_Index] = _SegmentTree[_Index * 2] + _SegmentTree[_Index * 2 + 1];
    }
}

ll Sum(int _s, int _e)
{
    ll _Res = 0;
    while (_s &lt;= _e) {
        if (_s % 2 == 1)
            _Res += _SegmentTree[_s];
        if (_e % 2 == 0)
            _Res += _SegmentTree[_e];
        _s = (_s + 1) / 2;
        _e = (_e - 1) / 2;
    }
    return _Res;
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> _N >> _M >> _K;
    _Numbers = new ll[_N + 1]();
    for (int i = 1; i &lt;= _N; i++)
        cin >> _Numbers[i];
    InitSegTree();

    ll _t1, _t2, _O;
    while (_M + _K > 0) {
        cin >> _O >> _t1 >> _t2;
        if (_O == 1) {
            Update(_t1, _t2);
            _M--;
        }
        else if (_O == 2) {
            cout &lt;&lt; Sum(_t1 + _Num - 1, _t2 + _Num - 1) &lt;&lt; "\n";
            _K--;
        }
    }
    return 0;
}
</pre>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img fetchpriority="high" decoding="async" width="1207" height="229" src="https://lycos7560.com/wp-content/uploads/2023/08/image-31.png" alt="" class="wp-image-36656" style="width:902px;height:171px" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-31.png 1207w, https://lycos7560.com/wp-content/uploads/2023/08/image-31-300x57.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-31-768x146.png 768w" sizes="(max-width: 1207px) 100vw, 1207px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="4386247858"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2042%eb%b2%88-%ea%b5%ac%ea%b0%84-%ed%95%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-c-baekjoon/36648/">백준 2042번 (구간 합 구하기, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2042%eb%b2%88-%ea%b5%ac%ea%b0%84-%ed%95%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-c-baekjoon/36648/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
