POJ 3928 Ping pong(树状数组)

这道题的题意就是给你一个一维坐标系,坐标系代表着一条街道,每个点上住着一个能力值为ai的人,他们之间总要相互比试,每次比试都需要个裁判,但是每个裁判都要在他们的两个人的坐标中间,问你所有的人两两比试,有多少种比试的方法。

思路就是

考虑把每个人当裁判的情况数求出来,加一起就是答案了。先求出对于每个人的左边比他能力值小的人的个数f[i],再求出他的右边每个能力值比他小的人的个数r[i] 。

[i]*(n-1-i-r[i])+r[i]*(i-f[i]);

AC代码:

<pre class="sh-cpp sh-sourceCode" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding: 5px 5px 5px 7px; overflow: auto; font-size: 16px; line-height: 17.6px; font-family: 'Courier New', Courier, monospace;"><span class="sh-comment" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(102, 102, 102); font-style: italic;">/* ***********************************************</span>
<span class="sh-comment" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(102, 102, 102); font-style: italic;">Author        :yzkAccepted</span>
<span class="sh-comment" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(102, 102, 102); font-style: italic;">Created Time  :2016/3/4 10:03:12</span>
<span class="sh-comment" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(102, 102, 102); font-style: italic;">TASK		  :ggfly.cpp</span>
<span class="sh-comment" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(102, 102, 102); font-style: italic;">LANG          :C++</span>
<span class="sh-comment" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(102, 102, 102); font-style: italic;">************************************************ */</span>

<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><iostream></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><cstdio></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><cstring></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><algorithm></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><vector></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><queue></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><set></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><map></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><string></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><cmath></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><cstdlib></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><ctime></span>
<span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span> <span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><stack></span>
<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">using</span> <span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">namespace</span> std<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>
<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">typedef</span> __<span class="sh-usertype" style="margin: 0px; padding: 0px; line-height: 1.1em;">int64</span><span class="sh-normal" style="margin: 0px; padding: 0px; line-height: 1.1em;"> </span>ll<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>
<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">const</span> <span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> maxn<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">100010</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>
<span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> n<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>a<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>maxn<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">],</span>f<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>maxn<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">],</span>r<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>maxn<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">],</span>d<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>maxn<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">],</span>lowbit<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>maxn<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">];</span>
<span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">void</span> <span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">add</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span><span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">)</span>
<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span>
	<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">while</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><</span>maxn<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">)</span>
	<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span>
		a<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]++;</span>
		k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">+=</span>lowbit<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">];</span>
	<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span>
<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span>
<span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> <span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">sum</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span><span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">)</span>
<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span>
	<span class="sh-usertype" style="margin: 0px; padding: 0px; line-height: 1.1em;">ll</span><span class="sh-normal" style="margin: 0px; padding: 0px; line-height: 1.1em;"> </span>s<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>
	<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">while</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">></span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">)</span>
	<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span>
		s<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">+=</span>a<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">];</span>
		k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">-=</span>lowbit<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">];</span>
	<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span>
	<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">return</span> s<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>
<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span>
<span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> <span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">main</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">()</span>
<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span>
	<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">for</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span><span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">1</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><</span>maxn<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">++)</span> lowbit<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]=</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">&-</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>
    <span class="sh-comment" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(102, 102, 102); font-style: italic;">//freopen("in.txt","r",stdin);</span>
    <span class="sh-comment" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(102, 102, 102); font-style: italic;">//freopen("out.txt","w",stdout);</span>
	<span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> t<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>cmp<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>
	<span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">scanf</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span><span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">"%d"</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,&</span>t<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">);</span>
	<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">while</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>t<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">--)</span>
	<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span>
		<span class="sh-usertype" style="margin: 0px; padding: 0px; line-height: 1.1em;">ll</span><span class="sh-normal" style="margin: 0px; padding: 0px; line-height: 1.1em;"> </span>ans<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>
		<span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">scanf</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span><span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">"%d"</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,&</span>n<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">);</span>
		<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">for</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span><span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><</span>n<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">++)</span>
			<span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">scanf</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span><span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">"%d"</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,&</span>d<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]);</span>
		<span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">memset</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>a<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span><span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">sizeof</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>a<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">));</span>
		<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">for</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span><span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><</span>n<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">++)</span>
		<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span>
			f<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]=</span><span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">sum</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>d<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]-</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">1</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">);</span>
			<span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">add</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>d<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]);</span>
		<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span>
		<span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">memset</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>a<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span><span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">sizeof</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>a<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">));</span>
		<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">for</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span><span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span>n<span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">-1</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">>=</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">--)</span>
		<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span>
			r<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]=</span><span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">sum</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>d<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]-</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">1</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">);</span>
			<span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">add</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>d<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]);</span>
		<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span>
		 <span class="sh-comment" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(102, 102, 102); font-style: italic;">//for(int i=0;i<n;i++) printf("%d ",f[i]); printf("\n");</span>
        <span class="sh-comment" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(102, 102, 102); font-style: italic;">//for(int i=0;i<n;i++) printf("%d ",r[i]); printf("\n");</span>
		<span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">for</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span><span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><</span>n<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">++)</span>
		<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span>
			ans<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">+=</span>f<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]*(</span>n<span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">-1</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">-</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">-</span>r<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">])+</span>r<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]*(</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">-</span>f<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">[</span>i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">]);</span>
		<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span>
		cout<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><<</span>ans<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><<</span>endl<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>
	<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span>
    <span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">return</span> <span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span>
<span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span>
 
 


你可能感兴趣的:(poj,树状数组)