﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-ACM___________________________-随笔分类-ACM ( 数据结构 )</title><link>http://www.cppblog.com/MiYu/category/14439.html</link><description>                             ______________白白の屋</description><language>zh-cn</language><lastBuildDate>Tue, 16 Nov 2010 20:50:30 GMT</lastBuildDate><pubDate>Tue, 16 Nov 2010 20:50:30 GMT</pubDate><ttl>60</ttl><item><title>HDU 3468 HDOJ 3468 A Simple Problem with Integers ACM 3468 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/11/16/133824.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Tue, 16 Nov 2010 13:28:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/11/16/133824.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/133824.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/11/16/133824.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/133824.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/133824.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;代码&lt;pre&nbsp;lang="cpp"&nbsp;line="1"&gt;/*Mail&nbsp;to&nbsp;&nbsp;&nbsp;:&nbsp;miyubai@gamil.comMy&nbsp;Blog&nbsp;&nbsp;&nbsp;:&nb...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/11/16/133824.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/133824.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-11-16 21:28 <a href="http://www.cppblog.com/MiYu/archive/2010/11/16/133824.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU 3584 HDOJ 3584 Cube  ACM 3584 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/11/14/133577.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sun, 14 Nov 2010 02:35:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/11/14/133577.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/133577.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/11/14/133577.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/133577.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/133577.html</trackback:ping><description><![CDATA[
<span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://baiyun.me/"><font color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目描述:</p><span style="font-family: 'Times New Roman'; line-height: normal; font-size: medium; "><h1 style="color: rgb(26, 92, 200); text-align: center; ">Cube</h1><font><strong><span style="font-family: Arial; font-size: 12px; font-weight: bold; color: green; ">Time Limit: 2000/1000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 131072/65536 K (Java/Others)<br>Total Submission(s): 495&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 226<br></span></strong></font><br><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the number in the i-th row , j-th column and k-th layer. Initially we have A[i, j, k] = 0 (1 &lt;= i, j, k &lt;= N).&nbsp;<br>We define two operations, 1: &#8220;Not&#8221; operation that we change the A[i, j, k]=!A[i, j, k]. that means we change A[i, j, k] from 0-&gt;1,or 1-&gt;0. (x1&lt;=i&lt;=x2,y1&lt;=j&lt;=y2,z1&lt;=k&lt;=z2).<br>0: &#8220;Query&#8221; operation we want to get the value of A[i, j, k].<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">Multi-cases.<br>First line contains N and M, M lines follow indicating the operation below.<br>Each operation contains an X, the type of operation. 1: &#8220;Not&#8221; operation and 0: &#8220;Query&#8221; operation.<br>If X is 1, following x1, y1, z1, x2, y2, z2.<br>If X is 0, following x, y, z.<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">For each query output A[x, y, z] in one line. (1&lt;=n&lt;=100 sum of m &lt;=10000)</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">2 5
1 1 1 1  1 1 1
0 1 1 1
1 1 1 1  2 2 2
0 1 1 1
0 2 2 2</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">1
0
1</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div></span><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目分析 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp;更新区间, 查询一个点, 三维线段树 直接无视.........数据不是很强, 所以 直接暴力就可以过, 过完发现自己的时间 在700MS 左右, 看了下rank,</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">小A 榜首..... 时间竟然才62MS<img src="http://www.cnblogs.com/Emoticons/baimantou/134950312.gif">....果然还是要用三维树状数组来加速啊 . 不过一直没理解 用 树状数组 解决这题的思路, 今早上终于明白了<img src="http://www.cnblogs.com/Emoticons/baimantou/22571567.gif">.</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">画个图先 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="http://www.cnblogs.com/images/cnblogs_com/miyu/HDU_3485.JPG" width="422" height="193" alt="">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">好了 , 看代码:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">树状数组代码 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;<span style="font-family: 'Courier New'; line-height: normal; font-size: 12px; color: rgb(0, 128, 0); white-space: pre; ">/*</span></p><span style="font-family: 'Times New Roman'; line-height: normal; font-size: 12px; "><div style="padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; "><pre style="font-family: 'Courier New'; text-align: left; "><font color="green">Mail to   : miyubai@gamil.com
My Blog   : www.baiyun.me
Link      : http://www.cnblogs.com/MiYu  || http://www.cppblog.com/MiYu
Author By : MiYu
Test      : 1
Complier  : g++ mingw32-3.4.2
Program   : HDU_3584
Doc Name  : Cube
*/</font><font color="green">
//#pragma warning( disable:4789 )
</font><font color="blue">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
#include &lt;string&gt;
#include &lt;set&gt;
#include &lt;map&gt;
#include &lt;utility&gt;
#include &lt;queue&gt;
#include &lt;stack&gt;
#include &lt;list&gt;
#include &lt;vector&gt;
#include &lt;cstdio&gt;
#include &lt;cstdlib&gt;
#include &lt;cstring&gt;
#include &lt;cmath&gt;
#include &lt;ctime&gt;
</font><strong><font color="#0000FF">using namespace</font></strong> std<strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">
inline</font></strong><strong><font color="blue"> bool</font></strong> scan_d<strong><font color="#FF00FF">(</font></strong><strong><font color="blue">int</font></strong><strong><font color="#FF00FF"> &amp;</font></strong>num<strong><font color="#FF00FF">)</font></strong><font color="green">  //整数输入
</font><strong><font color="#FF00FF">{</font></strong><strong><font color="blue">
        char</font></strong> in<strong><font color="#FF00FF">;</font></strong><strong><font color="blue">bool</font></strong> IsN<strong><font color="#FF00FF">=</font></strong><font color="#CC3300">false</font><strong><font color="#FF00FF">;</font></strong>
        in<strong><font color="#FF00FF">=</font></strong>getchar<strong><font color="#FF00FF">();</font></strong><strong><font color="#0000FF">
        if</font></strong><strong><font color="#FF00FF">(</font></strong>in<strong><font color="#FF00FF">==</font></strong>EOF<strong><font color="#FF00FF">)</font></strong><strong><font color="#0000FF"> return</font></strong><font color="#CC3300"> false</font><strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">
        while</font></strong><strong><font color="#FF00FF">(</font></strong>in<strong><font color="#FF00FF">!=</font></strong><font color="green">'-'</font><strong><font color="#FF00FF">&amp;&amp;(</font></strong>in<strong><font color="#FF00FF">&lt;</font></strong><font color="green">'0'</font><strong><font color="#FF00FF">||</font></strong>in<strong><font color="#FF00FF">&gt;</font></strong><font color="green">'9'</font><strong><font color="#FF00FF">))</font></strong> in<strong><font color="#FF00FF">=</font></strong>getchar<strong><font color="#FF00FF">();</font></strong><strong><font color="#0000FF">
        if</font></strong><strong><font color="#FF00FF">(</font></strong>in<strong><font color="#FF00FF">==</font></strong><font color="green">'-'</font><strong><font color="#FF00FF">){</font></strong> IsN<strong><font color="#FF00FF">=</font></strong><font color="#CC3300">true</font><strong><font color="#FF00FF">;</font></strong>num<strong><font color="#FF00FF">=</font></strong><font color="#CC3300">0</font><strong><font color="#FF00FF">;}</font></strong><strong><font color="#0000FF">
        else</font></strong> num<strong><font color="#FF00FF">=</font></strong>in<strong><font color="#FF00FF">-</font></strong><font color="green">'0'</font><strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">
        while</font></strong><strong><font color="#FF00FF">(</font></strong>in<strong><font color="#FF00FF">=</font></strong>getchar<strong><font color="#FF00FF">(),</font></strong>in<strong><font color="#FF00FF">&gt;=</font></strong><font color="green">'0'</font><strong><font color="#FF00FF">&amp;&amp;</font></strong>in<strong><font color="#FF00FF">&lt;=</font></strong><font color="green">'9'</font><strong><font color="#FF00FF">){</font></strong>
                num<strong><font color="#FF00FF">*=</font></strong><font color="#CC3300">10</font><strong><font color="#FF00FF">,</font></strong>num<strong><font color="#FF00FF">+=</font></strong>in<strong><font color="#FF00FF">-</font></strong><font color="green">'0'</font><strong><font color="#FF00FF">;
        }</font></strong><strong><font color="#0000FF">
        if</font></strong><strong><font color="#FF00FF">(</font></strong>IsN<strong><font color="#FF00FF">)</font></strong> num<strong><font color="#FF00FF">=-</font></strong>num<strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">
        return</font></strong><font color="#CC3300"> true</font><strong><font color="#FF00FF">;
}</font></strong><strong><font color="#0000FF">
const</font></strong><strong><font color="blue"> int</font></strong> MAXN<strong><font color="#FF00FF"> =</font></strong><font color="#CC3300"> 105</font><strong><font color="#FF00FF">;</font></strong><strong><font color="blue">
int</font></strong> mat<strong><font color="#FF00FF">[</font></strong>MAXN<strong><font color="#FF00FF">][</font></strong>MAXN<strong><font color="#FF00FF">][</font></strong>MAXN<strong><font color="#FF00FF">];</font></strong><strong><font color="blue">
int</font></strong> low<strong><font color="#FF00FF">[</font></strong>MAXN<strong><font color="#FF00FF">];</font></strong><strong><font color="blue">
int</font></strong> i<strong><font color="#FF00FF">,</font></strong> j<strong><font color="#FF00FF">,</font></strong> k<strong><font color="#FF00FF">;</font></strong><strong><font color="blue">
void</font></strong> setLow<strong><font color="#FF00FF"> () {</font></strong><strong><font color="#0000FF">
     for</font></strong><strong><font color="#FF00FF"> (</font></strong> i<strong><font color="#FF00FF"> =</font></strong><font color="#CC3300"> 1</font><strong><font color="#FF00FF">;</font></strong> i<strong><font color="#FF00FF"> &lt;=</font></strong> MAXN<strong><font color="#FF00FF">; ++</font></strong> i<strong><font color="#FF00FF"> )</font></strong> low<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">] =</font></strong> i<strong><font color="#FF00FF"> &amp; (-</font></strong> i<strong><font color="#FF00FF">);
}</font></strong><strong><font color="blue">
void</font></strong> modify<strong><font color="#FF00FF"> (</font></strong><strong><font color="blue"> int</font></strong> x<strong><font color="#FF00FF">,</font></strong><strong><font color="blue"> int</font></strong> y<strong><font color="#FF00FF">,</font></strong><strong><font color="blue"> int</font></strong> z<strong><font color="#FF00FF"> ) {</font></strong><strong><font color="#0000FF">
     for</font></strong><strong><font color="#FF00FF"> (</font></strong> i<strong><font color="#FF00FF"> =</font></strong> x<strong><font color="#FF00FF">;</font></strong> i<strong><font color="#FF00FF"> &lt;=</font></strong> MAXN<strong><font color="#FF00FF">;</font></strong> i<strong><font color="#FF00FF"> +=</font></strong> low<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">] ) {</font></strong><strong><font color="#0000FF">
         for</font></strong><strong><font color="#FF00FF"> (</font></strong> j<strong><font color="#FF00FF"> =</font></strong> y<strong><font color="#FF00FF">;</font></strong> j<strong><font color="#FF00FF"> &lt;=</font></strong> MAXN<strong><font color="#FF00FF">;</font></strong> j<strong><font color="#FF00FF"> +=</font></strong> low<strong><font color="#FF00FF">[</font></strong>j<strong><font color="#FF00FF">] ) {</font></strong><strong><font color="#0000FF">
             for</font></strong><strong><font color="#FF00FF"> (</font></strong> k<strong><font color="#FF00FF"> =</font></strong> z<strong><font color="#FF00FF">;</font></strong> k<strong><font color="#FF00FF"> &lt;=</font></strong> MAXN<strong><font color="#FF00FF">;</font></strong> k<strong><font color="#FF00FF"> +=</font></strong> low<strong><font color="#FF00FF">[</font></strong>k<strong><font color="#FF00FF">] )</font></strong>
                 mat<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">][</font></strong>j<strong><font color="#FF00FF">][</font></strong>k<strong><font color="#FF00FF">] ^=</font></strong><font color="#CC3300"> 1</font><strong><font color="#FF00FF">;   
         }    
     }    
}</font></strong><strong><font color="blue">
int</font></strong> query<strong><font color="#FF00FF"> (</font></strong><strong><font color="blue"> int</font></strong> x<strong><font color="#FF00FF">,</font></strong><strong><font color="blue"> int</font></strong> y<strong><font color="#FF00FF">,</font></strong><strong><font color="blue"> int</font></strong> z<strong><font color="#FF00FF"> ) {</font></strong><strong><font color="blue">
     int</font></strong> sum<strong><font color="#FF00FF"> =</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">
     for</font></strong><strong><font color="#FF00FF"> (</font></strong> i<strong><font color="#FF00FF"> =</font></strong> x<strong><font color="#FF00FF">;</font></strong> i<strong><font color="#FF00FF"> &gt;</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">;</font></strong> i<strong><font color="#FF00FF"> ^=</font></strong> low<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">] ) {</font></strong><strong><font color="#0000FF">
         for</font></strong><strong><font color="#FF00FF"> (</font></strong> j<strong><font color="#FF00FF"> =</font></strong> y<strong><font color="#FF00FF">;</font></strong> j<strong><font color="#FF00FF"> &gt;</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">;</font></strong> j<strong><font color="#FF00FF"> ^=</font></strong> low<strong><font color="#FF00FF">[</font></strong>j<strong><font color="#FF00FF">] ) {</font></strong><strong><font color="#0000FF">
             for</font></strong><strong><font color="#FF00FF"> (</font></strong> k<strong><font color="#FF00FF"> =</font></strong> z<strong><font color="#FF00FF">;</font></strong> k<strong><font color="#FF00FF"> &gt;</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">;</font></strong> k<strong><font color="#FF00FF"> ^=</font></strong> low<strong><font color="#FF00FF">[</font></strong>k<strong><font color="#FF00FF">] )</font></strong>
                 sum<strong><font color="#FF00FF"> +=</font></strong> mat<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">][</font></strong>j<strong><font color="#FF00FF">][</font></strong>k<strong><font color="#FF00FF">];    
         }    
     }</font></strong><strong><font color="#0000FF">    
     return</font></strong> sum<strong><font color="#FF00FF"> &amp;</font></strong><font color="#CC3300"> 1</font><strong><font color="#FF00FF">;
}</font></strong><strong><font color="blue">
int</font></strong><strong><font color="#0000FF"> main</font></strong><strong><font color="#FF00FF"> ()
{</font></strong>
    setLow<strong><font color="#FF00FF"> ();</font></strong><strong><font color="blue">
    int</font></strong> N<strong><font color="#FF00FF">,</font></strong> M<strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">
    while</font></strong><strong><font color="#FF00FF"> (</font></strong> scan_d<strong><font color="#FF00FF"> (</font></strong> N<strong><font color="#FF00FF"> ) &amp;&amp;</font></strong> scan_d<strong><font color="#FF00FF"> (</font></strong> M<strong><font color="#FF00FF"> ) ) {</font></strong>
          memset<strong><font color="#FF00FF"> (</font></strong> mat<strong><font color="#FF00FF">,</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">,</font></strong><strong><font color="#0000FF"> sizeof</font></strong><strong><font color="#FF00FF"> (</font></strong> mat<strong><font color="#FF00FF"> ) );</font></strong><strong><font color="#0000FF">
          while</font></strong><strong><font color="#FF00FF"> (</font></strong> M<strong><font color="#FF00FF"> -- ) {</font></strong><strong><font color="blue">
                int</font></strong> x1<strong><font color="#FF00FF">,</font></strong>y1<strong><font color="#FF00FF">,</font></strong>z1<strong><font color="#FF00FF">,</font></strong>x2<strong><font color="#FF00FF">,</font></strong>y2<strong><font color="#FF00FF">,</font></strong>z2<strong><font color="#FF00FF">;</font></strong><strong><font color="blue">
                int</font></strong> s<strong><font color="#FF00FF">;</font></strong>  scan_d<strong><font color="#FF00FF"> (</font></strong> s<strong><font color="#FF00FF"> );</font></strong><strong><font color="#0000FF"> 
                switch</font></strong><strong><font color="#FF00FF"> (</font></strong> s<strong><font color="#FF00FF"> ) {</font></strong><strong><font color="#0000FF">
                       case</font></strong><font color="#CC3300"> 1</font><strong><font color="#FF00FF">:</font></strong>
                            scan_d<strong><font color="#FF00FF"> (</font></strong> x1<strong><font color="#FF00FF"> );</font></strong> scan_d<strong><font color="#FF00FF"> (</font></strong> y1<strong><font color="#FF00FF"> );</font></strong> scan_d<strong><font color="#FF00FF"> (</font></strong> z1<strong><font color="#FF00FF"> );</font></strong> 
                            scan_d<strong><font color="#FF00FF"> (</font></strong> x2<strong><font color="#FF00FF"> );</font></strong> scan_d<strong><font color="#FF00FF"> (</font></strong> y2<strong><font color="#FF00FF"> );</font></strong> scan_d<strong><font color="#FF00FF"> (</font></strong> z2<strong><font color="#FF00FF"> );</font></strong>
                            modify<strong><font color="#FF00FF"> (</font></strong> x1<strong><font color="#FF00FF">,</font></strong>y1<strong><font color="#FF00FF">,</font></strong>z1<strong><font color="#FF00FF"> );</font></strong>    modify<strong><font color="#FF00FF"> (</font></strong> x1<strong><font color="#FF00FF">,</font></strong>y1<strong><font color="#FF00FF">,</font></strong>z2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF"> );</font></strong>
                            modify<strong><font color="#FF00FF"> (</font></strong> x2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF">,</font></strong>y1<strong><font color="#FF00FF">,</font></strong>z1<strong><font color="#FF00FF"> );</font></strong>    modify<strong><font color="#FF00FF"> (</font></strong> x2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF">,</font></strong>y1<strong><font color="#FF00FF">,</font></strong>z2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF"> );</font></strong>
                            modify<strong><font color="#FF00FF"> (</font></strong> x1<strong><font color="#FF00FF">,</font></strong>y2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF">,</font></strong>z1<strong><font color="#FF00FF"> );</font></strong>    modify<strong><font color="#FF00FF"> (</font></strong> x2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF">,</font></strong>y2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF">,</font></strong>z1<strong><font color="#FF00FF"> );</font></strong>
                            modify<strong><font color="#FF00FF"> (</font></strong> x2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF">,</font></strong>y2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF">,</font></strong>z2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF"> );</font></strong>    modify<strong><font color="#FF00FF"> (</font></strong> x1<strong><font color="#FF00FF">,</font></strong>y2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF">,</font></strong>z2<strong><font color="#FF00FF">+</font></strong><font color="#CC3300">1</font><strong><font color="#FF00FF"> );</font></strong><strong><font color="#0000FF"> 
                            break</font></strong><strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">
                       case</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">:</font></strong>
                            scan_d<strong><font color="#FF00FF"> (</font></strong> x1<strong><font color="#FF00FF"> );</font></strong> scan_d<strong><font color="#FF00FF"> (</font></strong> y1<strong><font color="#FF00FF"> );</font></strong> scan_d<strong><font color="#FF00FF"> (</font></strong> z1<strong><font color="#FF00FF"> );</font></strong>
                            printf<strong><font color="#FF00FF"> (</font></strong><font color="green"> "%d\n"</font><strong><font color="#FF00FF">,</font></strong> query<strong><font color="#FF00FF"> (</font></strong> x1<strong><font color="#FF00FF">,</font></strong>y1<strong><font color="#FF00FF">,</font></strong>z1<strong><font color="#FF00FF"> ) );      
                }
          }           
    }</font></strong><strong><font color="#0000FF">
    return</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">;
}</font></strong>
</pre><div><strong><font color="#FF00FF"><br></font></strong></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">暴力代码 :<br></p><div style="padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; "><pre style="font-family: 'Courier New'; text-align: left; "><font color="green">/*
Mail to   : miyubai@gamil.com
My Blog   : www.baiyun.me
Link      : http://www.cnblogs.com/MiYu  || http://www.cppblog.com/MiYu
Author By : MiYu
Test      : 1
Complier  : g++ mingw32-3.4.2
Program   :
Doc Name  :
*/</font><font color="green">
//#pragma warning( disable:4789 )
</font><font color="blue">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
#include &lt;string&gt;
#include &lt;set&gt;
#include &lt;map&gt;
#include &lt;utility&gt;
#include &lt;queue&gt;
#include &lt;stack&gt;
#include &lt;list&gt;
#include &lt;vector&gt;
#include &lt;cstdio&gt;
#include &lt;cstdlib&gt;
#include &lt;cstring&gt;
#include &lt;cmath&gt;
#include &lt;ctime&gt;
</font><strong><font color="#0000FF">using namespace</font></strong> std<strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">
struct</font></strong> node<strong><font color="#FF00FF"> {</font></strong><strong><font color="blue">  
    int</font></strong> x1<strong><font color="#FF00FF">,</font></strong> x2<strong><font color="#FF00FF">,</font></strong> y1<strong><font color="#FF00FF">,</font></strong> y2<strong><font color="#FF00FF">,</font></strong> z1<strong><font color="#FF00FF">,</font></strong> z2<strong><font color="#FF00FF">; 
}</font></strong>cube<strong><font color="#FF00FF">[</font></strong><font color="#CC3300">10010</font><strong><font color="#FF00FF">];</font></strong><strong><font color="blue">   
  
int</font></strong><strong><font color="#0000FF"> main</font></strong><strong><font color="#FF00FF">()  
{</font></strong><strong><font color="blue">  
    int</font></strong> N<strong><font color="#FF00FF">,</font></strong> M<strong><font color="#FF00FF">;</font></strong><strong><font color="blue">  
    int</font></strong> x<strong><font color="#FF00FF">,</font></strong> y<strong><font color="#FF00FF">,</font></strong> z<strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">
    while</font></strong><strong><font color="#FF00FF"> (</font></strong> scanf<strong><font color="#FF00FF"> (</font></strong><font color="green"> "%d%d"</font><strong><font color="#FF00FF">,&amp;</font></strong>N<strong><font color="#FF00FF">,&amp;</font></strong>M<strong><font color="#FF00FF"> ) !=</font></strong> EOF<strong><font color="#FF00FF"> )  {</font></strong><strong><font color="blue">  
        int</font></strong> cnt<strong><font color="#FF00FF"> =</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">  
        while</font></strong><strong><font color="#FF00FF"> (</font></strong> M<strong><font color="#FF00FF"> -- )  
        {</font></strong><strong><font color="blue">  
            int</font></strong> ask<strong><font color="#FF00FF">;</font></strong>  
            scanf<strong><font color="#FF00FF"> (</font></strong><font color="green"> "%d"</font><strong><font color="#FF00FF">, &amp;</font></strong>ask<strong><font color="#FF00FF"> );</font></strong><strong><font color="#0000FF">  
            switch</font></strong><strong><font color="#FF00FF"> (</font></strong> ask<strong><font color="#FF00FF"> ) {</font></strong><strong><font color="#0000FF">
                case</font></strong><font color="#CC3300"> 1</font><strong><font color="#FF00FF">:</font></strong>   
                    scanf<strong><font color="#FF00FF"> (</font></strong><font color="green"> "%d%d%d%d%d%d"</font><strong><font color="#FF00FF">, &amp;</font></strong>cube<strong><font color="#FF00FF">[</font></strong>cnt<strong><font color="#FF00FF">].</font></strong>x1<strong><font color="#FF00FF">,&amp;</font></strong>cube<strong><font color="#FF00FF">[</font></strong>cnt<strong><font color="#FF00FF">].</font></strong>y1<strong><font color="#FF00FF">,
                                            &amp;</font></strong>cube<strong><font color="#FF00FF">[</font></strong>cnt<strong><font color="#FF00FF">].</font></strong>z1<strong><font color="#FF00FF">,&amp;</font></strong>cube<strong><font color="#FF00FF">[</font></strong>cnt<strong><font color="#FF00FF">].</font></strong>x2<strong><font color="#FF00FF">,
                                            &amp;</font></strong>cube<strong><font color="#FF00FF">[</font></strong>cnt<strong><font color="#FF00FF">].</font></strong>y2<strong><font color="#FF00FF">,&amp;</font></strong>cube<strong><font color="#FF00FF">[</font></strong>cnt<strong><font color="#FF00FF">].</font></strong>z2<strong><font color="#FF00FF"> );  
                    ++</font></strong> cnt<strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">  
                    break</font></strong><strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">
                case</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">:</font></strong>
                    scanf<strong><font color="#FF00FF"> (</font></strong><font color="green"> "%d%d%d"</font><strong><font color="#FF00FF">, &amp;</font></strong>x<strong><font color="#FF00FF">, &amp;</font></strong>y<strong><font color="#FF00FF">, &amp;</font></strong>z<strong><font color="#FF00FF">);</font></strong><strong><font color="blue">  
                    int</font></strong> count<strong><font color="#FF00FF"> =</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">;</font></strong><strong><font color="#0000FF">  
                    for</font></strong><strong><font color="#FF00FF"> (</font></strong><strong><font color="blue"> int</font></strong> i<strong><font color="#FF00FF"> =</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">;</font></strong> i<strong><font color="#FF00FF"> !=</font></strong> cnt<strong><font color="#FF00FF">; ++</font></strong> i<strong><font color="#FF00FF"> )</font></strong><strong><font color="#0000FF">  
                        if</font></strong><strong><font color="#FF00FF"> (</font></strong> cube<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">].</font></strong>x1<strong><font color="#FF00FF"> &lt;=</font></strong> x<strong><font color="#FF00FF"> &amp;&amp;</font></strong> x<strong><font color="#FF00FF"> &lt;=</font></strong> cube<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">].</font></strong>x2<strong><font color="#FF00FF"> &amp;&amp;</font></strong> 
                             cube<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">].</font></strong>y1<strong><font color="#FF00FF"> &lt;=</font></strong> y<strong><font color="#FF00FF"> &amp;&amp;</font></strong> y<strong><font color="#FF00FF"> &lt;=</font></strong> cube<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">].</font></strong>y2<strong><font color="#FF00FF"> &amp;&amp;</font></strong> 
                             cube<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">].</font></strong>z1<strong><font color="#FF00FF"> &lt;=</font></strong> z<strong><font color="#FF00FF"> &amp;&amp;</font></strong> z<strong><font color="#FF00FF"> &lt;=</font></strong> cube<strong><font color="#FF00FF">[</font></strong>i<strong><font color="#FF00FF">].</font></strong>z2<strong><font color="#FF00FF"> )</font></strong>  
                                    count<strong><font color="#FF00FF"> ^=</font></strong><font color="#CC3300"> 1</font><strong><font color="#FF00FF">;;</font></strong>  
                    puts<strong><font color="#FF00FF"> (</font></strong> count<strong><font color="#FF00FF"> ?</font></strong><font color="green"> "1"</font><strong><font color="#FF00FF"> :</font></strong><font color="green"> "0"</font><strong><font color="#FF00FF"> ); 
            }  
        }  
    }</font></strong><strong><font color="#0000FF">  
    return</font></strong><font color="#CC3300"> 0</font><strong><font color="#FF00FF">;  
}</font></strong>  
</pre></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></div></span></span><img src ="http://www.cppblog.com/MiYu/aggbug/133577.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-11-14 10:35 <a href="http://www.cppblog.com/MiYu/archive/2010/11/14/133577.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Splay Tree 介绍</title><link>http://www.cppblog.com/MiYu/archive/2010/11/12/133404.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 12 Nov 2010 03:13:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/11/12/133404.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/133404.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/11/12/133404.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/133404.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/133404.html</trackback:ping><description><![CDATA[<span  style="font-size: 12px; "><div style="padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; "><pre style="font-family: 'Courier New'; text-align: left; "><span  style="font-family: verdana, 'courier new'; white-space: normal; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://baiyun.me/"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</span></pre><pre style="font-family: 'Courier New'; text-align: left; "><span  style="font-family: verdana, 'courier new'; white-space: normal; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span></pre><pre style="font-family: 'Courier New'; text-align: left; ">伸展树（Splay Tree）是一种二叉排序树，它能在O<strong><font color="#FF00FF">(</font></strong>log n<strong><font color="#FF00FF">)</font></strong>内完成插入、查找和删除操作。它由Daniel Sleator和Robert Tarjan创造。它的优势在于不需要记录用于平衡树的冗余信息。在伸展树上的一般操作都基于伸展操作。
查找树的相关知识
　　各种查找树存在不足。比如：对于一个有n个节点的平衡树，虽然最坏情况下每次查找的时间复杂度不会超过O<strong><font color="#FF00FF">(</font></strong>logn<strong><font color="#FF00FF">)</font></strong>，但是如果访问模式不均匀，平衡树的效率就会受到影响。此外，它们还需要额外的空间来存储平衡信息。 
　　这些查找树的设计目标都是减少最坏情况下单次操作时间，但是查找树的典型应用经常需要执行一系列的查找操作，此时更关心的性能指标是所有这些操作总共需要多少时间。对于此类应用，更好的目标就是降低操作的摊平时间，此处的摊平时间是指在一系列最坏情况的操作序列中单次操作的平均时间。获得摊平效率的一种方法 就是使用&#8220;自调整&#8221;的数据结构。 
　　和平衡的或是其它对结构有明确限制的数据结构比起来，自调整数据结构有以下几个优点：<font color="#CC3300"> 
　　1</font>、从摊平角度而言，它们忽略常量因子，因此绝对不会比有明确限制的数据结构差。而且由于它们可以根据使用情况进行调整，于是在使用模式不均匀的情况下更加有效。<font color="#CC3300"> 
　　2</font>、由于无需存储平衡或者其它的限制信息，它们所需的空间更小。<font color="#CC3300"> 
　　3</font>、它们的查找和更新算法概念简单，易于实现。 
　　当然，自调整结构也有潜在的缺点：<font color="#CC3300"> 
　　1</font>、它们需要更多的局部调整，尤其是在查找期间。（那些有明确限制的数据结构仅需在更新期间进行调整，查找期间则不用）<font color="#CC3300"> 
　　2</font>、一系列查找操作中的某一个可能会耗时较长，这在实时应用程序中可能是个不足之处。
伸展树存在的意义
　　假设想要对一个二叉查找树执行一系列的查找操作。为了使整个查找时间更小，被查频率高的那些条目就应当经常处于靠近树根的位置。于是想到设计一个简单方法， 在每次查找之后对树进行重构，把被查找的条目搬移到离树根近一些的地方。splay tree应运而生。splay tree是一种自调整形式的二叉查找树，它会沿着从某个节点到树根之间的路径，通过一系列的旋转把这个节点搬移到树根去。
已知重构方法与伸展树的重构方法
　　先前，已经存在两种重构方法：<font color="#CC3300"> 
　　1</font>、单旋：在查找完位于节点x中的条目i之后，旋转链接x和其父节点的边。（除非x就是树根）<font color="#CC3300"> 
　　2</font>、搬移至树根：在查找完位于节点x中的条目i之后，旋转链接x和其父节点的边，然后重复这个操作直至x成为树根。 
　　splay tree的重构方法和搬移至树根的方法相似，它也会沿着查找路径做自底向上的旋转，将被查找条目移至树根。但不同的是，它的旋转是成对进行的，顺序取决于查找路径的结构。为了在节点x处对树进行splay操作，我们需要重复下面的步骤，直至x成为树根为止：<font color="#CC3300"> 
　　1</font>、第一种情况：如果x的父节点p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>是树根，则旋转连接x和p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>的边。（这种情况是最后一步）<font color="#CC3300"> 
　　2</font>、第二种情况：如果p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>不是树根，而且x和p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>本身都是左孩子或者都是右孩子，则先旋转连接p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>和x的祖父节点g<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>的边，然后再旋转连接x和p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>的边。<font color="#CC3300"> 
　　3</font>、第三种情况：如果p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>不是树根，而且x是左孩子，p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>是右孩子，或者相反，则先旋转连接x和p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>的边，再旋转连接x和新的p<strong><font color="#FF00FF">(</font></strong>x<strong><font color="#FF00FF">)</font></strong>的边。 
　　在节点x处进行splay操作的时间是和查找x所需的时间成比例的。splay操作不单是把x搬移到了树根，而且还把查找路径上的每个节点的深度都大致减掉了一半。
伸展树（Splay Tree）支持的操作
　　具体操作包括：<font color="#CC3300"> 
　　1</font>、access<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">):</font></strong>如果i在树t中，则返回指向它的指针，否则返回空指针。为了实现access<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，可以从树t的根部向下查找i。如果 查找操作遇到了一个含有i的节点x，就在x处进行splay操作，并返回指向x的指针，访问结束。如果遇到了空指针，表示i不在树中，此时就在最后一个非 空节点处进行splay操作，然后返回空指针。如果树是空的，将忽略掉splay操作。<font color="#CC3300"> 
　　2</font>、insert<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">):</font></strong>将条目i插入树t中（假设其尚不存在）。为了实现insert<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，首先执行split<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，然后把t换成一个由新的包含有i的根节点组成的树，这个根节点的左右子树分别是split返回的树t1和t2。<font color="#CC3300"> 
　　3</font>、<strong><font color="#0000FF">delete</font></strong><strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">):</font></strong>从树t中删除条目i（假设其已经存在）。为了实现<strong><font color="#0000FF">delete</font></strong><strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，首先执行access<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，然后把t换成其左子树和右子树join之后的新树。<font color="#CC3300"> 
　　4</font>、join<strong><font color="#FF00FF">(</font></strong>t1<strong><font color="#FF00FF">,</font></strong>t2<strong><font color="#FF00FF">):</font></strong>将树t1和t2合并成一棵树，其中包含之前两棵树的所有条目，并返回合并之后的树。这个操作假设t1中的所有条目都小于t2 中的条目，操作完成之后会销毁t1和t2。为了实现join<strong><font color="#FF00FF">(</font></strong>t1<strong><font color="#FF00FF">,</font></strong>t2<strong><font color="#FF00FF">)</font></strong>，首先访问t1中最大的条目i。访问结束之后，t1的根节点中包含的就是i，它 的右孩子显然为空。于是把t2作为这个根节点的右子树并返回完成之后的新树即可实现join操作。<font color="#CC3300"> 
　　5</font>、split<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">):</font></strong>构建并返回两棵树t1和t2，其中t1包含t中所有小于等于i的条目，t2包含t中所有大于i的条目。操作完成之后销毁t。为 了实现split<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，首先执行access<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong>t<strong><font color="#FF00FF">)</font></strong>，然后根据新根节点中的值是大于还是小于等于i来切断这个根节点的左链接或右链接，并返回形 成的两棵树。 
　　另外insert和<strong><font color="#0000FF">delete</font></strong>方法有更好的实现，时间复杂度更小：<font color="#CC3300"> 
　　1</font>、insert<strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong> t<strong><font color="#FF00FF">):</font></strong>查找i，把遇到的空指针替换成一个含有i的新节点，然后再在新节点处对树进行splay操作。<font color="#CC3300"> 
　　2</font>、<strong><font color="#0000FF">delete</font></strong><strong><font color="#FF00FF">(</font></strong>i<strong><font color="#FF00FF">,</font></strong> t<strong><font color="#FF00FF">):</font></strong>查找含有i的节点，设此节点为x，其父节点为y。把x的左右子树合并之后替换掉x，然后再从y处进行splay操作。
伸展树的优势
　　由于Splay Tree仅仅是不断调整，并没有引入额外的标记，因而树结构与标准BST没有任何不同，从空间角度来看，它比Treap、Red<strong><font color="#FF00FF">-</font></strong>Black Tree、AVL要高效得多。因为结构不变，因此只要是通过左旋和右旋进行的操作对Splay Tree性质都没有丝毫影响，因而它也提供了BST中最丰富的功能，包括快速的拆分和合并（这里指的是将原树拆分成两棵子树，其中一棵子树所有节点都比另一子树小，以及它的逆过程），并且实现极为便捷。这一点是其它结构较难实现的。其时间效率也相当稳定，和Treap基本相当</pre></div></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/133404.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-11-12 11:13 <a href="http://www.cppblog.com/MiYu/archive/2010/11/12/133404.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)伸展树 ( Splay tree )</title><link>http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 12 Nov 2010 03:13:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/133405.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/133405.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/133405.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;伸展树(Splay Tree)是AVL树不错的替代，它有以下几个特点：(1)它是二叉查找树的改进，所以具有二叉查找树的有序性。(2)对伸展树的操作的平摊复杂度是O(log2n)。(3)伸展树的空间要求、编程难度非常低。提到伸展树，就不得不提到AVL树和R...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/133405.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-11-12 11:13 <a href="http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU 2689 HDOJ 2689 Sort it ACM 2689 IN HDU </title><link>http://www.cppblog.com/MiYu/archive/2010/10/27/131506.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 27 Oct 2010 07:23:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/27/131506.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/131506.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/27/131506.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/131506.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/131506.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://baiyun.me/"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目地址 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://acm.hdu.edu.cn/showproblem.php?pid=2689">&nbsp;http://acm.hdu.edu.cn/showproblem.php?pid=2689&nbsp;</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目描述:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; 其实就是求 冒泡排序时 的交换次数, &nbsp;当然也可以求逆序数来解决问题, 下面是2份 代码:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_088c1207-98d6-4d06-b962-93bbb10457cb" style="vertical-align: middle; padding-right: 5px; "><span class="cnblogs_code_collapse" style="border-right-color: rgb(128, 128, 128); border-right-width: 1px; border-right-style: solid; border-top-color: rgb(128, 128, 128); border-top-width: 1px; border-top-style: solid; border-left-color: rgb(128, 128, 128); border-left-width: 1px; border-left-style: solid; border-bottom-color: rgb(128, 128, 128); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(255, 255, 255); padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px; font-family: 'Courier New'; ">代码</span><div id="cnblogs_code_open_088c1207-98d6-4d06-b962-93bbb10457cb"><div><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">直接冒泡排序求交换的次数</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br>/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br>Mail&nbsp;to&nbsp;&nbsp;&nbsp;:&nbsp;miyubai@gamil.com<br>My&nbsp;Blog&nbsp;&nbsp;&nbsp;:&nbsp;www.baiyun.me<br>Link&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); text-decoration: underline; ">http://www.cnblogs.com/MiYu</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;&nbsp;||&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); text-decoration: underline; ">http://www.cppblog.com/MiYu</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br>Author&nbsp;By&nbsp;:&nbsp;MiYu<br>Test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;1<br>Complier&nbsp;&nbsp;:&nbsp;g++&nbsp;mingw32-3.4.2<br>Program&nbsp;&nbsp;&nbsp;:&nbsp;HDU_2689<br>Doc&nbsp;Name&nbsp;&nbsp;:&nbsp;Sort&nbsp;it<br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">#pragma&nbsp;warning(&nbsp;disable:4789&nbsp;)</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">iostream</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">fstream</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">sstream</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">algorithm</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">string</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">set</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">map</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">utility</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">queue</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">stack</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">list</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">vector</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">cstdio</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">cstdlib</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">cstring</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">cmath</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">ctime</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">using</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">namespace</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;std;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N,&nbsp;num[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1010</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];<br>inline&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;swap&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">a,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">b&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;b&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;a&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;b;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;bouble&nbsp;()&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;j&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;j&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;j&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;num[j</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;num[j]&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swap&nbsp;(&nbsp;num[j</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">],&nbsp;num[j]&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum;<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;print&nbsp;()&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;num[i]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;main&nbsp;()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;scanf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%d</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">N&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">==</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%d</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;num&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%d\n</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,bouble&nbsp;()&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;print&nbsp;();</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>}<br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">树状数组求逆序数法</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br>/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br>Mail&nbsp;to&nbsp;&nbsp;&nbsp;:&nbsp;miyubai@gamil.com<br>My&nbsp;Blog&nbsp;&nbsp;&nbsp;:&nbsp;www.baiyun.me<br>Link&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); text-decoration: underline; ">http://www.cnblogs.com/MiYu</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;&nbsp;||&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); text-decoration: underline; ">http://www.cppblog.com/MiYu</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br>Author&nbsp;By&nbsp;:&nbsp;MiYu<br>Test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;1<br>Complier&nbsp;&nbsp;:&nbsp;g++&nbsp;mingw32-3.4.2<br>Program&nbsp;&nbsp;&nbsp;:&nbsp;HDU_2689<br>Doc&nbsp;Name&nbsp;&nbsp;:&nbsp;Sort&nbsp;it<br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">#pragma&nbsp;warning(&nbsp;disable:4789&nbsp;)</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">iostream</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">fstream</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">sstream</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">algorithm</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">string</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">set</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">map</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">utility</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">queue</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">stack</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">list</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">vector</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">cstdio</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">cstdlib</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">cstring</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">cmath</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">ctime</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">using</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">namespace</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;std;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N,val,num[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1010</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">],low[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1010</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;init&nbsp;()&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1010</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;low[i]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">i&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;modify&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;x&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;num[x];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;low[x];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;query&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;x&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;num[x];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;low[x];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum;<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;main&nbsp;()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;init&nbsp;();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;scanf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%d</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">N&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">==</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset&nbsp;(&nbsp;num,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">sizeof</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;num&nbsp;)&nbsp;);&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%d</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">val&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;modify&nbsp;(&nbsp;val&nbsp;);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;query&nbsp;(&nbsp;val&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;);&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%d\n</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;sum&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>}</span></div></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/131506.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-27 15:23 <a href="http://www.cppblog.com/MiYu/archive/2010/10/27/131506.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU 1512 HDOJ 1512 Monkey King ( 左偏树 ) ACM 1512 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/10/24/131043.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sun, 24 Oct 2010 03:43:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/24/131043.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/131043.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/24/131043.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/131043.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/131043.html</trackback:ping><description><![CDATA[<p>MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://baiyun.me/"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p>&nbsp;</p><p>题目地址:</p><p><a href="http://acm.hdu.edu.cn/showproblem.php?pid=1512">http://acm.hdu.edu.cn/showproblem.php?pid=1512</a></p><p>题目描述 :</p><div class="cnblogs_code" onclick="cnblogs_code_show('93ce2b86-12de-4b7c-b5b1-c014e2356326')"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" class="code_img_closed" id="code_img_closed_93ce2b86-12de-4b7c-b5b1-c014e2356326" alt=""><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_93ce2b86-12de-4b7c-b5b1-c014e2356326" onclick="cnblogs_code_hide('93ce2b86-12de-4b7c-b5b1-c014e2356326',event)" style="display:none"><span class="cnblogs_code_collapse">代码</span><div id="cnblogs_code_open_93ce2b86-12de-4b7c-b5b1-c014e2356326" class="cnblogs_code_hide"><div><!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><span style="color: #000000;">Monkey&nbsp;King<br><br>Time&nbsp;Limit:&nbsp;</span><span style="color: #800080;">10000</span><span style="color: #000000;">/</span><span style="color: #800080;">5000</span><span style="color: #000000;">&nbsp;MS&nbsp;(Java</span><span style="color: #000000;">/</span><span style="color: #000000;">Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory&nbsp;Limit:&nbsp;</span><span style="color: #800080;">65536</span><span style="color: #000000;">/</span><span style="color: #800080;">32768</span><span style="color: #000000;">&nbsp;K&nbsp;(Java</span><span style="color: #000000;">/</span><span style="color: #000000;">Others)<br>Total&nbsp;Submission(s):&nbsp;</span><span style="color: #800080;">914</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;Accepted&nbsp;Submission(s):&nbsp;</span><span style="color: #800080;">426</span><span style="color: #000000;"><br><br><br>Problem&nbsp;Description<br>Once&nbsp;</span><span style="color: #0000FF;">in</span><span style="color: #000000;">&nbsp;a&nbsp;forest,&nbsp;there&nbsp;lived&nbsp;N&nbsp;aggressive&nbsp;monkeys.&nbsp;At&nbsp;the&nbsp;beginning,&nbsp;they&nbsp;each&nbsp;does&nbsp;things&nbsp;</span><span style="color: #0000FF;">in</span><span style="color: #000000;">&nbsp;its&nbsp;own&nbsp;way&nbsp;and&nbsp;none&nbsp;of&nbsp;them&nbsp;knows&nbsp;each&nbsp;other.&nbsp;But&nbsp;monkeys&nbsp;can</span><span style="color: #800000;">'</span><span style="color: #800000;">t&nbsp;avoid&nbsp;quarrelling,&nbsp;and&nbsp;it&nbsp;only&nbsp;happens&nbsp;between&nbsp;two&nbsp;monkeys&nbsp;who&nbsp;does&nbsp;not&nbsp;know&nbsp;each&nbsp;other.&nbsp;And&nbsp;when&nbsp;it&nbsp;happens,&nbsp;both&nbsp;the&nbsp;two&nbsp;monkeys&nbsp;will&nbsp;invite&nbsp;the&nbsp;strongest&nbsp;friend&nbsp;of&nbsp;them,&nbsp;and&nbsp;duel.&nbsp;Of&nbsp;course,&nbsp;after&nbsp;the&nbsp;duel,&nbsp;the&nbsp;two&nbsp;monkeys&nbsp;and&nbsp;all&nbsp;of&nbsp;there&nbsp;friends&nbsp;knows&nbsp;each&nbsp;other,&nbsp;and&nbsp;the&nbsp;quarrel&nbsp;above&nbsp;will&nbsp;no&nbsp;longer&nbsp;happens&nbsp;between&nbsp;these&nbsp;monkeys&nbsp;even&nbsp;if&nbsp;they&nbsp;have&nbsp;ever&nbsp;conflicted.</span><span style="color: #800000;"><br></span><span style="color: #000000;"><br>Assume&nbsp;that&nbsp;every&nbsp;money&nbsp;has&nbsp;a&nbsp;strongness&nbsp;value,&nbsp;which&nbsp;will&nbsp;be&nbsp;reduced&nbsp;to&nbsp;only&nbsp;half&nbsp;of&nbsp;the&nbsp;original&nbsp;after&nbsp;a&nbsp;duel(that&nbsp;</span><span style="color: #0000FF;">is</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800080;">10</span><span style="color: #000000;">&nbsp;will&nbsp;be&nbsp;reduced&nbsp;to&nbsp;</span><span style="color: #800080;">5</span><span style="color: #000000;">&nbsp;and&nbsp;</span><span style="color: #800080;">5</span><span style="color: #000000;">&nbsp;will&nbsp;be&nbsp;reduced&nbsp;to&nbsp;</span><span style="color: #800080;">2</span><span style="color: #000000;">).<br><br>And&nbsp;we&nbsp;also&nbsp;assume&nbsp;that&nbsp;every&nbsp;monkey&nbsp;knows&nbsp;himself.&nbsp;That&nbsp;</span><span style="color: #0000FF;">is</span><span style="color: #000000;">,&nbsp;when&nbsp;he&nbsp;</span><span style="color: #0000FF;">is</span><span style="color: #000000;">&nbsp;the&nbsp;strongest&nbsp;one&nbsp;</span><span style="color: #0000FF;">in</span><span style="color: #000000;">&nbsp;all&nbsp;of&nbsp;his&nbsp;friends,&nbsp;he&nbsp;himself&nbsp;will&nbsp;go&nbsp;to&nbsp;duel.<br>&nbsp;<br><br>Input<br>There&nbsp;are&nbsp;several&nbsp;test&nbsp;cases,&nbsp;and&nbsp;each&nbsp;</span><span style="color: #0000FF;">case</span><span style="color: #000000;">&nbsp;consists&nbsp;of&nbsp;two&nbsp;parts.<br><br>First&nbsp;part:&nbsp;The&nbsp;first&nbsp;line&nbsp;contains&nbsp;an&nbsp;integer&nbsp;N(N</span><span style="color: #000000;">&lt;=</span><span style="color: #800080;">100</span><span style="color: #000000;">,</span><span style="color: #800080;">000</span><span style="color: #000000;">),&nbsp;which&nbsp;indicates&nbsp;the&nbsp;number&nbsp;of&nbsp;monkeys.&nbsp;And&nbsp;then&nbsp;N&nbsp;lines&nbsp;follows.&nbsp;There&nbsp;</span><span style="color: #0000FF;">is</span><span style="color: #000000;">&nbsp;one&nbsp;number&nbsp;on&nbsp;each&nbsp;line,&nbsp;indicating&nbsp;the&nbsp;strongness&nbsp;value&nbsp;of&nbsp;ith&nbsp;monkey(</span><span style="color: #000000;">&lt;=</span><span style="color: #800080;">32768</span><span style="color: #000000;">).<br><br>Second&nbsp;part:&nbsp;The&nbsp;first&nbsp;line&nbsp;contains&nbsp;an&nbsp;integer&nbsp;M(M</span><span style="color: #000000;">&lt;=</span><span style="color: #800080;">100</span><span style="color: #000000;">,</span><span style="color: #800080;">000</span><span style="color: #000000;">),&nbsp;which&nbsp;indicates&nbsp;there&nbsp;are&nbsp;M&nbsp;conflicts&nbsp;happened.&nbsp;And&nbsp;then&nbsp;M&nbsp;lines&nbsp;follows,&nbsp;each&nbsp;line&nbsp;of&nbsp;which&nbsp;contains&nbsp;two&nbsp;integers&nbsp;x&nbsp;and&nbsp;y,&nbsp;indicating&nbsp;that&nbsp;there&nbsp;</span><span style="color: #0000FF;">is</span><span style="color: #000000;">&nbsp;a&nbsp;conflict&nbsp;between&nbsp;the&nbsp;Xth&nbsp;monkey&nbsp;and&nbsp;Yth.<br><br>&nbsp;<br><br>Output<br>For&nbsp;each&nbsp;of&nbsp;the&nbsp;conflict,&nbsp;output&nbsp;</span><span style="color: #000000;">-</span><span style="color: #800080;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000FF;">if</span><span style="color: #000000;">&nbsp;the&nbsp;two&nbsp;monkeys&nbsp;know&nbsp;each&nbsp;other,&nbsp;otherwise&nbsp;output&nbsp;the&nbsp;strongness&nbsp;value&nbsp;of&nbsp;the&nbsp;strongest&nbsp;monkey&nbsp;</span><span style="color: #0000FF;">in</span><span style="color: #000000;">&nbsp;all&nbsp;friends&nbsp;of&nbsp;them&nbsp;after&nbsp;the&nbsp;duel.<br>&nbsp;<br><br>Sample&nbsp;Input<br></span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #800080;">20</span><span style="color: #000000;"><br></span><span style="color: #800080;">16</span><span style="color: #000000;"><br></span><span style="color: #800080;">10</span><span style="color: #000000;"><br></span><span style="color: #800080;">10</span><span style="color: #000000;"><br></span><span style="color: #800080;">4</span><span style="color: #000000;"><br></span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #800080;">2</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">3</span><span style="color: #000000;"><br></span><span style="color: #800080;">3</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">4</span><span style="color: #000000;"><br></span><span style="color: #800080;">3</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #800080;">4</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #800080;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">5</span><span style="color: #000000;"><br>&nbsp;<br><br>Sample&nbsp;Output<br></span><span style="color: #800080;">8</span><span style="color: #000000;"><br></span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #800080;">5</span><span style="color: #000000;"><br></span><span style="color: #000000;">-</span><span style="color: #800080;">1</span><span style="color: #000000;"><br></span><span style="color: #800080;">10</span><span style="color: #000000;"><br>&nbsp;<br></span></div></div></div><p>&nbsp;</p><p>&nbsp;</p><p>题目分析:</p><div class="cnblogs_code"><div><font  color="#800080"><div><div>/*</div><div>Mail to &nbsp; : miyubai@gamil.com</div><div>My Blog &nbsp; : www.baiyun.me</div><div>Link &nbsp; &nbsp; &nbsp;: http://www.cnblogs.com/MiYu &nbsp;|| http://www.cppblog.com/MiYu</div><div>Author By : MiYu</div><div>Test &nbsp; &nbsp; &nbsp;: 1</div><div>Complier &nbsp;: g++ mingw32-3.4.2</div><div>Program &nbsp; : HDU_1512</div><div>Doc Name &nbsp;: Monkey King</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>题目意思:&nbsp;</div><div><br></div><div>有N只猴子, 每只都有一个力量值. 开始的时候互不认识, 它们之间会发生M次斗争. 每次发生a, b的斗争时, a, b都会从各自的朋友圈里拉出一个最强的, 之后两只猴子打, 打完后这两只猴子的力量值各减半. 并且打完后, 两只猴子的朋友圈的所有人都互相认识(也就是不会再打).</div><div><br></div><div>你的任务就是对于每个斗争, 若a, b是朋友, 那么输出-1, 否则输出打完后它们的朋友圈的最强猴子的力量值.</div><div><br></div><div>&nbsp;使用 普通 优先队列的话 估计会超时, 因为数据量很大 100000 ! !, 等下有空试试看.&nbsp;</div><div><br></div><div>对于每一个节点, 定义dis 表示X节点到最右边的空节点的距离的最小值</div><div><br></div><div>对于每个节点X, 要求X的左儿子的dis &gt;= 右儿子的dis, 那么容易发现, 对于N个节点的左偏树, 其右儿子最多只有logN个节点.</div><div><br></div><div>合并操作就是让复杂度落在右儿子上, 从而达到logN的合并复杂度.</div><div><br></div><div>首先对于两个堆, 若其中一个为空, 返回另一个.</div><div><br></div><div>否则(这里以大根堆为例), a指向堆顶较大的堆, b指向另一个. 让a的右儿子和b合并, 合并后的子树作为a的右儿子.</div><div><br></div><div>接下来, 检查a的两个儿子是否满足dis, 不满足就交换两个儿子.</div><div><br></div><div>最后, 更新a的dis.</div><div><br></div><div>这样就容易实现堆的其他操作 ( 比如插入, 删除顶等 ).</div><div><br></div><div>另外 还需要用到 并查集. &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>*/</div><div>//#pragma warning( disable:4789 )</div><div>#include &lt;iostream&gt;</div><div>#include &lt;fstream&gt;</div><div>#include &lt;sstream&gt;</div><div>#include &lt;algorithm&gt;</div><div>#include &lt;string&gt;</div><div>#include &lt;set&gt;</div><div>#include &lt;map&gt;</div><div>#include &lt;utility&gt;</div><div>#include &lt;queue&gt;</div><div>#include &lt;stack&gt;</div><div>#include &lt;list&gt;</div><div>#include &lt;vector&gt;</div><div>#include &lt;cstdio&gt;</div><div>#include &lt;cstdlib&gt;</div><div>#include &lt;cstring&gt;</div><div>#include &lt;cmath&gt;</div><div>#include &lt;ctime&gt;</div><div>using namespace std;</div><div>const int MM = 100010;</div><div>struct left {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;int l,r,dis,val,dad;</div><div>} heap[MM];</div><div><br></div><div>int N, M;</div><div><br></div><div>inline int max ( const int &amp;a, const int &amp;b) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; return a &gt; b ? a : b;</div><div>}</div><div><br></div><div>inline int find ( int &amp;x ) {</div><div>&nbsp;&nbsp; &nbsp;return heap[x].dad == x ? x : heap[x].dad = find ( heap[x].dad );</div><div>}</div><div><br></div><div>inline void swap(int &amp;a, int &amp;b) {</div><div>&nbsp;&nbsp; &nbsp; a ^= b ^= a ^= b;</div><div>}</div><div><br></div><div>inline int merge ( int x, int y ) {</div><div>&nbsp;&nbsp; &nbsp;if ( x == 0 ) return y;</div><div>&nbsp;&nbsp; &nbsp;if ( y == 0 ) return x;</div><div>&nbsp;&nbsp; &nbsp;if ( heap[y].val &gt; heap[x].val ) swap ( x, y ); &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;heap[x].r = merge ( heap[x].r, y );</div><div>&nbsp;&nbsp; &nbsp;heap[heap[x].r].dad = x;</div><div>&nbsp;&nbsp; &nbsp;if ( heap[ heap[x].l ].dis &lt; heap[ heap[x].r ].dis )&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; swap ( heap[x].l, heap[x].r );</div><div>&nbsp;&nbsp; &nbsp;if ( heap[x].r == 0 ) heap[x].dis = 0;</div><div>&nbsp;&nbsp; &nbsp;else heap[x].dis = heap[ heap[x].r ].dis + 1;</div><div>&nbsp;&nbsp; &nbsp;return x;</div><div>}</div><div><br></div><div>inline int push ( int x, int y ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; return merge ( x, y ); &nbsp; &nbsp; &nbsp;&nbsp;</div><div>}</div><div><br></div><div>inline int pop ( int &amp;x ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; int l = heap[x].l;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; int r = heap[x].r;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; heap[l].dad = l;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; heap[r].dad = r;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; heap[x].l = heap[x].r = heap[x].dis = 0; &nbsp;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; return merge ( l, r ); &nbsp;</div><div>}</div><div><br></div><div>inline bool scan_d(int &amp;num) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;char in;bool IsN=false;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;in=getchar();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in==EOF) return false;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in!='-'&amp;&amp;(in&lt;'0'||in&gt;'9')) in=getchar();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in=='-'){ IsN=true;num=0;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;else num=in-'0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in=getchar(),in&gt;='0'&amp;&amp;in&lt;='9'){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num*=10,num+=in-'0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(IsN) num=-num;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return true;</div><div>}</div><div><br></div><div>int main() {</div><div>&nbsp;&nbsp; &nbsp;while ( scan_d ( N ) ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 1; i &lt;= N; ++ i ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scan_d ( heap[i].val );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;heap[i].l = heap[i].r = heap[i].dis = 0;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;heap[i].dad = i; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; scan_d ( M );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int a, b, x, y;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; while ( M -- ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scan_d (a); scan_d (b);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = find ( a );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = find ( b );&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( x == y ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;puts ( "-1" ); &nbsp; &nbsp;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;heap[x].val /= 2;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int xx = push ( pop ( x ), x ); &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;heap[y].val /= 2;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int yy = push ( pop ( y ), y ); &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf ( "%d\n", heap[ merge ( xx, yy ) ].val ); &nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;return 0;</div><div>}</div></div><div><br></div><div><br></div></font></div></div><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><img src ="http://www.cppblog.com/MiYu/aggbug/131043.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-24 11:43 <a href="http://www.cppblog.com/MiYu/archive/2010/10/24/131043.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>左偏树详解 ( 转载 )</title><link>http://www.cppblog.com/MiYu/archive/2010/10/24/131040.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sun, 24 Oct 2010 02:53:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/24/131040.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/131040.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/24/131040.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/131040.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/131040.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.1.2&nbsp;可并堆的定义可并堆(Mergeable Heap)也是一种抽象数据类型，它除了支持优先队列的三个基本操作(Insert, Minimum, Delete-Min)，还支持一个额外的操作——合并操作：H &#8592; Merge(H...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/10/24/131040.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/131040.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-24 10:53 <a href="http://www.cppblog.com/MiYu/archive/2010/10/24/131040.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PKU 2528 POJ 2528 Mayor's posters ( 线段树+离散化 ) ACM 2528 IN PKU</title><link>http://www.cppblog.com/MiYu/archive/2010/10/15/PKU-2528.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 15 Oct 2010 03:12:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/15/PKU-2528.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/129997.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/15/PKU-2528.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/129997.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/129997.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;题目地址 :	http://poj.org/problem?id=2528题目描述:Mayor's postersTime Limit:&nbsp;1000MSMemory Limit:&nbsp;65536KTotal Submissions:&nbsp;1572...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/10/15/PKU-2528.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/129997.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-15 11:12 <a href="http://www.cppblog.com/MiYu/archive/2010/10/15/PKU-2528.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PKU 2352 POJ 2352 Stars ( 线段树版 ) ACM 2352 IN PKU</title><link>http://www.cppblog.com/MiYu/archive/2010/10/13/PKU-2352-POJ-2352-Stars-ACM-2352-IN-PKU.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 13 Oct 2010 14:29:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/13/PKU-2352-POJ-2352-Stars-ACM-2352-IN-PKU.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/129780.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/13/PKU-2352-POJ-2352-Stars-ACM-2352-IN-PKU.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/129780.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/129780.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">题目地址 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span class="Apple-tab-span" style="white-space: pre; ">	</span><a href="http://poj.org/problem?id=2352">http://poj.org/problem?id=2352</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">题目描述:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><div><font  color="#800080"><span  style="font-family: Simsun; color: rgb(0, 0, 0); line-height: normal; font-size: medium; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><div class="ptt" lang="en-US" style="text-align: center; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Stars</div><div class="plm" style="text-align: center; font-family: Arial, Helvetica, sans-serif; font-size: 12pt; "><table align="center"><tbody><tr><td><strong>Time Limit:</strong>&nbsp;1000MS</td><td width="10px"></td><td><strong>Memory Limit:</strong>&nbsp;65536K</td></tr><tr><td><strong>Total Submissions:</strong>&nbsp;14814</td><td width="10px"></td><td><strong>Accepted:</strong>&nbsp;6404</td></tr></tbody></table></div><p class="pst" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Description</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; ">Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.&nbsp;<br><center><img src="http://poj.org/images/2352_1.jpg"></center><br>For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.&nbsp;<br><br>You are to write a program that will count the amounts of the stars of each level on a given map.</div><p class="pst" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Input</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; ">The first line of the input file contains a number of stars N (1&lt;=N&lt;=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0&lt;=X,Y&lt;=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.&nbsp;<br></div><p class="pst" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Output</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; ">The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.</div><p class="pst" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Sample Input</p><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; ">5
1 1
5 1
7 1
3 3
5 5</pre><p class="pst" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; ">Sample Output</p><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; ">1
2
1
1
0</pre><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; "> 与树状数组的解法一样,  查询更新就可以了. </pre><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>树状数组 解法传送门 :&nbsp;<span  style="font-family: Simsun; white-space: normal; font-size: medium; "><a href="http://www.cnblogs.com/MiYu/archive/2010/08/26/1808963.html">http://www.cnblogs.com/MiYu/archive/2010/08/26/1808963.html</a><span  style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; "> </span></span></pre><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; ">代码如下 : </pre><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; "><font  face="'Courier New', Courier, monospace">/*
Mail to   : miyubai@gamil.com
Link      : http://www.cnblogs.com/MiYu  || http://www.cppblog.com/MiYu
Author By : MiYu
Test      : 1
Complier  : g++ mingw32-3.4.2
Program   : POJ_2352
Doc Name  : Stars
*/
//#pragma warning( disable:4789 )
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
#include &lt;string&gt;
#include &lt;set&gt;
#include &lt;map&gt;
#include &lt;utility&gt;
#include &lt;queue&gt;
#include &lt;stack&gt;
#include &lt;list&gt;
#include &lt;vector&gt;
#include &lt;cstdio&gt;
#include &lt;cstdlib&gt;
#include &lt;cstring&gt;
#include &lt;cmath&gt;
#include &lt;ctime&gt;
using namespace std;
struct P{
       int x, y;
}p[15010];
int seg[100000];
int NN = 32768, res[15010];
int get ( int &amp;n ) {
    int i = 1;
    while ( i &lt; n ) i &lt;&lt; 1;
    return i;    
}
void modify ( int &amp;pos ) {
     pos += NN;
     seg[pos] ++;
     while ( pos &gt; 1 ) {
           if ( ~pos &amp; 1 ) seg[pos&gt;&gt;1] ++;
           pos &gt;&gt;= 1;
     }
}
int query ( int pos ) {
     pos += NN;  
     int sum = seg[pos];
     while ( pos &gt; 1 ) {
            if ( pos &amp; 1 ) {
                 sum += seg[pos&gt;&gt;1];  
            }      
            pos &gt;&gt;= 1;
     }   
     return sum;
}
int main ()
{
    int N;
    while ( scanf ( "%d", &amp;N ) == 1 ) {
           int pos;
           memset ( res, 0, sizeof ( res ) );
           memset ( seg, 0, sizeof ( seg ) );
           for ( int i = 0; i &lt; N; ++ i ) {
                  scanf ( "%d%*d", &amp;pos );
                  res[ query ( pos ) ] ++;
                  modify ( pos );   
           }   
           for ( int i = 0; i &lt; N; ++ i ) {
                printf ( "%d\n", res[i] );     
           } 
    }
    return 0;
}</font>
</pre><div><br></div></span></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/129780.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-13 22:29 <a href="http://www.cppblog.com/MiYu/archive/2010/10/13/PKU-2352-POJ-2352-Stars-ACM-2352-IN-PKU.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU 3016  HDOJ 3016 Man Down ACM 3016 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/10/13/HDU-2871-HDOJ-2871-Memory-Control-ACM-2871-IN-HDU.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 13 Oct 2010 13:26:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/13/HDU-2871-HDOJ-2871-Memory-Control-ACM-2871-IN-HDU.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/129775.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/13/HDU-2871-HDOJ-2871-Memory-Control-ACM-2871-IN-HDU.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/129775.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/129775.html</trackback:ping><description><![CDATA[<p style="LINE-HEIGHT: 21px; TEXT-INDENT: 0px; MARGIN: 5px auto; FONT-FAMILY: verdana, 'courier new'; FONT-SIZE: 14px">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font color=#000000>______________白白の屋</font></a>&nbsp;&nbsp;<img alt="" src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif">&nbsp;&nbsp;</p>
<p style="LINE-HEIGHT: 21px; TEXT-INDENT: 0px; MARGIN: 5px auto; FONT-FAMILY: verdana, 'courier new'; FONT-SIZE: 14px">题目地址 :</p>
<p style="LINE-HEIGHT: 21px; TEXT-INDENT: 0px; MARGIN: 5px auto; FONT-FAMILY: verdana, 'courier new'; FONT-SIZE: 14px"><span style="WHITE-SPACE: pre" class=Apple-tab-span></span><a href="http://acm.hdu.edu.cn/showproblem.php?pid=3016">http://acm.hdu.edu.cn/showproblem.php?pid=3016</a></p>
<p style="LINE-HEIGHT: 21px; TEXT-INDENT: 0px; MARGIN: 5px auto; FONT-FAMILY: verdana, 'courier new'; FONT-SIZE: 14px">题目描述:</p>
<div style="PADDING-BOTTOM: 5px; LINE-HEIGHT: 21px; OVERFLOW-X: auto; OVERFLOW-Y: auto; BACKGROUND-COLOR: rgb(245,245,245); PADDING-LEFT: 5px; PADDING-RIGHT: 5px; FONT-FAMILY: 'Courier New'; FONT-SIZE: 13px; BORDER-LEFT-COLOR: rgb(204,204,204); WORD-BREAK: break-all; PADDING-TOP: 5px" class=cnblogs_code>
<div><font color=#800080><span style="LINE-HEIGHT: normal; FONT-FAMILY: 'Times New Roman'; COLOR: rgb(0,0,0); FONT-SIZE: medium">
<h1 style="TEXT-ALIGN: center; COLOR: rgb(26,92,200)">Man Down</h1>
<font size=+0><strong><span style="FONT-FAMILY: Arial; COLOR: green; FONT-SIZE: 12px; FONT-WEIGHT: bold">Time Limit: 2000/1000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 32768/32768 K (Java/Others)<br>Total Submission(s): 618&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 197<br></span></strong></font><br><br>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-title.png); PADDING-BOTTOM: 0px; BACKGROUND-COLOR: transparent; PADDING-LEFT: 14px; PADDING-RIGHT: 14px; FONT-FAMILY: Arial; BACKGROUND-POSITION: 0% 100%; HEIGHT: 38px; COLOR: rgb(124,169,237); FONT-SIZE: 18px; FONT-WEIGHT: bold; PADDING-TOP: 0px; background-origin: initial; background-clip: initial" class=panel_title align=left>Problem Description</div>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-content.png); TEXT-ALIGN: left; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 20px; PADDING-RIGHT: 20px; FONT-FAMILY: 'Times New Roman'; HEIGHT: auto; FONT-SIZE: 14px; PADDING-TOP: 0px; background-origin: initial; background-clip: initial" class=panel_content>The Game &#8220;Man Down 100 floors&#8221; is an famous and interesting game.You can enjoy the game from&nbsp;<br>http://hi.baidu.com/abcdxyzk/blog/item/16398781b4f2a5d1bd3e1eed.html<br>
<center><img style="BORDER-BOTTOM-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-TOP-STYLE: none; BORDER-LEFT-STYLE: none" src="http://acm.hdu.edu.cn/data/images/exe3016-1.JPG"></center><br>We take a simplified version of this game. We have only two kinds of planks. One kind of the planks contains food and the other one contains nails. And if the man falls on the plank which contains food his energy will increase but if he falls on the plank which contains nails his energy will decrease. The man can only fall down vertically .We assume that the energy he can increase is unlimited and no borders exist on the left and the right.<br><br>First the man has total energy 100 and stands on the topmost plank of all. Then he can choose to go left or right to fall down. If he falls down from the position (Xi,Yi),he will fall onto the nearest plank which satisfies (xl &lt;= xi &lt;= xr)(xl is the leftmost position of the plank and xr is the rightmost).If no planks satisfies that, the man will fall onto the floor and he finishes his mission. But if the man&#8217;s energy is below or equal to 0 , he will die and the game is Over.<br><br>Now give you the height and position of all planks. And ask you whether the man can falls onto the floor successfully. If he can, try to calculate the maximum energy he can own when he is on the floor.(Assuming that the floor is infinite and its height is 0,and all the planks are located at different height).</div>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-bottom.png); MARGIN: 0px; BACKGROUND-POSITION: 0% 0%; HEIGHT: auto; background-origin: initial; background-clip: initial" class=panel_bottom>&nbsp;</div>
<br>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-title.png); PADDING-BOTTOM: 0px; BACKGROUND-COLOR: transparent; PADDING-LEFT: 14px; PADDING-RIGHT: 14px; FONT-FAMILY: Arial; BACKGROUND-POSITION: 0% 100%; HEIGHT: 38px; COLOR: rgb(124,169,237); FONT-SIZE: 18px; FONT-WEIGHT: bold; PADDING-TOP: 0px; background-origin: initial; background-clip: initial" class=panel_title align=left>Input</div>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-content.png); TEXT-ALIGN: left; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 20px; PADDING-RIGHT: 20px; FONT-FAMILY: 'Times New Roman'; HEIGHT: auto; FONT-SIZE: 14px; PADDING-TOP: 0px; background-origin: initial; background-clip: initial" class=panel_content>There are multiple test cases.<br><br>For each test case, The first line contains one integer N (2 &lt;= N &lt;= 100,000) representing the number of planks.<br><br>Then following N lines representing N planks, each line contain 4 integers (h,xl,xr,value)(h &gt; 0, 0 &lt; xl &lt; xr &lt; 100,000, -1000 &lt;= value &lt;= 1000), h represents the plank&#8217;s height, xl is the leftmost position of the plank and xr is the rightmost position. Value represents the energy the man will increase by( if value &gt; 0) or decrease by( if value &lt; 0) when he falls onto this plank.</div>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-bottom.png); MARGIN: 0px; BACKGROUND-POSITION: 0% 0%; HEIGHT: auto; background-origin: initial; background-clip: initial" class=panel_bottom>&nbsp;</div>
<br>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-title.png); PADDING-BOTTOM: 0px; BACKGROUND-COLOR: transparent; PADDING-LEFT: 14px; PADDING-RIGHT: 14px; FONT-FAMILY: Arial; BACKGROUND-POSITION: 0% 100%; HEIGHT: 38px; COLOR: rgb(124,169,237); FONT-SIZE: 18px; FONT-WEIGHT: bold; PADDING-TOP: 0px; background-origin: initial; background-clip: initial" class=panel_title align=left>Output</div>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-content.png); TEXT-ALIGN: left; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 20px; PADDING-RIGHT: 20px; FONT-FAMILY: 'Times New Roman'; HEIGHT: auto; FONT-SIZE: 14px; PADDING-TOP: 0px; background-origin: initial; background-clip: initial" class=panel_content>If the man can falls onto the floor successfully just output the maximum energy he can own when he is on the floor. But if the man can not fall down onto the floor anyway ,just output &#8220;-1&#8221;(not including the quote)</div>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-bottom.png); MARGIN: 0px; BACKGROUND-POSITION: 0% 0%; HEIGHT: auto; background-origin: initial; background-clip: initial" class=panel_bottom>&nbsp;</div>
<br>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-title.png); PADDING-BOTTOM: 0px; BACKGROUND-COLOR: transparent; PADDING-LEFT: 14px; PADDING-RIGHT: 14px; FONT-FAMILY: Arial; BACKGROUND-POSITION: 0% 100%; HEIGHT: 38px; COLOR: rgb(124,169,237); FONT-SIZE: 18px; FONT-WEIGHT: bold; PADDING-TOP: 0px; background-origin: initial; background-clip: initial" class=panel_title align=left>Sample Input</div>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-content.png); TEXT-ALIGN: left; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 20px; PADDING-RIGHT: 20px; FONT-FAMILY: 'Times New Roman'; HEIGHT: auto; FONT-SIZE: 14px; PADDING-TOP: 0px; background-origin: initial; background-clip: initial" class=panel_content>
<pre style="MARGIN: 0px; FONT-SIZE: 14px">
<div style="FONT-FAMILY: 'Courier New', Courier, monospace">4
10 5 10 10
5 3 6 -100
4 7 11 20
2 2 1000 10</div>
</pre>
</div>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-bottom.png); MARGIN: 0px; BACKGROUND-POSITION: 0% 0%; HEIGHT: auto; background-origin: initial; background-clip: initial" class=panel_bottom>&nbsp;</div>
<br>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-title.png); PADDING-BOTTOM: 0px; BACKGROUND-COLOR: transparent; PADDING-LEFT: 14px; PADDING-RIGHT: 14px; FONT-FAMILY: Arial; BACKGROUND-POSITION: 0% 100%; HEIGHT: 38px; COLOR: rgb(124,169,237); FONT-SIZE: 18px; FONT-WEIGHT: bold; PADDING-TOP: 0px; background-origin: initial; background-clip: initial" class=panel_title align=left>Sample Output</div>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-content.png); TEXT-ALIGN: left; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 20px; PADDING-RIGHT: 20px; FONT-FAMILY: 'Times New Roman'; HEIGHT: auto; FONT-SIZE: 14px; PADDING-TOP: 0px; background-origin: initial; background-clip: initial" class=panel_content>
<pre style="MARGIN: 0px; FONT-SIZE: 14px">
<div style="FONT-FAMILY: 'Courier New', Courier, monospace">140</div>
</pre>
</div>
<div style="BACKGROUND-IMAGE: url(http://acm.hdu.edu.cn/images/panel-bottom.png); MARGIN: 0px; BACKGROUND-POSITION: 0% 0%; HEIGHT: auto; background-origin: initial; background-clip: initial" class=panel_bottom>&nbsp;</div>
<div><br></div>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;/*</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp;题目描述: &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;不同高度处有不同的水平板，跳到该板会有血量变化v，</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;问当一个人从最高板开始，可以向左或者向右，</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;竖直跳到下面的板，求下落到地面的最大血量，或者-1。</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;线段树+dp &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;需要用线段树查询得到每个板的两个端点下落后会到哪个板；</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;然后根据这个从最高的开始dp就可以了</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dp[i] = max ( dp[i], dp[i^].v ) &nbsp;// dp[i^] 代表能走到 i 的线段&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">/*&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto"><br></p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">/*</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">Mail to &nbsp; : miyubai@gamil.com</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">Link &nbsp; &nbsp; &nbsp;: http://www.cnblogs.com/MiYu &nbsp;|| http://www.cppblog.com/MiYu</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">Author By : MiYu</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">Test &nbsp; &nbsp; &nbsp;: 1</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">Complier &nbsp;: g++ mingw32-3.4.2</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">Program &nbsp; : HDU_3016</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">Doc Name &nbsp;: Man Down</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">*/</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">//#pragma warning( disable:4789 )</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;iostream&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;fstream&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;sstream&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;algorithm&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;string&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;set&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;map&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;utility&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;queue&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;stack&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;list&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;vector&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;cstdio&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;cstdlib&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;cstring&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;cmath&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">#include &lt;ctime&gt;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">using namespace std;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">struct seg_tree {</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;int id, left, right;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;int mid () { return ( left + right )&gt;&gt;1; } &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">}seg[333333];</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">inline void creat ( int x, int y, int rt = 1 ) {</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; seg[rt].left = x;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; seg[rt].right = y;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; //0 代表地面 其他的自然数代表各层的木板编号 &nbsp;-1 代表有多条线段覆盖&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; seg[rt].id = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; if ( x == y ) return ;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; int mid = seg[rt].mid();</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; creat ( x, mid, rt &lt;&lt; 1 );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; creat ( mid + 1, y, rt &lt;&lt; 1 | 1 ); &nbsp; &nbsp;&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">}</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">inline void modify ( int x, int y, int id, int rt = 1 ) {</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; //找到了线段, 直接修改ID 覆盖掉&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; if ( seg[rt].left == x &amp;&amp; seg[rt].right == y ) {</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; seg[rt].id = id;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp;&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; }</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; int LL = rt &lt;&lt; 1, RR = rt &lt;&lt; 1 | 1, mid = seg[rt].mid();</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; // 前面没有return掉, 那么这条线段肯定是被覆盖的, 将它的标记下传后标记为-1&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; if ( seg[rt].id != -1 ) { &nbsp; &nbsp; &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; seg[LL].id = seg[RR].id = seg[rt].id; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; seg[rt].id = -1;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; } &nbsp; &nbsp; &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; if ( y &lt;= mid ) modify ( x, y, id, LL ); //分段修改&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; else if ( x &gt; mid ) modify ( x, y, id, RR );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; else {</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modify ( x, mid, id, LL );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modify ( mid + 1, y, id, RR ); &nbsp; &nbsp;&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; }</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">}</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">inline int query ( int pos, int rt = 1 ) { &nbsp; // 查询 Pos 所在线段的 id &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;if ( seg[rt].id != -1 ) return seg[rt].id; //线段被覆盖 直接返回 ID&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;int LL = rt &lt;&lt; 1, RR = rt &lt;&lt; 1 | 1, mid = seg[rt].mid();</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;if ( pos &lt;= mid ) return query ( pos, LL ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //分段查询&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;else return query ( pos, RR ); &nbsp; &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">}</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">inline bool scan_d(int &amp;num) &nbsp;//整数输入</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">{</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;char in;bool IsN=false;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;in=getchar();</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in==EOF) return false;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in!='-'&amp;&amp;(in&lt;'0'||in&gt;'9')) in=getchar();</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in=='-'){ IsN=true;num=0;}</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;else num=in-'0';</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in=getchar(),in&gt;='0'&amp;&amp;in&lt;='9'){</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num*=10,num+=in-'0';</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(IsN) num=-num;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return true;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">}</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">struct Plank {</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; int x,y,h,v,left,right;&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; //按高排序 &nbsp; &nbsp; &nbsp;&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; friend bool operator &lt; ( const Plank &amp;a, const Plank &amp;b ) {</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a.h &lt; b.h;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; }</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">}pk[100010];</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">int dp[100010];</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">int main ()</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">{</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;int N, M;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;creat ( 1, 100000 );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;while ( scan_d ( N ) ) {</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; M = -1;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 1; i &lt;= N; ++ i ) {</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scan_d ( pk[i].h );scan_d ( pk[i].x );scan_d ( pk[i].y );scan_d ( pk[i].v );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( pk[i].y &gt; M ) M = pk[i].y; &nbsp; &nbsp; &nbsp; // 记录 区间最大值, 加速用的&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modify &nbsp;( 1, M, 0 );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sort ( pk + 1, pk + N + 1 ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 按高排序&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memset ( dp, 0, sizeof ( dp ) );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dp[N] = 100 + pk[N].v;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 自底向上 更新 线段, 记录 每条线段 左右端点能到达的 线段 ID&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 1; i &lt;= N; ++ i ) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int x = pk[i].left = query ( pk[i].x );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int y = pk[i].right = query ( pk[i].y );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modify ( pk[i].x, pk[i].y, i );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int res = -1;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //自顶向下 DP &nbsp; &nbsp;dp[i] = max ( dp[i], dp[i^].v ) &nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // dp[i^] 代表能走到 i 的线段&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = N; i &gt;= 1; -- i ) { &nbsp;&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( dp[ pk[i].left ] &lt; dp[i] + pk[ pk[i].left ].v )</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dp[ pk[i].left ] = dp[i] + pk[ pk[i].left ].v;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( dp[ pk[i].right ] &lt; dp[i] + pk[ pk[i].right ].v )</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dp[ pk[i].right ] = dp[i] + pk[ pk[i].right ].v;&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf ( "%d\n",dp[0] &gt; 0 ? dp[0] : -1 );</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;}</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;&nbsp; &nbsp;return 0;</p>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">}</p>
<div><br></div>
<p style="TEXT-INDENT: 0px; MARGIN: 5px auto">&nbsp;</p>
</span></font></div>
</div>
<p style="LINE-HEIGHT: 21px; TEXT-INDENT: 0px; MARGIN: 5px auto; FONT-FAMILY: verdana, 'courier new'; FONT-SIZE: 14px"></p>
<p style="LINE-HEIGHT: 21px; TEXT-INDENT: 0px; MARGIN: 5px auto; FONT-FAMILY: verdana, 'courier new'; FONT-SIZE: 14px">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/129775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-13 21:26 <a href="http://www.cppblog.com/MiYu/archive/2010/10/13/HDU-2871-HDOJ-2871-Memory-Control-ACM-2871-IN-HDU.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU 2871 HDOJ 2871 Memory Control ACM 2871 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/10/07/128951.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Thu, 07 Oct 2010 13:49:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/07/128951.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/128951.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/07/128951.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/128951.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/128951.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题目地址:	http://acm.hdu.edu.cn/showproblem.php?pid=2871题目描述:Memory ControlTime Limit: 2000/1000 MS (Java/Others)&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/10/07/128951.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/128951.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-07 21:49 <a href="http://www.cppblog.com/MiYu/archive/2010/10/07/128951.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PKU 3667 HDOJ 3667 Hotel ACM 3667 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/10/07/128936.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Thu, 07 Oct 2010 09:36:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/07/128936.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/128936.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/07/128936.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/128936.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/128936.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题目地址 :	http://poj.org/problem?id=3667题目描述:HotelTime Limit:&nbsp;3000MSMemory Limit:&nbsp;65536KTotal Submissions:&nbsp;2993Acce...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/10/07/128936.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/128936.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-07 17:36 <a href="http://www.cppblog.com/MiYu/archive/2010/10/07/128936.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU 1540 HDOJ 1540 Tunnel Warfare ACM 1540 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/10/06/128767.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 06 Oct 2010 03:06:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/10/06/128767.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/128767.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/10/06/128767.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/128767.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/128767.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题目地址:	http://acm.hdu.edu.cn/showproblem.php?pid=1540题目描述:Tunnel WarfareTime Limit: 4000/2000 MS (Java/Others)&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/10/06/128767.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/128767.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-10-06 11:06 <a href="http://www.cppblog.com/MiYu/archive/2010/10/06/128767.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU 2795 HDOJ 2795 Billboard ACM 2795 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/09/26/127809.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sun, 26 Sep 2010 14:15:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/09/26/127809.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/127809.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/09/26/127809.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/127809.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/127809.html</trackback:ping><description><![CDATA[
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><br><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目地址:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">	</span>&nbsp;<a href="http://acm.hdu.edu.cn/showproblem.php?pid=2795">http://acm.hdu.edu.cn/showproblem.php?pid=2795</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目描述:<br></p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font  color="#800080"><span  style="font-family: 'Times New Roman'; color: rgb(0, 0, 0); line-height: normal; font-size: medium; "><h1 style="color: rgb(26, 92, 200); text-align: center; ">Billboard</h1><font><strong><span style="font-family: Arial; font-size: 12px; font-weight: bold; color: green; ">Time Limit: 20000/8000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 32768/32768 K (Java/Others)<br>Total Submission(s): 733&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 340<br></span></strong></font><br><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.<br><br>On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.<br><br>Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.<br><br>When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.<br><br>If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).<br><br>Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">There are multiple cases (no more than 40 cases).<br><br>The first line of the input file contains three integer numbers, h, w, and n (1 &lt;= h,w &lt;= 10^9; 1 &lt;= n &lt;= 200,000) - the dimensions of the billboard and the number of announcements.<br><br>Each of the next n lines contains an integer number wi (1 &lt;= wi &lt;= 10^9) - the width of i-th announcement.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">3 5 5
2
4
3
3
3</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">1
2
1
3
-1</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><div><br></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">一开始没想明白怎么做 , &nbsp;仔细想了想, &nbsp; 再次 读题后 发现 , n &lt;= 200000; &nbsp;也就是说 &nbsp;最多 也就 200000 条广告 , 你就算每行贴一张 ,</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">最多也就贴到 200000 行, &nbsp;所以 &nbsp;不要被 &nbsp;h &lt;= 10^9 &nbsp;次方吓到了 &nbsp;,认为 线段树开不了那么大的数组 &nbsp;. 只要开 200000 就可以了 .</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">其他的 没什么 好说的 , 知道这个 就直接 暴 吧 ............将近 7000MS .=水过.................&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/134950908.gif" alt="">&nbsp;&nbsp;g++提交 还华丽的 送出了 一次 TLE....<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">C++ &nbsp;水过了 ............</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 'Courier New'; color: red; ">// &nbsp; &nbsp;一直没明白 为什么我的 代码速度 那么 慢, &nbsp;查询后 更新 的时间是 2000MS 左右 &nbsp;, 我的 是 查询 就更新了,</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 'Courier New'; color: red; ">竟然要 将近 7000MS ? 非常 郁闷 , &nbsp;不信邪的 继续 检查 代码, 在 瞪了 1哥小时后 , 忽然想到 : 把 cout 改成</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 'Courier New'; color: red; ">printf 会怎样? &nbsp; 结果 : &nbsp;1640 MS &nbsp;AC ..............鬼知道他的 数据量有多大..... cout 和 printf</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span style="font-family: 'Courier New'; color: red; ">竟然 差了 5000 MS 的时间 ...........无语</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">代码如下 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;/*</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Coded By &nbsp;: MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Link &nbsp; &nbsp; &nbsp;: http://www.cnblogs.com/MiYu &nbsp;|| http://www.cppblog.com/MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Author By : MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Test &nbsp; &nbsp; &nbsp;: 1</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Program &nbsp; : 2795</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">*/</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">//#pragma warning( disable:4789 )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;iostream&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;algorithm&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;string&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;set&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;map&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;utility&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;queue&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;stack&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;list&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;vector&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;cstdio&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;cstdlib&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;cstring&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;cmath&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">using namespace std;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><br></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">struct ADV {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; int left, right, val;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; int mid () { return ( left + right ) &gt;&gt; 1; } &nbsp; &nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}adv[800000];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int N, W, H, w;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">void creat ( int l, int r, int rt = 1 ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; adv[rt].left = l;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; adv[rt].right = r;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; adv[rt].val = W;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; if ( l == r )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return ;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; int mid = adv[rt].mid();</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; creat ( l, mid, rt &lt;&lt; 1 );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; creat ( mid + 1, r, ( rt &lt;&lt; 1 ) + 1 );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">void add ( int rt = 1, int wei = w ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;if ( wei &lt;= adv[rt].val ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ( adv[rt].left == adv[rt].right ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adv[rt].val -= wei;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="font-family: 'Courier New'; color: red; ">&nbsp;//cout &lt;&lt; adv[rt].left &lt;&lt; endl; &nbsp;//杯具的 地方</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	printf ( "%d\n", adv[rt].left );</span>&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return ; &nbsp; &nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } else if ( adv[rt&lt;&lt;1].val &gt;= wei ) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add ( rt &lt;&lt; 1 );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adv[rt].val = max ( adv[rt&lt;&lt;1].val, adv[(rt&lt;&lt;1)+1].val ); &nbsp; &nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } else {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add ( ( rt &lt;&lt; 1 ) + 1 );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adv[rt].val = max ( adv[rt&lt;&lt;1].val, adv[(rt&lt;&lt;1)+1].val ); &nbsp; &nbsp; &nbsp; &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;} else {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<span style="font-family: 'Courier New'; color: red; ">cout &lt;&lt; -1 &lt;&lt; endl;&nbsp;</span>&nbsp;&nbsp;&nbsp;<span style="font-family: 'Courier New'; color: red; ">&nbsp; //杯具的地方</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>puts ( "-1" ); &nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;} &nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">inline bool scan_ud(int &amp;num)</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;char in;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;in=getchar();</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;if(in==EOF) return false;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while(in&lt;'0'||in&gt;'9') in=getchar();</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;num=in-'0';</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while(in=getchar(),in&gt;='0'&amp;&amp;in&lt;='9'){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;num*=10,num+=in-'0';</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return true;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int main ()</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( scan_ud (H)&amp;&amp;scan_ud (W)&amp;&amp;scan_ud (N) ) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( H &gt; 200000 )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; H = 200010;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; creat ( 1, H );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 1; i &lt;= N; ++ i ) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scan_ud (w);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add ( ); &nbsp; &nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">另 附上 傻崽 &nbsp;神牛 代码 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;iostream&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;algorithm&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;string&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;set&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;map&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;utility&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;queue&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;stack&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;list&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;vector&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;cstdio&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;cstdlib&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;cstring&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;cmath&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#define FF(i,a)<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">				</font></span>for( int i = 0 ; i &lt; a ; i ++ )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#define FOR(i,a,b)<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">			</font></span>for( int i = a ; i &lt; b ; i ++ )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#define LL(a)<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">				</font></span>a&lt;&lt;1<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#define RR(a)<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">				</font></span>a&lt;&lt;1|1<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">template&lt;class T&gt; inline void checkmin(T &amp;a,T b)<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>{if(a &lt; 0 || a &gt; b)a = b;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">template&lt;class T&gt; inline void checkmax(T &amp;a,T b)<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>{if(a &lt; b)<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>a = b;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">using namespace std;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">struct Node {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>int val;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>int idx;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>friend bool operator &lt; (Node a , Node b) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>if(a.val == b.val) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">			</font></span>return a.idx &gt; b.idx;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>return a.val &lt; b.val;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}error;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">struct Seg_Tree{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>int left,right;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>Node node;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>int mid() {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>return (left + right)&gt;&gt;1;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}tt[800000];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int n , h , m;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">void build(int l,int r,int idx) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>tt[idx].left = l;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>tt[idx].right = r;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>tt[idx].node.idx = l;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>tt[idx].node.val = h;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>if(l == r) return ;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>int mid = tt[idx].mid();</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>build(l,mid,LL(idx));</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>build(mid+1,r,RR(idx));</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">void update(int l,int r,int val,int idx) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>if(l &lt;= tt[idx].left &amp;&amp; r &gt;= tt[idx].right) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>tt[idx].node.val += val;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>return ;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>int mid = tt[idx].mid();</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>if(l &lt;= mid) update(l,r,val,LL(idx));</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>if(mid &lt; r)<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>update(l,r,val,RR(idx));</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>tt[idx].node = max(tt[LL(idx)].node,tt[RR(idx)].node);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Node query(int w,int idx) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>if(tt[idx].node.val &lt; w) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>return error;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>if(tt[idx].left == tt[idx].right) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>return tt[idx].node;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>if(tt[LL(idx)].node.val &gt;= w) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>return query(w,LL(idx));</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>} else {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>return query(w,RR(idx));</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int main() {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>error.idx = -1;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>while(scanf("%d%d%d",&amp;n,&amp;h,&amp;m) == 3) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>checkmin(n,m);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>build(1,n,1);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>while(m --) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">			</font></span>int w;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">			</font></span>scanf("%d",&amp;w);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">			</font></span>Node ret = query(w,1);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">			</font></span>printf("%d\n",ret.idx);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">			</font></span>if(ret.idx != -1) {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">				</font></span>update(ret.idx,ret.idx,-w,1);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">			</font></span>}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">		</font></span>}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "><font  face="'Times New Roman'">	</font></span>return 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}&nbsp;</p></span></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p>&nbsp;</p><img src ="http://www.cppblog.com/MiYu/aggbug/127809.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-09-26 22:15 <a href="http://www.cppblog.com/MiYu/archive/2010/09/26/127809.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 1698 HDU 1698 Just a Hook ACM 1698 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/09/17/126829.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 17 Sep 2010 02:09:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/09/17/126829.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/126829.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/09/17/126829.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/126829.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/126829.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题目地址 :&nbsp;&nbsp; &nbsp; &nbsp;http://acm.hdu.edu.cn/showproblem.php?pid=1698题目描述 :&nbsp;Just a HookTime Limit: 40...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/09/17/126829.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/126829.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-09-17 10:09 <a href="http://www.cppblog.com/MiYu/archive/2010/09/17/126829.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 1754 HDU 1754 I Hate It ACM 1754 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/09/15/126668.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 15 Sep 2010 08:08:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/09/15/126668.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/126668.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/09/15/126668.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/126668.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/126668.html</trackback:ping><description><![CDATA[<p><span style="font-family: Verdana,Geneva,Arial,Helvetica,sans-serif; line-height: normal; font-size: 13px;">MiYu原创, 转帖请注明 : 转载自&nbsp;<font color="#1d58d1"><a href="http://www.cnblogs.com/MiYu" style="text-decoration: none; color: rgb(29, 88, 209);">______________白白の屋</a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</font></span></p>
<p>&nbsp;</p><p>题目地址:</p><p>　　<a href="http://acm.hdu.edu.cn/showproblem.php?pid=1754">http://acm.hdu.edu.cn/showproblem.php?pid=1754</a></p><p>题目描述:</p><p>　　</p>
<div class="cnblogs_code"><div><span style="color: rgb(128, 0, 128);"><span style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><h1>I Hate It</h1><strong><span style="font-family: Arial; font-size: 12px; font-weight: bold; color: green;">Time Limit: 9000/3000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 32768/32768 K (Java/Others)<br>Total Submission(s): 6306&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 2267<br></span></strong><br><br><div id="" style="height: 38px; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-title.png&quot;); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%;" align="left">Problem Description</div><div id="" style="height: auto; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-content.png&quot;); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman';">很多学校流行一种比较的习惯。老师们很喜欢询问，从某某到某某当中，分数最高的是多少。<br>这让很多学生很反感。<br><br>不管你喜不喜欢，现在需要你做的是，就是按照老师的要求，写一个程序，模拟老师的询问。当然，老师有时候需要更新某位同学的成绩。</div><div style="height: auto; margin: 0px; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-bottom.png&quot;); background-position: 0% 0%;">&nbsp;</div><br><div style="height: 38px; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-title.png&quot;); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%;" align="left">Input</div><div id="" style="height: auto; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-content.png&quot;); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman';">本题目包含多组测试，请处理到文件结束。<br>在每个测试的第一行，有两个正整数 N 和 M ( 0&lt;N&lt;=200000,0&lt;M&lt;5000 )，分别代表学生的数目和操作的数目。<br>学生ID编号分别从1编到N。<br>第二行包含N个整数，代表这N个学生的初始成绩，其中第i个数代表ID为i的学生的成绩。<br>接下来有M行。每一行有一个字符 C (只取'Q'或'U') ，和两个正整数A，B。<br>当C为'Q'的时候，表示这是一条询问操作，它询问ID从A到B(包括A,B)的学生当中，成绩最高的是多少。<br>当C为'U'的时候，表示这是一条更新操作，要求把ID为A的学生的成绩更改为B。<br></div><div style="height: auto; margin: 0px; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-bottom.png&quot;); background-position: 0% 0%;">&nbsp;</div><br><div style="height: 38px; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-title.png&quot;); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%;" align="left">Output</div><div style="height: auto; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-content.png&quot;); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman';">对于每一次询问操作，在一行里面输出最高成绩。</div><div style="height: auto; margin: 0px; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-bottom.png&quot;); background-position: 0% 0%;">&nbsp;</div><br><div style="height: 38px; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-title.png&quot;); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%;" align="left">Sample Input</div><div style="height: auto; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-content.png&quot;); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman';"><pre style="margin: 0px; font-size: 14px;"><div style="font-family: 'Courier New',Courier,monospace;">5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5</div></pre></div><div style="height: auto; margin: 0px; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-bottom.png&quot;); background-position: 0% 0%;">&nbsp;</div><br><div style="height: 38px; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-title.png&quot;); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%;" align="left">Sample Output</div><div style="height: auto; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-content.png&quot;); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman';"><pre style="margin: 0px; font-size: 14px;"><div style="font-family: 'Courier New',Courier,monospace;">5 6 5 9  <div style="font-family: 'Times New Roman'; font-size: 14px; background-color: rgb(244, 251, 255); border-left-color: rgb(183, 203, 255); padding-top: 6px; padding-right: 6px; padding-bottom: 6px; padding-left: 6px; "><div style="font-family: Arial; font-weight: bold; color: rgb(124, 169, 237); "><em>Hint</em></div>Huge input,the C function scanf() will work better than cin</div><em style="font-size: 1px;"> </em></div></pre></div><div style="height: auto; margin: 0px; background-image: url(&quot;http://acm.hdu.edu.cn/images/panel-bottom.png&quot;); background-position: 0% 0%;">&nbsp;</div></span><br>
</span></div><p>&nbsp;</p><p>感觉好久没有A题了 , 最近一直没有状态,&nbsp; 豆豆也转行了, 郁闷....... &nbsp;&nbsp; 因为打算 专精 数据结构方面,</p><p>所以这几天一直都在复习 数据结构, 再一次学习了 线段树, 以前只会用它来 更新点 求和 , 现在终于水了一</p><p>个 RMQ 的裸题了, HAPPY 一下.... <br>
</p><p>对于 RMQ 的题目, 看PPT 上面的 DP 我直接0rz了...........表示DP只会做水题.... 这方面还是交给<br>
</p><p>YCH 吧. &nbsp; 不过看了&nbsp;<span style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Segoe UI',Calibri,'Myriad Pro',Myriad,'Trebuchet MS',Helvetica,Arial,sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="color: rgb(187, 187, 187); font-size: 13px; line-height: 32px;"><a href="http://www.notonlysuccess.com/?author=1" title="shǎ崽 " style="margin: 0px; padding: 0px; font-family: 'Segoe UI',Calibri,'Myriad Pro',Myriad,'Trebuchet MS',Helvetica,Arial,sans-serif; color: rgb(153, 153, 153); outline-style: none; font-weight: bold;">shǎ崽</a></span></span> 大神 博客的 线段树专辑后, 发现 用线段树处理 这类问题 非常方便, 修改查询</p><p>都是 O (logN)的 ,&nbsp; 稍稍优化了下输入, 234MS AC ........ <br>
</p><p>&nbsp;</p><p>代码如下 :<br>
</p>
  <div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" class="code_img_closed" id="code_img_closed_c9d6cbc8-2faa-4358-87c6-2f687a9ae84d" style="display: none;"><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_c9d6cbc8-2faa-4358-87c6-2f687a9ae84d"><span class="cnblogs_code_collapse">代码</span><div id="cnblogs_code_open_c9d6cbc8-2faa-4358-87c6-2f687a9ae84d"><div><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br>Coded&nbsp;By&nbsp;&nbsp;:&nbsp;MiYu<br>Link&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;</span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://www.cnblogs.com/MiYu</span><span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;||&nbsp;</span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://www.cppblog.com/MiYu</span><span style="color: rgb(0, 128, 0);"><br>Author&nbsp;By&nbsp;:&nbsp;MiYu<br>Test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;1<br>Program&nbsp;&nbsp;&nbsp;:&nbsp;1754<br></span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">#pragma&nbsp;warning(&nbsp;disable:4789&nbsp;)</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">algorithm</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">map</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">utility</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">queue</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stack</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">list</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">vector</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">cstdio</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">cstdlib</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">cstring</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br>#include&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">cmath</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);">&nbsp;std;<br>inline&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;max&nbsp;(&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;a,&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;b&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;a&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;b&nbsp;</span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);">&nbsp;a&nbsp;:&nbsp;b;<br>}<br>typedef&nbsp;</span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);">&nbsp;seg_Tree&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;left,&nbsp;right;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;mid()&nbsp;{&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;(left</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">right)</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;max;<br>}S;<br>S&nbsp;seg[</span><span style="color: rgb(128, 0, 128);">605000</span><span style="color: rgb(0, 0, 0);">];<br></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;key[</span><span style="color: rgb(128, 0, 128);">200010</span><span style="color: rgb(0, 0, 0);">];<br></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;creat&nbsp;(&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;left,&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;right,&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;root&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;seg[root].left&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;left;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;seg[root].right&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;right;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;left&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;right&nbsp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;seg[root].max&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;key[left];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;mid&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;seg[root].mid();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;seg[root].max&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;max&nbsp;(&nbsp;creat&nbsp;(&nbsp;left,&nbsp;mid,&nbsp;root&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;),creat&nbsp;(&nbsp;mid&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;right,&nbsp;(&nbsp;root&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;)&nbsp;);<br>}<br><br></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;modify&nbsp;(&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;val,&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;pos,&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;r&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;seg[r].left&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;seg[r].right&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seg[r].max&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;val;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;mid&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;seg[r].mid();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;pos&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">&nbsp;mid&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;modify&nbsp;(&nbsp;val,&nbsp;pos,&nbsp;r&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;modify&nbsp;(&nbsp;val,&nbsp;pos,&nbsp;(&nbsp;r&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;seg[r].max&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;max&nbsp;(&nbsp;seg[r</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">].max,&nbsp;seg[&nbsp;(r</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;].max&nbsp;);<br>}<br><br></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;quy&nbsp;(&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;left,&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;right,&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;r&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;seg[r].left&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;left&nbsp;</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">&nbsp;seg[r].right&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;right&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;seg[r].max;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;mid&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;seg[r].mid();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;right&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">&nbsp;mid&nbsp;&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;quy&nbsp;(&nbsp;left,&nbsp;right,&nbsp;r&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;left&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;mid&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;quy&nbsp;(&nbsp;left,&nbsp;right,&nbsp;(r&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;max&nbsp;(&nbsp;quy&nbsp;(&nbsp;left,&nbsp;mid,&nbsp;r&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;),&nbsp;quy&nbsp;(&nbsp;mid&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;right,&nbsp;(r&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;)&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br>inline&nbsp;</span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);">&nbsp;scan_d(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">num) <br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);">&nbsp;IsN</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">EOF)&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">-</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">9</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">))&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">-</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">){&nbsp;IsN</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;num</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;num</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">getchar(),</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">9</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num</span><span style="color: rgb(0, 0, 0);">*=</span><span style="color: rgb(128, 0, 128);">10</span><span style="color: rgb(0, 0, 0);">,num</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(IsN)&nbsp;num</span><span style="color: rgb(0, 0, 0);">=-</span><span style="color: rgb(0, 0, 0);">num;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br>}<br></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;main&nbsp;()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;N,&nbsp;M,&nbsp;x,&nbsp;y;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;scan_d(N)&nbsp;</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">&nbsp;scan_d(M)&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">&nbsp;N;&nbsp;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scan_d(&nbsp;key[i]&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;creat&nbsp;(&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;N&nbsp;);&nbsp;&nbsp; </span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;M&nbsp;</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">&nbsp;ask[</span><span style="color: rgb(128, 0, 128);">5</span><span style="color: rgb(0, 0, 0);">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf&nbsp;(&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">%s</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;ask&nbsp;);</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scan_d(x);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scan_d(y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">switch</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;ask[</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">]&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Q</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:&nbsp;&nbsp;&nbsp;&nbsp;printf&nbsp;(&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">%d\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;quy&nbsp;(&nbsp;x,y&nbsp;)&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">U</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:&nbsp;&nbsp;&nbsp;&nbsp;modify&nbsp;(&nbsp;y,&nbsp;x&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;<br>}<br><br></span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br>5&nbsp;6<br>1&nbsp;2&nbsp;3&nbsp;4&nbsp;5<br>Q&nbsp;1&nbsp;5<br>U&nbsp;3&nbsp;6<br>Q&nbsp;3&nbsp;4<br>Q&nbsp;4&nbsp;5<br>U&nbsp;2&nbsp;9<br>Q&nbsp;1&nbsp;5<br></span><span style="color: rgb(0, 128, 0);">*/</span></div></div></div>
  <p><br>
  </p>
<p>&nbsp;</p><p>&nbsp;</p></div>
<p><br>
</p>
<p> </p>
<img src ="http://www.cppblog.com/MiYu/aggbug/126668.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-09-15 16:08 <a href="http://www.cppblog.com/MiYu/archive/2010/09/15/126668.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 1166 HDU 1166 敌兵布阵 (线段树解法)ACM 1166 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/09/04/125867.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sat, 04 Sep 2010 04:53:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/09/04/125867.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125867.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/09/04/125867.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125867.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125867.html</trackback:ping><description><![CDATA[

<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">先前做了这一题, 不过是用 树状数组做的, 对于这一类型的题目也来说非常方便快捷. 具体地址 :&nbsp;<a href="http://www.cnblogs.com/MiYu/archive/2010/08/25/1808441.html">http://www.cnblogs.com/MiYu/archive/2010/08/25/1808441.html</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">这几天学了 线段树 , 不是很明白它的用途 和 使用方法, &nbsp;因为 听说 树状数组是 线段树的 一种特殊情况 ( 部分资料是这么说的 ) . &nbsp;于是就又做了一次这个题目.</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">不过使用的是线段树的 方法 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">代码如下 :</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_fd9a7ebe-31b4-4010-86f1-450ac5860cfa" style="vertical-align: middle; padding-right: 5px; "><span class="cnblogs_code_collapse" style="border-right-color: rgb(128, 128, 128); border-right-width: 1px; border-right-style: solid; border-top-color: rgb(128, 128, 128); border-top-width: 1px; border-top-style: solid; border-left-color: rgb(128, 128, 128); border-left-width: 1px; border-left-style: solid; border-bottom-color: rgb(128, 128, 128); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(255, 255, 255); padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px; font-family: 'Courier New'; ">代码</span><div id="cnblogs_code_open_fd9a7ebe-31b4-4010-86f1-450ac5860cfa"><div><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br>MiYu原创,&nbsp;转帖请注明&nbsp;:&nbsp;转载自&nbsp;______________白白の屋<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); text-decoration: underline; ">http://www.cnblog.com/MiYu</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br>Author&nbsp;By&nbsp;:&nbsp;MiYu<br>Test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;3<br>Program&nbsp;&nbsp;&nbsp;:&nbsp;1166<br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">iostream</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">algorithm</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">using</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">namespace</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;std;<br>typedef&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;Line{<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">		</span>//线段树结构体<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;friend&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;optLine;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line&nbsp;()&nbsp;{&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;left&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;right&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;NULL;&nbsp;};<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;l,r,sum;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;Line&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">left,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">right;<br>}L;<br>typedef&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;optLine&nbsp;{<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>//线段树操作结构<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;Creat&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;Line&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">node,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;beg,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;end&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;modify&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;Line&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">node,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;pos,</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;val&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;quy&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;Line&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">node,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;beg,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;end&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>}OPT;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;optLine::Creat&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;Line&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">node,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;beg,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;end&nbsp;){<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>//建树<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">l&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;beg;&nbsp;&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">r&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;beg&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">==</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;end&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;mid&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">l&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">r&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">p&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">new</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;L;&nbsp;&nbsp;L&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">q&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">new</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;L;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">left&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;p;&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">right&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;q;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creat&nbsp;(&nbsp;p,&nbsp;beg,&nbsp;mid&nbsp;);&nbsp;&nbsp;&nbsp;Creat&nbsp;(&nbsp;q,&nbsp;mid&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;end&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;optLine::modify&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;Line&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">node,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;pos,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;val&nbsp;){<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>//修改<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;pos&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">l&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;pos&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">r&nbsp;)&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;val;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">l&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">==</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">r&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;mid&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">l&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">r&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;mid&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;pos&nbsp;)&nbsp;modify&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">left,&nbsp;pos,&nbsp;val&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">else</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;mid&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;pos&nbsp;)&nbsp;modify&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">right,&nbsp;pos,val&nbsp;);<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;optLine::quy&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;Line&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">node,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;beg,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;end&nbsp;){<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>//查询<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;mid&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">l&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">r&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">l&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">==</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;beg&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">r&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">==</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;end&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">sum;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;mid&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;end&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;quy&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">left,&nbsp;beg,end&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">else</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;beg&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;mid&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;quy&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">right,&nbsp;beg,&nbsp;end&nbsp;);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">else</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;quy&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">left,&nbsp;beg,&nbsp;mid&nbsp;)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;quy&nbsp;(&nbsp;node</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">right,&nbsp;mid</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,end&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;<br>}<br>inline&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">bool</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;scan_ud(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">num)&nbsp;<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">==</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">EOF)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">false</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">0</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">||</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">9</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">0</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">getchar(),</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">0</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">9</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">10</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,num</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">0</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">true</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;main&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;T,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N&nbsp;)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;scan_ud&nbsp;(&nbsp;T&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;ca&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;ca&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;T&nbsp;;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;ca&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">Case&nbsp;%d:\n</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,ca&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scan_ud&nbsp;(&nbsp;N&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;OPT&nbsp;opt;&nbsp;&nbsp;L&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">root&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">new</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;L;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;val,x,y;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opt.Creat&nbsp;(&nbsp;root,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;N&nbsp;);&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scan_ud&nbsp;(&nbsp;val&nbsp;);&nbsp;&nbsp;opt.modify&nbsp;(&nbsp;root,&nbsp;i,&nbsp;val&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;ask[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">10</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;scanf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%s</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;ask&nbsp;),&nbsp;ask[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">!=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">E</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%d%d</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">x,</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">y&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">switch</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;ask[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">case</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">Q</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">:&nbsp;&nbsp;printf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%d\n</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,opt.quy&nbsp;(&nbsp;root,&nbsp;x,y&nbsp;)&nbsp;);&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">break</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">case</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">A</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">:&nbsp;&nbsp;opt.modify&nbsp;(&nbsp;root,&nbsp;x,&nbsp;y&nbsp;);&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">break</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">case</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">S</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">:&nbsp;&nbsp;opt.modify&nbsp;(&nbsp;root,&nbsp;x,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">y&nbsp;);&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">break</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>}<br></span></div></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><img src ="http://www.cppblog.com/MiYu/aggbug/125867.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-09-04 12:53 <a href="http://www.cppblog.com/MiYu/archive/2010/09/04/125867.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 2688 HDU 2688 Rotate ACM 2688 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/08/30/125283.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Mon, 30 Aug 2010 08:18:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/30/125283.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125283.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/30/125283.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125283.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125283.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">题目地址:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span class="Apple-tab-span" style="white-space: pre; ">	</span><a href="http://acm.hdu.edu.cn/showproblem.php?pid=2688">http://acm.hdu.edu.cn/showproblem.php?pid=2688</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">题目描述:&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span class="Apple-tab-span" style="white-space: pre; "></span></p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><div id="cnblogs_code_open_893d025d-eb91-47ce-b372-a104db456f21"><div><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><font  size="4"><span  style="font-family: 'Times New Roman'; font-size: medium; line-height: normal; "><h1 style="color: rgb(26, 92, 200); text-align: center; ">Rotate</h1><font><strong><span style="font-family: Arial; font-size: 12px; font-weight: bold; color: green; ">Time Limit: 2000/1000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 32768/32768 K (Java/Others)<br>Total Submission(s): 250&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 68<br></span></strong></font><br><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">Recently yifenfei face such a problem that give you millions of positive integers,tell how many pairs i and j that satisfy F[i] smaller than F[j] strictly when i is smaller than j strictly. i and j is the serial number in the interger sequence. Of course, the problem is not over, the initial interger sequence will change all the time. Changing format is like this [S E] (abs(E-S)&lt;=1000) that mean between the S and E of the sequece will Rotate one times.<br>For example initial sequence is 1 2 3 4 5.<br>If changing format is [1 3], than the sequence will be 1 3 4 2 5 because the first sequence is base from 0.<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">The input contains multiple test cases.<br>Each case first given a integer n standing the length of integer sequence (2&lt;=n&lt;=3000000)&nbsp;<br>Second a line with n integers standing F[i](0&lt;F[i]&lt;=10000)<br>Third a line with one integer m (m &lt; 10000)<br>Than m lines quiry, first give the type of quiry. A character C, if C is &#8216;R&#8217; than give the changing format, if C equal to &#8216;Q&#8217;, just put the numbers of satisfy pairs.<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">Output just according to said.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">5
1 2 3 4 5
3
Q
R 1 3
Q</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">10
8</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><div><br></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;题目分析:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>如果是暴力 , 没一次更新都要重新计算的话, &nbsp;时间上的开销会非常大.</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>对数据进行分析,可以看到, 当旋转的时候, 除了 第一个数, 其他的数的对数的个数值都与这个数有关, &nbsp; 因此, 只要开始</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">先把总的对数 sum 算出来, 再 根据旋转时 每个数跟第一个数的大小 比较 ,对sum 进行更新就可以了.</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">代码如下:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_1c302009-a3a9-495f-a7c0-be4b8a811956" style="vertical-align: middle; padding-right: 5px; "><span class="cnblogs_code_collapse" style="border-right-color: rgb(128, 128, 128); border-right-width: 1px; border-right-style: solid; border-top-color: rgb(128, 128, 128); border-top-width: 1px; border-top-style: solid; border-left-color: rgb(128, 128, 128); border-left-width: 1px; border-left-style: solid; border-bottom-color: rgb(128, 128, 128); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(255, 255, 255); padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px; font-family: 'Courier New'; ">代码</span><div id="cnblogs_code_open_1c302009-a3a9-495f-a7c0-be4b8a811956"><div><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br>MiYu原创,&nbsp;转帖请注明&nbsp;:&nbsp;转载自&nbsp;______________白白の屋<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); text-decoration: underline; ">http://www.cnblog.com/MiYu</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br>Author&nbsp;By&nbsp;:&nbsp;MiYu<br>Test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;1<br>Program&nbsp;&nbsp;&nbsp;:&nbsp;2688<br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">iostream</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>#include&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">algorithm</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">using</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">namespace</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;std;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">const</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;MAX&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">10000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;nCount&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;N,&nbsp;M,&nbsp;x&nbsp;,&nbsp;y;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;com[MAX&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">],&nbsp;num[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">300</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;MAX&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];&nbsp;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">long</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">long</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;ask[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">5</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];<br>inline&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;low&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;x&nbsp;)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">x&nbsp;);<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;modify&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;x,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;val&nbsp;){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;修改&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;MAX&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;com[x]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;val;&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;low&nbsp;(&nbsp;x&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;quy&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;x&nbsp;){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;查询&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;com[x];&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">^=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;low&nbsp;(&nbsp;x&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;;<br>}<br>inline&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">bool</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;scan_d(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">num)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">//</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">&nbsp;输入&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">bool</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;IsN</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">false</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">==</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">EOF)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">false</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">!=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">0</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">||</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">9</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">))&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">==</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">){&nbsp;IsN</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">true</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;num</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">else</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;num</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">0</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">getchar(),</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">0</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&amp;&amp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">9</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">10</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,num</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">0</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(IsN)&nbsp;num</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">num;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">true</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>}<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;main&nbsp;()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;scan_d&nbsp;(&nbsp;N&nbsp;)&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset&nbsp;(&nbsp;com,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">sizeof</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;com&nbsp;)&nbsp;);&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;N;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scan_d&nbsp;(&nbsp;num[i]&nbsp;);&nbsp;modify&nbsp;(&nbsp;num[i],&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;quy&nbsp;(&nbsp;num[i]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scan_d&nbsp;(&nbsp;M&nbsp;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;M&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">--</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf&nbsp;(&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">%s</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,ask&nbsp;);&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;temp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">switch</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;ask[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">case</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">Q</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;:&nbsp;cout&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;endl;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">break</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">case</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">R</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">'</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;:&nbsp;scan_d&nbsp;(&nbsp;x&nbsp;),&nbsp;scan_d&nbsp;(&nbsp;y&nbsp;);&nbsp;temp&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;num[x];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">while</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;y&nbsp;)&nbsp;{&nbsp;num[x]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;num[x</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num[x]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;temp&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">?</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;sum&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">--</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;:&nbsp;num[x]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">==</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;temp&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">?</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">:&nbsp;sum</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;;&nbsp;x&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num[y]&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;temp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">break</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br>}<br></span></div></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span></font></span></div></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/125283.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-30 16:18 <a href="http://www.cppblog.com/MiYu/archive/2010/08/30/125283.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 3450 HDU 3450 Counting Sequences ACM 3450 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/08/30/125251.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Mon, 30 Aug 2010 01:59:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/30/125251.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125251.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/30/125251.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125251.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125251.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">题目地址:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><span class="Apple-tab-span" style="white-space: pre; ">	</span><a href="http://acm.hdu.edu.cn/showproblem.php?pid=3450"><span class="Apple-tab-span" style="white-space: pre; ">http://acm.hdu.edu.cn/showproblem.php?pid=3450</span>&nbsp;</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">题目描述:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><div id="cnblogs_code_open_fb296ff0-eb92-437c-960c-72458cd53a64"><div><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><font  color="#800080"><span  style="font-family: 'Times New Roman'; color: rgb(0, 0, 0); line-height: normal; font-size: medium; "><h1 style="color: rgb(26, 92, 200); text-align: center; ">Counting Sequences</h1><font><strong><span style="font-family: Arial; font-size: 12px; font-weight: bold; color: green; ">Time Limit: 2000/1000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 32768/65536 K (Java/Others)<br>Total Submission(s): 312&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 105<br></span></strong></font><br><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">For a set of sequences of integers｛a1，a2，a3，...an｝, we define a sequence｛ai1,ai2,ai3...aik｝in which 1&lt;=i1&lt;i2&lt;i3&lt;...&lt;ik&lt;=n, as the sub-sequence of ｛a1，a2，a3，...an｝. It is quite obvious that a sequence with the length n has 2^n sub-sequences. And for a sub-sequence｛ai1,ai2,ai3...aik｝，if it matches the following qualities: k &gt;= 2, and the neighboring 2 elements have the difference not larger than d, it will be defined as a Perfect Sub-sequence. Now given an integer sequence, calculate the number of its perfect sub-sequence.<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">Multiple test cases The first line will contain 2 integers n, d（2&lt;=n&lt;=100000，1&lt;=d=&lt;=10000000） The second line n integers, representing the suquence</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">The number of Perfect Sub-sequences mod 9901<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">4 2
1 3 7 5</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">4</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><div><br></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目分析 :<span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>(&nbsp;线段树 || 树状数组 ) + DP&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>可以使用线段树 或 树状数组来做, &nbsp;可惜本人现在还不会线段树, 下面是 树状数组 解题的分析 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">因为题目要求的 是 k &gt;= 2 , &nbsp;而且&nbsp;<span style="font-family: 'Courier New'; color: red; ">相邻元素 之差的绝对值 不大于 d</span>, &nbsp;这里纠结了很久 , 一直没明白 "<span  style="font-family: 'Courier New'; font-size: 14px; ">and the</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span  style="font-family: 'Courier New'; font-size: 14px; ">neighboring 2 elements have the difference not larger than d" 这句话的意思, 英语水平太差了. &nbsp;最后在</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span  style="font-family: 'Courier New'; font-size: 14px; ">Amb 的解释下才明白, &nbsp;0rz........... &nbsp;</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span  style="font-family: 'Courier New'; font-size: 14px; ">为了使问题简单化, 我们不凡讨论 k &gt;= 1 的情况:</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; ">	</span>&nbsp;因为 输入数据的范围比较大, &nbsp;<span  style="font-family: 'Courier New'; font-size: 14px; ">2&lt;=n&lt;=100000，1&lt;=d=&lt;=10000000 , 所以先要将 数据排序后 用&nbsp;</span><span  style="font-family: 'Courier New'; font-size: 14px; color: red; ">hash[max]</span><span  style="font-family: 'Courier New'; font-size: 14px; ">&nbsp;做离散化处理.</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">当然, 原数组需要另开一个数组保存起来. 处理完成后, &nbsp;我们可以这样理解 : &nbsp;&nbsp;用树状数组&nbsp;<span style="font-family: 'Courier New'; color: red; ">com[pos]<span  style="font-family: 'Times New Roman'; color: rgb(0, 0, 0); ">, ( pos 为 num 在 hash 数组中的位</span></span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">置 ) . 记录 能与 num 构成 完美串的个数. &nbsp; &nbsp;&nbsp;初始状态的 完美子串 为0, 每次加入一个数, 那么这个数 num 与 比他小且差不超过d的第一</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">个数&nbsp;而且与 比他大且差不超过d的第一个数 &nbsp;构成完美串 . 更新树状数组. 最后求出所有的子串和 sum, &nbsp;因为算法是以 k &gt;= 1 来做的,而</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目要求 k &gt;= 2, 也就是说, 对 N 个数, 就多计算了 N 次, 因此 (sum - N ) % 9901 即为所求. ( 别忘了MOD....... )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">代码如下 :</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font  color="#800080"><div>/*</div><div>MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</div><div>Author By : MiYu</div><div>Test &nbsp; &nbsp; &nbsp;: 1</div><div>Program &nbsp; : 3450</div><div>*/</div><div><br></div><div>#include &lt;iostream&gt;</div><div>#include &lt;algorithm&gt;</div><div>using namespace std;</div><div>const int MAX = 100010;</div><div>const int MOD = 9901;</div><div>int nCount = 0;</div><div>int com[MAX];</div><div>int hash[MAX];</div><div>int num1[MAX];</div><div>int num2[MAX];</div><div>int N,D;</div><div>inline int low ( int x ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; return x &amp; ( -x );</div><div>}</div><div>void modify ( int x, int val ){ &nbsp; &nbsp; // 修改&nbsp;</div><div>&nbsp;&nbsp; &nbsp; while ( x &lt;= nCount ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;com[x] += val; x += low ( x );</div><div>&nbsp;&nbsp; &nbsp; } &nbsp; &nbsp;&nbsp;</div><div>}</div><div>int quy ( int x ){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 查询&nbsp;</div><div>&nbsp;&nbsp; &nbsp;int sum = 0;</div><div>&nbsp;&nbsp; &nbsp;while ( x &gt; 0 ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += com[x]; x -= low ( x );</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;return sum ;</div><div>}</div><div>int find1 ( int val ){ &nbsp; &nbsp; &nbsp; &nbsp; // 二分 找 &lt;= val 的第一个数&nbsp;</div><div>&nbsp;&nbsp; &nbsp;int beg = 1, end = nCount; int mid = ( beg + end ) &gt;&gt; 1; int res = mid;</div><div>&nbsp;&nbsp; &nbsp;while ( beg &lt;= end ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( hash[mid] &lt;= val ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;beg = mid + 1; res = mid;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end = mid - 1;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mid = ( beg + end ) &gt;&gt; 1;</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;return res;</div><div>}</div><div>int find2 ( int val ){ &nbsp; &nbsp; &nbsp; &nbsp; // 二分 找 &lt;= val 的第一个数&nbsp;</div><div>&nbsp;&nbsp; &nbsp;int beg = 1, end = nCount; int mid = ( beg + end ) &gt;&gt; 1; int res = mid;</div><div>&nbsp;&nbsp; &nbsp;while ( beg &lt;= end ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( hash[mid] &lt; val ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;beg = mid + 1;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end = mid - 1; res = mid;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mid = ( beg + end ) &gt;&gt; 1;</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;return res;</div><div>}</div><div>inline bool scan_d(int &amp;num) &nbsp; &nbsp; &nbsp;// 输入&nbsp;</div><div>{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;char in;bool IsN=false;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;in=getchar();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in==EOF) return false;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in!='-'&amp;&amp;(in&lt;'0'||in&gt;'9')) in=getchar();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in=='-'){ IsN=true;num=0;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;else num=in-'0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in=getchar(),in&gt;='0'&amp;&amp;in&lt;='9'){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num*=10,num+=in-'0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(IsN) num=-num;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return true;</div><div>}</div><div>int main ()</div><div>{</div><div>&nbsp;&nbsp; &nbsp;while ( scan_d ( N ) &amp;&amp; scan_d ( D ) ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i &lt; N; ++ i ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scan_d ( num1[i] ); &nbsp;num2[i] = num1[i]; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sort ( num1, num1 + N );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hash[1] = num1[0]; &nbsp;nCount = 1;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 1; i &lt; N; ++ i ){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 数据 离散化&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( num1[i] != num1[i-1] ) hash[++nCount] = num1[i];</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memset ( com, 0, sizeof ( com ) );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i &lt; N; ++ i ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int pos = find1 ( num2[i] ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 找当前元素的 hash 下标 &nbsp;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int pre = find1 ( num2[i] + D ); &nbsp; &nbsp; &nbsp; &nbsp; // 找 &lt;= &nbsp;num2[i] + D 的第一个数&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int tail = find2 ( num2[i] - D ); &nbsp; &nbsp; &nbsp; &nbsp;// 找 &gt;= num2[i] - D &nbsp;的第一个数&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int val = quy ( pre ) - quy ( tail - 1 ) + 1; &nbsp;// 区间 [ num2[i] - D, num2[i] + D ] 的元素个数 + 新增的一个元素&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modify ( pos, val % MOD );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout &lt;&lt; ( quy ( nCount ) - N &nbsp;) % MOD &lt;&lt; endl; &nbsp;//以 一个元素 的 [ elem - D, elem + D ] 计算, 题目要求 k &gt;= 2, 减掉 N 个&nbsp;</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;return 0;</div><div>}</div><div><br></div></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span></font></span></div></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/125251.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-30 09:59 <a href="http://www.cppblog.com/MiYu/archive/2010/08/30/125251.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>线段树入门 (zz)</title><link>http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sun, 29 Aug 2010 13:22:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125199.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125199.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125199.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 从简单说起，线段树其实可以理解成一种特殊的二叉树。但是这种二叉树较为平衡，和静态二叉树一样，都是提前已经建立好的树形结构。针对性强，所以效率要高。这里又想到了一句题外话：动态和静态的差别。动态结构较为灵活，但是速度较慢；静态结构节省内存，速度较快。接着回到线段树上来，线段树是建立在线段的基础上，每个结点都代表了一条线段&nbsp;[a , b]。长度为1的线段成为元线段。非元线段都有两个子结点，左...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/125199.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-29 21:22 <a href="http://www.cppblog.com/MiYu/archive/2010/08/29/125199.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 2642 HDU 2642 Stars ACM 2642 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/08/29/125126.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Sun, 29 Aug 2010 01:30:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/29/125126.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/125126.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/29/125126.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/125126.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/125126.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目地址:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;&nbsp;<a href="http://acm.hdu.edu.cn/showproblem.php?pid=2642">http://acm.hdu.edu.cn/showproblem.php?pid=2642</a>&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目描述:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font  color="#800080"><span  style="font-family: 'Times New Roman'; color: rgb(0, 0, 0); line-height: normal; font-size: medium; "><h1 style="color: rgb(26, 92, 200); text-align: center; ">Stars</h1><font><strong><span style="font-family: Arial; font-size: 12px; font-weight: bold; color: green; ">Time Limit: 5000/2000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 32768/65536 K (Java/Others)<br>Total Submission(s): 166&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 66<br></span></strong></font><br><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">Yifenfei is a romantic guy and he likes to count the stars in the sky.<br>To make the problem easier,we considerate the sky is a two-dimension plane.Sometimes the star will be bright and sometimes the star will be dim.At first,there is no bright star in the sky,then some information will be given as "B x y" where 'B' represent bright and x represent the X coordinate and y represent the Y coordinate means the star at (x,y) is bright,And the 'D' in "D x y" mean the star at(x,y) is dim.When get a query as "Q X1 X2 Y1 Y2",you should tell Yifenfei how many bright stars there are in the region correspond X1,X2,Y1,Y2.<br><br>There is only one case.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">The first line contain a M(M &lt;= 100000), then M line followed.<br>each line start with a operational character.<br>if the character is B or D,then two integer X,Y (0 &lt;=X,Y&lt;= 1000)followed.<br>if the character is Q then four integer X1,X2,Y1,Y2(0 &lt;=X1,X2,Y1,Y2&lt;= 1000) followed.<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">For each query,output the number of bright stars in one line.<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">5
B 581 145
B 581 145
Q 0 600 0 200
D 581 145
Q 0 600 0 200</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">1
0</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><div><br></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目分析 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;与 HDU 1892 基本相同的一道题, &nbsp;只是矩阵数组的值被限定在 [0,1] , &nbsp; 是一道 二维 树状数组的 裸题, &nbsp;直接 模板, 加上对题目数据做一些</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">处理即可.</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">代码如下 :</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); "><div>/*</div><div>MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</div><div>Author By : MiYu</div><div>Test &nbsp; &nbsp; &nbsp;: 1</div><div>Program &nbsp; : 2642</div><div>*/</div><div><br></div><div>#include &lt;iostream&gt;</div><div>#include &lt;algorithm&gt;</div><div>using namespace std;</div><div>#define lowbit(x) (x&amp;(-x))</div><div>int T;</div><div>const int MAX = 1001;</div><div>int mat[1002][1002];</div><div>int com[1002][1002];</div><div>void modify ( int x,int y, int n )</div><div>{</div><div>&nbsp;&nbsp; &nbsp; while ( x &lt;= MAX ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int t = y;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ( t &lt;= MAX ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;com[x][t] += n;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t += lowbit(t);&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += lowbit(x);</div><div>&nbsp;&nbsp; &nbsp; }&nbsp;</div><div>}</div><div>int quy ( int x, int y )</div><div>{</div><div>&nbsp;&nbsp; &nbsp; int sum = 0;</div><div>&nbsp;&nbsp; &nbsp; while ( x &gt; 0 ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int t = y;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ( t &gt; 0 ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum += com[x][t];</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t ^= lowbit(t);&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x ^= lowbit(x);</div><div>&nbsp;&nbsp; &nbsp; }&nbsp;</div><div>&nbsp;&nbsp; &nbsp; return sum;&nbsp;</div><div>}</div><div>inline bool scan_d(int &amp;num)&nbsp;</div><div>{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;char in;bool IsN=false;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;in=getchar();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in==EOF) return false;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in!='-'&amp;&amp;(in&lt;'0'||in&gt;'9')) in=getchar();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in=='-'){ IsN=true;num=0;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;else num=in-'0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in=getchar(),in&gt;='0'&amp;&amp;in&lt;='9'){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num*=10,num+=in-'0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(IsN) num=-num;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return true;</div><div>}</div><div>int main ()</div><div>{</div><div>&nbsp;&nbsp; &nbsp; &nbsp;while ( scan_d(T) ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int ca = 1;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char s[5]; &nbsp;int a,b,x,y,m,res,maxx,maxy,minx,miny;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memset ( com, 0, sizeof ( com ) );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ( T -- ) {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf ( "%s",s );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch ( s[0] ){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'Q' : scan_d(a);scan_d(x);scan_d(b);scan_d(y); minx = min ( a,x );miny=min(b,y);maxx=max(a,x)+1;maxy=max(b,y)+1;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = 0; &nbsp;res += quy( maxx,maxy ); res -= quy (maxx,miny); res -= quy(minx,maxy); res += quy(minx,miny);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf ( "%d\n",res ); break; &nbsp;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'B' : scan_d(x);scan_d(y); x++;y++; if ( !mat[x][y] ) { modify ( x,y,1 ); mat[x][y] = 1; } break;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'D' : scan_d(x);scan_d(y); x++;y++; if ( mat[x][y] ) { modify ( x,y,-1 ); mat[x][y] = 0; } break; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;return 0;</div><div>}</div><div><br></div></span></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/125126.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-29 09:30 <a href="http://www.cppblog.com/MiYu/archive/2010/08/29/125126.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>霍夫曼编码</title><link>http://www.cppblog.com/MiYu/archive/2010/08/27/124964.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 27 Aug 2010 11:20:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/27/124964.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124964.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/27/124964.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124964.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124964.html</trackback:ping><description><![CDATA[<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; line-height: 21px; "><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_04f99516-bbed-4a4d-a8ad-2cc60a559eaa" style="vertical-align: middle; padding-right: 5px; "><span class="cnblogs_code_collapse" style="border-right-color: rgb(128, 128, 128); border-right-width: 1px; border-right-style: solid; border-top-color: rgb(128, 128, 128); border-top-width: 1px; border-top-style: solid; border-left-color: rgb(128, 128, 128); border-left-width: 1px; border-left-style: solid; border-bottom-color: rgb(128, 128, 128); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(255, 255, 255); padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px; font-family: 'Courier New'; ">代码</span><div id="cnblogs_code_open_04f99516-bbed-4a4d-a8ad-2cc60a559eaa"><div><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">霍夫曼编码是一种被广泛应用而且非常有效的数据压缩技术，根据待压缩数据的特征，一个可压缩掉20</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%~</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">90</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">%</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">。这里考虑的数据指的是字符串序列。要理解霍夫曼编码，先要理解霍夫曼树，即最优二叉树，是一类带权路径长度最短的树。<br><br>路径是指从树中一个结点到另一个结点之间的通路，路径上的分支数目称为路径长度。<br><br>树的路径长度是从树根到每一个叶子之间的路径长度之和。结点的带权路径长度为从该结点到树根之间的路径长度与该结点权的乘积，树的带权路径长度为树中所有叶子结点的带权路径长度之和.<br><br>霍夫曼树是指所有叶子结点的二叉树中带权路径长度最小的二叉树.<br><br>当给定了n个叶子结点的权值后，构造出的最优二叉树的结点数目m就确定了，即m</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">2n</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,所以可用一维结构数组来存储最优二叉树<br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">#define</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;MAXLEAFNUM&nbsp;50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*最优二叉树中最大叶子树目*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">struct</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;node{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;ch;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">当前结点表示的字符，对于非叶子结点，此域不用</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;weight;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">当前结点的权值</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;parent;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">当前结点的父结点的下标，为0时表示无父结点</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;lchild,rchild;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">当前结点的左，右孩子结点的下标，为0时表示无孩子结点</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>}HuffmanTree[</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;MAXLEAFNUM];<br><br><br>typedef&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">HuffmanCode[MAXLEAFNUM&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">]；<br><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">创建最优二叉树</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">void</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;createHTree(HuffmanTree&nbsp;HT,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">char</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">c,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">w,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;n)<br><br>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">数组c[0..n-1]和w[0..n-1]存放了n个字符及其概率，构造霍夫树HT</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">int</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i,&nbsp;s1,&nbsp;s2;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(n&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">return</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">根据n个权值构造n棵只有根结点的二叉树</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;&nbsp;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].ch&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;c[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].weight&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;w[i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].parent&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;HT[i].lchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;HT[i].rchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(;&nbsp;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">i)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].parent&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].lchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].rchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><br></span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">构造霍夫曼树</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">for</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;(i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">;&nbsp;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">*</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n;&nbsp;i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">++</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">/*</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">从HT[1..i-1]中选择parent为0且weight最小的两棵树，其序号为s1和s2</span><span style="font-family: 'Courier New'; color: rgb(0, 128, 0); ">*/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select(HT,i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,s1,s2);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[s1].parent&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[s2].parent&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;i;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].lchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;s1;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].rchild&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;s2;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HT[i].weight&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;HT[s1].weight&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">+</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;HT[s2].weight;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>}<br>复制代码<br>霍夫曼算法(构造靍夫曼树)<br>　　对应于霍夫曼树的算法也叫做霍夫曼算法。此算法的思想是：<br>　　（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）设给定的一组权值为{W1，W2，W3，&#8230;&#8230;Wn}，据此生成森林F</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">{T1，T2，T3，&#8230;&#8230;Tn},F&nbsp;中的每棵二叉树只有一个带权为W1的根节点（i</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，&#8230;&#8230;n）。<br>　　（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）在F中选取两棵根节点的权值最小和次小的二叉树作为左右构造一棵新的二叉树，新二叉树根节点的权值为其左、右子树根节点的权值之和。<br>　　（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">3</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）在F中删除这两棵最小和次小的二叉树，同时将新生成的二叉树并入森林中。<br>　　（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">4</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）重复（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）（</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">3</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">）过程直到F中只有一棵二叉树为止。<br>霍夫曼树的应用非常广，在不同的应用中叶子节点的权值可以作不同的解释。霍夫曼树应用于信息编码中，权值可以看成某个符号出现的频率；应用到判定过程中，权值可以看成某类数据出现的频率；应用到排序过程中，权值可以看成是已排好次序而等待合并的序列长度等。<br><br><br>应用霍夫曼编码<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;假设有一个包含100&nbsp;000个字符的数据文件要压缩存储。各字符在该文件中的出现频度见表1。仅有6种不同字符出现过，字符a出现了45000次。<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f<br><br>频度（千字）&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">45</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">13</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">12</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">16</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">9</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">5</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>固定代码字&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">001</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">010</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">011</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">100</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">101</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>变长代码字&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">101</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">100</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">111</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1101</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1100</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>表1&nbsp;&nbsp;&nbsp;一个字符编码问题。大小为100&nbsp;000个字符的一个数据文件仅包含字符a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">~</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">f，每个字符出现的频度如表中所示。如果对每个字符赋予一个三位的编码，则该文件可被编码为300000位。如果利用表中的可变长度编码，该文件可被编码为224000位。<br><br>可以用很多种方式来表示这样一个文件。采用固定长度编码，则需要三位二进制数字来表示六个字符：a</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，b</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">001</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">，&#8230;，f</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">101</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">。这种方法需要300&nbsp;000来对整个原文件编码。<br></span></div></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; ">&nbsp;</p>
<img src ="http://www.cppblog.com/MiYu/aggbug/124964.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-27 19:20 <a href="http://www.cppblog.com/MiYu/archive/2010/08/27/124964.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 2227 HDU 2227 Find the nondecreasing subsequences ACM 2227 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/08/27/124917.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Fri, 27 Aug 2010 02:07:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/27/124917.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124917.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/27/124917.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124917.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124917.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目地址:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;&nbsp;<a href="http://acm.hdu.edu.cn/showproblem.php?pid=2227">http://acm.hdu.edu.cn/showproblem.php?pid=2227&nbsp;</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目描述:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); "><span  style="font-family: 'Times New Roman'; color: rgb(0, 0, 0); line-height: normal; font-size: medium; "><h1 style="color: rgb(26, 92, 200); text-align: center; ">Find the nondecreasing subsequences</h1><font><strong><span style="font-family: Arial; font-size: 12px; font-weight: bold; color: green; ">Time Limit: 10000/5000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 32768/32768 K (Java/Others)<br>Total Submission(s): 211&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 80<br></span></strong></font><br><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">The input consists of multiple test cases. Each case begins with a line containing a positive integer n that is the length of the sequence S, the next line contains n integers {s1, s2, s3, ...., sn}, 1 &lt;= n &lt;= 100000, 0 &lt;= si &lt;= 2^31.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">3
1 2 3</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">7</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><div><br></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;题目分析:&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;整整一天的时间, 终于是把这个题A了 &nbsp;,HDU 第一, &nbsp;纪念下.....................</p><table width="100%" border="0" align="center" cellspacing="2"><tbody><tr class="table_text" align="center" style="font-family: Tahoma; font-size: 12px; "><td>1</td><td><a href="http://acm.hdu.edu.cn/userstatus.php?user=lyzardiar" style="color: rgb(26, 92, 200); text-decoration: none; ">HUT-MiYu</a></td><td>375MS</td><td>1824K</td><td>2358B</td><td>G++</td><td><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">2<font  face="Tahoma">010-08-27 09:44:32&nbsp;</font></p></td></tr></tbody></table><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;但也没有什么值得高兴的, &nbsp;题目的思路是 小A 的, &nbsp;0rz....................... &nbsp;现在还是没有理解树状数组是怎么解这一题的, &nbsp;它的思路是怎样的,</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">一点也没明白, &nbsp; 刚开始做的时候, 还以为 输入数据已经是按非递减排序了, 所以直接用 公式 2^n - 1, WA ...<img src="http://www.cnblogs.com/Emoticons/baimantou/134950908.gif"></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">..... &nbsp;问过小A 才发现, 输入数据是 随机的. &nbsp;这时就要像找上升子串一样, 找到它 所有的上升子串. &nbsp;这里用到了 树状数组, 继续理解-ing......</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">先发代码 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font  color="#800080"><div>/*</div><div>MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</div><div>Author By : MiYu</div><div>Test &nbsp; &nbsp; &nbsp;: 1</div><div>Program &nbsp; : 2227&nbsp;</div><div>*/</div><div><br></div><div>#include &lt;iostream&gt;</div><div>#include &lt;algorithm&gt;</div><div>using namespace std;</div><div>#define lowbit(x) (x&amp;(-x))</div><div>int num[100005];</div><div>int numcopy[100005];</div><div>int hash[100005];</div><div>int com[100005];</div><div>int nCount = 1;</div><div>void add ( int x,int k )</div><div>{</div><div>&nbsp;&nbsp; &nbsp; while ( x &lt;= nCount )</div><div>&nbsp;&nbsp; &nbsp; {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; com[x] += k;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( com[x] &gt;= 1000000007 ) com[x] %= 1000000007;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += lowbit(x);&nbsp;</div><div>&nbsp;&nbsp; &nbsp; }&nbsp;</div><div>}</div><div>int sum ( int x )</div><div>{</div><div>&nbsp;&nbsp; &nbsp;int s = 0;</div><div>&nbsp;&nbsp; &nbsp;while ( x &gt; 0 )</div><div>&nbsp;&nbsp; &nbsp;{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s += com[x];</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( s &gt;= 1000000007 ) s %= 1000000007;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x -= lowbit(x);</div><div>&nbsp;&nbsp; &nbsp;}&nbsp;</div><div>&nbsp;&nbsp; &nbsp;return s %= 1000000007;</div><div>}</div><div>int cmp (const void *a, const void *b)</div><div>{</div><div>&nbsp;&nbsp; &nbsp;return *((int*)a) - *((int*)b);&nbsp;</div><div>}</div><div>int sfind ( int x )</div><div>{</div><div>&nbsp;&nbsp; &nbsp;int *p = (int *)bsearch ( &amp;x,hash+1,nCount+1,sizeof ( int ),cmp );&nbsp;</div><div>&nbsp;&nbsp; &nbsp;return p - hash;</div><div>}</div><div>int find(int num){</div><div>&nbsp;&nbsp; &nbsp;int top=1,bottom=nCount,mid=(top+bottom)/2,ans=mid;</div><div>&nbsp;&nbsp; &nbsp;while(num!=hash[ans]){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(hash[mid]&lt;=num){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;top=(ans=mid)+1;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}else{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bottom=mid-1;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;mid=(top+bottom)/2;</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;return ans;</div><div>}</div><div><br></div><div>inline bool scan_d(int &amp;num) &nbsp;//整数输入</div><div>{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;char in;bool IsN=false;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;in=getchar();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in==EOF) return false;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in!='-'&amp;&amp;(in&lt;'0'||in&gt;'9')) in=getchar();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in=='-'){ IsN=true;num=0;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;else num=in-'0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in=getchar(),in&gt;='0'&amp;&amp;in&lt;='9'){</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num*=10,num+=in-'0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(IsN) num=-num;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return true;</div><div>}</div><div>int main ()</div><div>{</div><div>&nbsp;&nbsp; &nbsp;int N;</div><div>&nbsp;&nbsp; &nbsp;while ( scan_d ( N ) )</div><div>&nbsp;&nbsp; &nbsp;{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i != N; ++ i )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scan_d ( num[i] ),numcopy[i] = num[i];</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sort ( num, num + N );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memset ( com,0,sizeof (com) );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nCount = 1;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hash[1] = num[0];</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 1; i &lt; N; ++ i )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( num[i] != num[i-1] )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hash[++nCount] = num[i];&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i &lt; N; ++ i )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int pos = find ( numcopy[i] );&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int res = sum ( pos ) + 1;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add ( pos,res );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout &lt;&lt; sum ( nCount ) &lt;&lt; endl;</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;return 0;</div><div>}</div><div><br></div></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span></span></div></div><p>&#160;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124917.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-27 10:07 <a href="http://www.cppblog.com/MiYu/archive/2010/08/27/124917.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 1541 HDU 1541 Stars ACM 1541 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/08/26/124806.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Thu, 26 Aug 2010 04:33:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/26/124806.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124806.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/26/124806.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124806.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124806.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目地址:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://acm.hdu.edu.cn/showproblem.php?pid=1541">http://acm.hdu.edu.cn/showproblem.php?pid=1541&nbsp;</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目描述:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font  color="#800080"><span  style="font-family: 'Times New Roman'; color: rgb(0, 0, 0); line-height: normal; font-size: medium; "><h1 style="color: rgb(26, 92, 200); text-align: center; ">Stars</h1><font><strong><span style="font-family: Arial; font-size: 12px; font-weight: bold; color: green; ">Time Limit: 2000/1000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 65536/32768 K (Java/Others)<br>Total Submission(s): 857&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 320<br></span></strong></font><br><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.&nbsp;<br><br><img src="http://acm.hdu.edu.cn/data/images/1541-1.jpg" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "><br><br>For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.&nbsp;<br><br>You are to write a program that will count the amounts of the stars of each level on a given map.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">The first line of the input file contains a number of stars N (1&lt;=N&lt;=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0&lt;=X,Y&lt;=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">5
1 1
5 1
7 1
3 3
5 5</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">1
2
1
1
0</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><div><br></div><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font  color="#800080">题目分析 :</font></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; 题目的意思就是计算 星星的左下角 ( 包括 正左, 正下 ) 有多少颗其他的星星, 左下角星星的个数就是这个星星的等级, &nbsp;题目要求输出每个等级星星的个数.&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; 具体做法:&nbsp;&nbsp;&nbsp;<span  style="font-family: Georgia; font-size: 12px; color: rgb(90, 59, 28); letter-spacing: 2px; line-height: 20px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">另外开个数组存储星星的等级，比他小的数的个数作为下标，即等级，每出现一次，数组加一，com数组的大小即为它存储数据的最大值。</span>&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">代码如下 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;/*</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Author By : MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Test &nbsp; &nbsp; &nbsp;: 1</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Program &nbsp; : 1541</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">*/</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><br></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;iostream&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">using namespace std;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">const int MAX = 32005;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#define lowbit(x) ((x)&amp;(-x))</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int com[MAX], res[MAX], N;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">void modify ( int pos, int val ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; while ( pos &lt;= MAX ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;com[pos] += val;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos += lowbit(pos); &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int quy ( int x ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;int sum = 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( x &gt; 0 ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += com[x];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x -= lowbit(x);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return sum;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int main (int x)</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( scanf ( "%d",&amp;N ) != EOF ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memset ( com,0,sizeof (com) ); memset ( res,0,sizeof (res) );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( int i = 1; i &lt;= N; ++ i ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scanf ( "%d%*d", &amp;x );&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x++;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;res[ quy(x) ] ++; &nbsp;modify ( x,1 );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( int i = 0; i &lt; N; ++ i )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf ( "%d\n",res[i] );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><div><br></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124806.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-26 12:33 <a href="http://www.cppblog.com/MiYu/archive/2010/08/26/124806.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 1892 HDU 1892 See you~ ACM 1892 IN HDU </title><link>http://www.cppblog.com/MiYu/archive/2010/08/26/124798.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Thu, 26 Aug 2010 03:48:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/26/124798.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124798.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/26/124798.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124798.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124798.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目地址:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://acm.hdu.edu.cn/showproblem.php?pid=1892">http://acm.hdu.edu.cn/showproblem.php?pid=1892</a>&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目描述:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_75f56976-74c7-4633-8a8d-6c3700b14301" style="vertical-align: middle; padding-right: 5px; "><span class="cnblogs_code_collapse" style="border-right-color: rgb(128, 128, 128); border-right-width: 1px; border-right-style: solid; border-top-color: rgb(128, 128, 128); border-top-width: 1px; border-top-style: solid; border-left-color: rgb(128, 128, 128); border-left-width: 1px; border-left-style: solid; border-bottom-color: rgb(128, 128, 128); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(255, 255, 255); padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px; font-family: 'Courier New'; ">代码</span><div id="cnblogs_code_open_75f56976-74c7-4633-8a8d-6c3700b14301"><div><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">See&nbsp;you</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">~</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br>Time&nbsp;Limit:&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">5000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">/</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">3000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;MS&nbsp;(Java</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory&nbsp;Limit:&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">65535</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">/</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">32768</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;K&nbsp;(Java</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">/</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">Others)<br>Total&nbsp;Submission(s):&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">921</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;Accepted&nbsp;Submission(s):&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">291</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br><br><br>Problem&nbsp;Description<br>Now&nbsp;I&nbsp;am&nbsp;leaving&nbsp;hust&nbsp;acm.&nbsp;In&nbsp;the&nbsp;past&nbsp;two&nbsp;and&nbsp;half&nbsp;years,&nbsp;I&nbsp;learned&nbsp;so&nbsp;many&nbsp;knowledge&nbsp;about&nbsp;Algorithm&nbsp;and&nbsp;Programming,&nbsp;and&nbsp;I&nbsp;met&nbsp;so&nbsp;many&nbsp;good&nbsp;friends.&nbsp;I&nbsp;want&nbsp;to&nbsp;say&nbsp;sorry&nbsp;to&nbsp;Mr,&nbsp;Yin,&nbsp;I&nbsp;must&nbsp;leave&nbsp;now&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">~~&gt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">.</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;~~</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">.&nbsp;I&nbsp;am&nbsp;very&nbsp;sorry,&nbsp;we&nbsp;could&nbsp;not&nbsp;advanced&nbsp;to&nbsp;the&nbsp;World&nbsp;Finals&nbsp;last&nbsp;year.&nbsp;<br>When&nbsp;coming&nbsp;into&nbsp;our&nbsp;training&nbsp;room,&nbsp;a&nbsp;lot&nbsp;of&nbsp;books&nbsp;are&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;my&nbsp;eyes.&nbsp;And&nbsp;every&nbsp;time&nbsp;the&nbsp;books&nbsp;are&nbsp;moving&nbsp;from&nbsp;one&nbsp;place&nbsp;to&nbsp;another&nbsp;one.&nbsp;Now&nbsp;give&nbsp;you&nbsp;the&nbsp;position&nbsp;of&nbsp;the&nbsp;books&nbsp;at&nbsp;the&nbsp;early&nbsp;of&nbsp;the&nbsp;day.&nbsp;And&nbsp;the&nbsp;moving&nbsp;information&nbsp;of&nbsp;the&nbsp;books&nbsp;the&nbsp;day,&nbsp;your&nbsp;work&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">is</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;to&nbsp;tell&nbsp;me&nbsp;how&nbsp;many&nbsp;books&nbsp;are&nbsp;stayed&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;some&nbsp;rectangles.&nbsp;<br>To&nbsp;make&nbsp;the&nbsp;problem&nbsp;easier,&nbsp;we&nbsp;divide&nbsp;the&nbsp;room&nbsp;into&nbsp;different&nbsp;grids&nbsp;and&nbsp;a&nbsp;book&nbsp;can&nbsp;only&nbsp;stayed&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;one&nbsp;grid.&nbsp;The&nbsp;length&nbsp;and&nbsp;the&nbsp;width&nbsp;of&nbsp;the&nbsp;room&nbsp;are&nbsp;less&nbsp;than&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">.&nbsp;I&nbsp;can&nbsp;move&nbsp;one&nbsp;book&nbsp;from&nbsp;one&nbsp;position&nbsp;to&nbsp;another&nbsp;position,&nbsp;take&nbsp;away&nbsp;one&nbsp;book&nbsp;from&nbsp;a&nbsp;position&nbsp;or&nbsp;bring&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;one&nbsp;book&nbsp;and&nbsp;put&nbsp;it&nbsp;on&nbsp;one&nbsp;position.&nbsp;<br>&nbsp;<br><br>Input<br>In&nbsp;the&nbsp;first&nbsp;line&nbsp;of&nbsp;the&nbsp;input&nbsp;file&nbsp;there&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">is</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;an&nbsp;Integer&nbsp;T(</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">T</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">10</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">),&nbsp;which&nbsp;means&nbsp;the&nbsp;number&nbsp;of&nbsp;test&nbsp;cases&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;the&nbsp;input&nbsp;file.&nbsp;Then&nbsp;N&nbsp;test&nbsp;cases&nbsp;are&nbsp;followed.&nbsp;<br>For&nbsp;each&nbsp;test&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">case</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;the&nbsp;first&nbsp;line&nbsp;there&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">is</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;an&nbsp;Integer&nbsp;Q(</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">Q</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">100</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">),&nbsp;means&nbsp;the&nbsp;queries&nbsp;of&nbsp;the&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">case</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">.&nbsp;Then&nbsp;followed&nbsp;by&nbsp;Q&nbsp;queries.&nbsp;<br>There&nbsp;are&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">4</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;kind&nbsp;of&nbsp;queries,&nbsp;sum,&nbsp;add,&nbsp;delete&nbsp;and&nbsp;move.&nbsp;<br>For&nbsp;example:&nbsp;<br>S&nbsp;x1&nbsp;y1&nbsp;x2&nbsp;y2&nbsp;means&nbsp;you&nbsp;should&nbsp;tell&nbsp;me&nbsp;the&nbsp;total&nbsp;books&nbsp;of&nbsp;the&nbsp;rectangle&nbsp;used&nbsp;(x1,y1)</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">-</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">(x2,y2)&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">as</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;the&nbsp;diagonal,&nbsp;including&nbsp;the&nbsp;two&nbsp;points.&nbsp;<br>A&nbsp;x1&nbsp;y1&nbsp;n1&nbsp;means&nbsp;I&nbsp;put&nbsp;n1&nbsp;books&nbsp;on&nbsp;the&nbsp;position&nbsp;(x1,y1)&nbsp;<br>D&nbsp;x1&nbsp;y1&nbsp;n1&nbsp;means&nbsp;I&nbsp;move&nbsp;away&nbsp;n1&nbsp;books&nbsp;on&nbsp;the&nbsp;position&nbsp;(x1,y1),&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;less&nbsp;than&nbsp;n1&nbsp;books&nbsp;at&nbsp;that&nbsp;position,&nbsp;move&nbsp;away&nbsp;all&nbsp;of&nbsp;them.&nbsp;<br>M&nbsp;x1&nbsp;y1&nbsp;x2&nbsp;y2&nbsp;n1&nbsp;means&nbsp;you&nbsp;move&nbsp;n1&nbsp;books&nbsp;from&nbsp;(x1,y1)&nbsp;to&nbsp;(x2,y2),&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">if</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;less&nbsp;than&nbsp;n1&nbsp;books&nbsp;at&nbsp;that&nbsp;position,&nbsp;move&nbsp;away&nbsp;all&nbsp;of&nbsp;them.&nbsp;<br>Make&nbsp;sure&nbsp;that&nbsp;at&nbsp;first,&nbsp;there&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">is</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;one&nbsp;book&nbsp;on&nbsp;every&nbsp;grid&nbsp;and&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">0</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">x1,y1,x2,y2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1000</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">n1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&lt;=</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">100</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">.&nbsp;<br>&nbsp;<br><br>Output<br>At&nbsp;the&nbsp;beginning&nbsp;of&nbsp;each&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">case</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;output&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">Case&nbsp;X:</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">where</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;X&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">is</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;the&nbsp;index&nbsp;of&nbsp;the&nbsp;test&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">case</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">,&nbsp;then&nbsp;followed&nbsp;by&nbsp;the&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">S</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;queries.&nbsp;<br>For&nbsp;each&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">S</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 0); ">"</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;query,&nbsp;just&nbsp;print&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">out</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;the&nbsp;total&nbsp;number&nbsp;of&nbsp;books&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 255); ">in</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;that&nbsp;area.&nbsp;<br>&nbsp;<br><br>Sample&nbsp;Input<br></span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">3</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>S&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>A&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>S&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">3</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>S&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>A&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>S&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>&nbsp;<br><br>Sample&nbsp;Output<br>Case&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">:<br></span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">3</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br>Case&nbsp;</span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">2</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); ">:<br></span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">1</span><span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><br></span><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); ">4</span></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font  color="#800080">题目分析 :</font></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; 一道二维树状数组 的裸题, 只是需要对坐标做些处理即可, 另外, 初始化的时候 原来 com[i][j] = lowbit (i) * lowbit (j);&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/134950908.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WA 好多次, 直接用的modify(i,j,1)</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">好了, &nbsp;直接代码吧, 代码过长, 内存多了一点点 , HDU 第二 &nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif">&nbsp;&nbsp;&nbsp;</p><span  style="font-family: 'Times New Roman'; line-height: normal; font-size: medium; "><table width="100%" border="0" align="center" cellspacing="2"><tbody><tr bgcolor="#D7EBFF" class="table_text" align="center" style="font-family: Tahoma; font-size: 12px; "><td>2</td><td><a href="http://acm.hdu.edu.cn/userstatus.php?user=lyzardiar" style="color: rgb(26, 92, 200); text-decoration: none; ">HUT-MiYu</a></td><td>156MS</td><td>8044K</td><td>3172B</td><td>C++</td></tr></tbody></table></span><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;/*</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Author By : MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Test &nbsp; &nbsp; &nbsp;: 1</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Program &nbsp; : 1892</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">*/</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><br></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;iostream&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;cmath&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;algorithm&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">using namespace std;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#define lowbit(x) (x&amp;(-x))</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int T,N;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">const int MAX = 1001;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int mat[1002][1002];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int com[1002][1002];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">void modify ( int x,int y, int n )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; while ( x &lt;= MAX ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int t = y;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ( t &lt;= MAX ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;com[x][t] += n;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t += lowbit(t);&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += lowbit(x);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int quy ( int x, int y )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; int sum = 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; while ( x &gt; 0 ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int t = y;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ( t &gt; 0 ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum += com[x][t];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t -= lowbit(t);&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x -= lowbit(x);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; return sum;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">inline bool scan_d(int &amp;num)</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;char in;bool IsN=false;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;in=getchar();</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in==EOF) return false;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in!='-'&amp;&amp;(in&lt;'0'||in&gt;'9')) in=getchar();</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(in=='-'){ IsN=true;num=0;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;else num=in-'0';</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while(in=getchar(),in&gt;='0'&amp;&amp;in&lt;='9'){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num*=10,num+=in-'0';</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(IsN) num=-num;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return true;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int main ()</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp;scan_d(T);{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int ca = 1;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while ( T -- ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf ( "Case %d:\n",ca++ );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scan_d(N); &nbsp;char s[5]; &nbsp;int a,b,x,y,m,res,maxx,maxy,minx,miny;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 1; i &lt;= MAX; ++ i )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( int j = 1; j &lt;= MAX; ++ j )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;com[i][j] = lowbit(i) * lowbit(j), mat[i][j] = 1;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 1; i &lt;= N; &nbsp;++ i ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf ( "%s",s );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch ( s[0] ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'S' : scan_d(a);scan_d(b);scan_d(x);scan_d(y); minx = min ( a,x );miny=min(b,y);maxx=max(a,x)+1;maxy=max(b,y)+1;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = 0; &nbsp;res += quy( maxx,maxy ); res -= quy (maxx,miny); res -= quy(minx,maxy); res += quy(minx,miny);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf ( "%d\n",res ); break; &nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'A' : scan_d(x);scan_d(y);scan_d(a);x++;y++; modify ( x,y,a ); mat[x][y] += a; break;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'D' : scan_d(x);scan_d(y);scan_d(a);x++;y++; if ( mat[x][y] &gt;= a ) { modify ( x,y,-a ); mat[x][y] -= a; }</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else &nbsp;{ modify ( x,y,-mat[x][y] ); mat[x][y] = 0; } break; &nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'M' : scan_d(a);scan_d(b);scan_d(x);scan_d(y);scan_d(m);a++;b++;x++;y++; if ( mat[a][b] &gt;= m )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp;mat[a][b] -= m; mat[x][y] += m; modify ( a,b,-m ); modify ( x,y,m ); }</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else { modify ( a,b,-mat[a][b] ); modify ( x,y,mat[a][b] ); mat[x][y] += mat[a][b]; mat[a][b] = 0; } break;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><div><br></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124798.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-26 11:48 <a href="http://www.cppblog.com/MiYu/archive/2010/08/26/124798.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 1166 HDU 1166 敌兵布阵 ACM 1166 IN HDU </title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124725.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 11:54:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124725.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124725.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124725.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124725.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124725.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font  color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目地址:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">　　<a href="http://acm.hdu.edu.cn/showproblem.php?pid=1166">http://acm.hdu.edu.cn/showproblem.php?pid=1166&nbsp;</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目描述:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><span style="font-family: 'Courier New'; color: rgb(128, 0, 128); "><span  style="font-family: 'Times New Roman'; color: rgb(0, 0, 0); line-height: normal; font-size: medium; "><h1 style="color: rgb(26, 92, 200); text-align: center; ">敌兵布阵</h1><font><strong><span style="font-family: Arial; font-size: 12px; font-weight: bold; color: green; ">Time Limit: 2000/1000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 65536/32768 K (Java/Others)<br>Total Submission(s): 4546&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 1818<br></span></strong></font><br><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">C国的死对头A国这段时间正在进行军事演习，所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段，所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动，可能增加或减少若干人手,但这些都逃不过C国的监视。<br>中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:&#8220;Tidy,马上汇报第3个营地到第10个营地共有多少人!&#8221;Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动，而Derek每次询问的段都不一样，所以Tidy不得不每次都一个一个营地的去数，很快就精疲力尽了，Derek对Tidy的计算速度越来越不满:"你个死肥仔，算得这么慢，我炒你鱿鱼!&#8221;Tidy想：&#8220;你自己来算算看，这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!&#8221;无奈之下，Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说：&#8220;死肥仔，叫你平时做多点acm题和看多点算法书，现在尝到苦果了吧!&#8221;Tidy说："我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼，这么算他真的会崩溃的，聪明的读者，你能写个程序帮他完成这项工作吗？不过如果你的程序效率不够高的话，Tidy还是会受到Derek的责骂的.<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">第一行一个整数T，表示有T组数据。<br>每组数据第一行一个正整数N（N&lt;=50000）,表示敌人有N个工兵营地，接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人（1&lt;=ai&lt;=50）。<br>接下来每行有一条命令，命令有4种形式：<br>(1) Add i j,i和j为正整数,表示第i个营地增加j个人（j不超过30）<br>(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人（j不超过30）;<br>(3)Query i j ,i和j为正整数,i&lt;=j，表示询问第i到第j个营地的总人数;<br>(4)End 表示结束，这条命令在每组数据最后出现;<br>每组数据最多有40000条命令<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">对第i组数据,首先输出&#8220;Case i:&#8221;和回车,<br>对于每个Query询问，输出一个整数并回车,表示询问的段中的总人数,这个数最多不超过1000000。<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End </div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">Case 1:
6
33
59</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><div><br></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目分析 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<span  style="font-family: Verdana, Helvetica, Arial, sans-serif; border-collapse: collapse; color: rgb(68, 68, 68); font-size: 14px; line-height: 22px; ">看了 一下午的书,&nbsp;&nbsp;终于有一点明白了树状数组这东西 , 果然还是要做题啊, 一直不明白的地方, 刷了这<br style="word-wrap: break-word; line-height: normal; ">水题后,&nbsp;&nbsp;貌似明白了一点了&nbsp; &nbsp;<img src="http://www.cppleyuan.com/images/smilies/yangcong/95.gif" smilieid="185" border="0" alt="" style="word-wrap: break-word; line-height: normal; ">&nbsp;&nbsp;.&nbsp;</span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; 这是一道纯粹的 树状数组 的题目, 有模板的话直接模板就可以AC了, 不过手打也用不了什么时间.</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">因为查询语句是固定的, 所以不需要用 字符串比较函数, 直接比较首字符就可以了, 刚开始的时候想偷</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">懒, 把 quy 函数写成 quy ( int x, int y ) , 想一次就把结果求出来......还是对树状数组的理解不深刻......从其</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">结构而言, 这种想法就不可能实现. &nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">简单介绍下 树状数组 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; 　　&nbsp;<span style="font-family: 宋体; font-size: 10pt; ">树状数组是一个查询和修改复杂度都为</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">log(n)</span><span style="font-family: 宋体; font-size: 10pt; ">的数据结构，假设数组</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">a[1...n]</span><span style="font-family: 宋体; font-size: 10pt; ">，那么查询</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">a[1] +&nbsp;</span><span style="font-family: 宋体; font-size: 10pt; ">&#8230;&#8230;</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">&nbsp;+ a[i]</span><span style="font-family: 宋体; font-size: 10pt; ">　的时间是</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">log</span><span style="font-family: 宋体; font-size: 10pt; ">级别的，而且是一个在线的数据结构，支持随时修改某个元素的值，复杂度也为</span><span lang="EN-US" style="font-family: 'Courier New'; font-size: 10pt; ">log</span><span style="font-family: 宋体; font-size: 10pt; ">级别。</span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">来观察一下这个图：</span><span lang="EN-US" style="font-family: Arial; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: Arial; font-size: 12pt; ">&nbsp;<o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><font  face="Arial" size="3"><span  style="font-family: 'Courier New'; font-size: 13px; "><img src="http://www.cnblogs.com/images/cnblogs_com/miyu/3.jpg" border="0" alt="" width="462" height="293"><br></span></font></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: Arial; font-size: 12pt; ">&nbsp;<o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">令这棵树的结点编号为</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C1</span><span style="font-family: 宋体; font-size: 10pt; ">，</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C2</span><span style="font-family: 宋体; font-size: 10pt; ">&#8230;&#8230;</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">Cn</span><span style="font-family: 宋体; font-size: 10pt; ">。令每个结点的值为这棵树的值的总和，那么容易发现：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C1 = A1</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C2 = A1 + A2</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C3 = A3</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C4 = A1 + A2 + A3 + A4</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C5 = A5</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C6 = A5 + A6</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C7 = A7</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">&#8230;&#8230;</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">C16 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 + A11 + A12 + A13 + A14 + A15 + A16</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">这里有一个有趣的性质，下午推了一下发现：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">设节点编号为</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">x</span><span style="font-family: 宋体; font-size: 10pt; ">，那么这个节点管辖的区间为</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">2^k</span><span style="font-family: 宋体; font-size: 10pt; ">（其中</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">k</span><span style="font-family: 宋体; font-size: 10pt; ">为</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">x</span><span style="font-family: 宋体; font-size: 10pt; ">二进制末尾</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">0</span><span style="font-family: 宋体; font-size: 10pt; ">的个数）个元素。因为这个区间最后一个元素必然为</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">Ax</span><span style="font-family: 宋体; font-size: 10pt; ">，所以很明显：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">Cn = A(n &#8211; 2^k + 1) +&nbsp;</span><span style="font-family: 宋体; font-size: 10pt; ">&#8230;&#8230;</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">&nbsp;+ An</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">算这个</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">2^k</span><span style="font-family: 宋体; font-size: 10pt; ">有一个快捷的办法，定义一个函数如下即可：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">int lowbit(int x){</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 21.75pt; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">return x &amp; (x ^ (x &#8211; 1));</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">}</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">当想要查询一个</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">SUM(n)</span><span style="font-family: 宋体; font-size: 10pt; ">时，可以依据如下算法即可：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">step1:</span><span style="font-family: 宋体; font-size: 10pt; ">　令</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">sum = 0</span><span style="font-family: 宋体; font-size: 10pt; ">，转第二步；</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">step2:</span><span style="font-family: 宋体; font-size: 10pt; ">　假如</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">n &lt;= 0</span><span style="font-family: 宋体; font-size: 10pt; ">，算法结束，返回</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">sum</span><span style="font-family: 宋体; font-size: 10pt; ">值，否则</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">sum = sum + Cn</span><span style="font-family: 宋体; font-size: 10pt; ">，转第三步；</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">step3:<span style="font-family: 'Courier New'; ">&nbsp;&nbsp;</span></span><span style="font-family: 宋体; font-size: 10pt; ">令</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">n = n &#8211; lowbit(n)</span><span style="font-family: 宋体; font-size: 10pt; ">，转第二步。</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">可以看出，这个算法就是将这一个个区间的和全部加起来，为什么是效率是</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">log(n)</span><span style="font-family: 宋体; font-size: 10pt; ">的呢？以下给出证明：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">n = n &#8211; lowbit(n)</span><span style="font-family: 宋体; font-size: 10pt; ">这一步实际上等价于将</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">n</span><span style="font-family: 宋体; font-size: 10pt; ">的二进制的最后一个</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">1</span><span style="font-family: 宋体; font-size: 10pt; ">减去。而</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">n</span><span style="font-family: 宋体; font-size: 10pt; ">的二进制里最多有</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">log(n)</span><span style="font-family: 宋体; font-size: 10pt; ">个</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">1</span><span style="font-family: 宋体; font-size: 10pt; ">，所以查询效率是</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">log(n)</span><span style="font-family: 宋体; font-size: 10pt; ">的。</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">那么修改呢，修改一个节点，必须修改其所有祖先，最坏情况下为修改第一个元素，最多有</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">log(n)</span><span style="font-family: 宋体; font-size: 10pt; ">的祖先。所以修改算法如下（给某个结点</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">i</span><span style="font-family: 宋体; font-size: 10pt; ">加上</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">x</span><span style="font-family: 宋体; font-size: 10pt; ">）：</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">step1:&nbsp;</span><span style="font-family: 宋体; font-size: 10pt; ">当</span><span lang="EN-US" style="font-family: 宋体; font-size: 10pt; ">i &gt; n</span><span style="font-family: 宋体; font-size: 10pt; ">时，算法结束，否则转第二步；</span><span lang="EN-US" style="font-family: 宋体; font-size: 12pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">step2: Ci = Ci + x</span><span style="font-family: 宋体; font-size: 10pt; ">，</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">&nbsp;i = i + lowbit(i)</span><span style="font-family: 宋体; font-size: 10pt; ">转第一步。</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">i = i +lowbit(i)</span><span style="font-family: 宋体; font-size: 10pt; ">这个过程实际上也只是一个把末尾</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">1</span><span style="font-family: 宋体; font-size: 10pt; ">补为</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">0</span><span style="font-family: 宋体; font-size: 10pt; ">的过程。</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="PL" style="font-family: 宋体; font-size: 10pt; ">//</span><span style="font-family: 宋体; font-size: 10pt; ">修改过程必须满足减法规则！</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p>&nbsp;</o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span style="font-family: 宋体; font-size: 10pt; ">所以整个程序如下：</span><span lang="PL" style="font-family: 宋体; font-size: 10pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 新宋体; font-size: 10pt; color: blue; "></span></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">const int MAX = 50000;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#define lowbit(x) ((x)&amp;(-x))</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int com[ MAX + 1 ],N,T;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">void modify ( int pos, int val ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; while ( pos &lt;= N ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;com[pos] += val;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos = pos + lowbit(pos); &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int quy ( int x ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;int sum = 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( x &gt; 0 ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + com[x];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = x - lowbit(x);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return sum;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><span lang="EN-US" style="font-family: 新宋体; font-size: 10pt; "><o:p></o:p></span></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><font  face="新宋体" size="3"><span  style="font-family: 'Courier New'; font-size: 13px; ">初始化 : &nbsp;　　&nbsp;for ( int i = 1; i &lt;= N; ++ i ){</span></font></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><font  face="'Courier New'" size="3"><span  style="font-family: 'Courier New'; font-size: 13px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf ( "%d",&amp;x );</span></font></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><font  face="'Courier New'" size="3"><span  style="font-family: 'Courier New'; font-size: 13px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modify ( i, x );&nbsp;</span></font></p><p class="MsoNormal" align="left" style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; text-align: left; word-break: break-all; "><font  face="'Courier New'" size="3"><span  style="font-family: 'Courier New'; font-size: 13px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</span></font></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">----------------------------------------------------------------------------------------------------------------------------</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">本题代码如下 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;/*</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Author By : MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Test &nbsp; &nbsp; &nbsp;: 1</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Program &nbsp; : 1166</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">*/</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><br></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;iostream&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">using namespace std;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">const int MAX = 50000;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#define lowbit(x) ((x)&amp;(-x))</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int com[ MAX + 1 ],N,T;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">void modify ( int pos, int val ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; while ( pos &lt;= N ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;com[pos] += val;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos = pos + lowbit(pos); &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int quy ( int x ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;int sum = 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( x &gt; 0 ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + com[x];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = x - lowbit(x);</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return sum;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int main ()</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( scanf ( "%d",&amp;T ) != EOF ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int ca = 1,x,y;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while ( T -- ){ &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf ( "Case %d:\n",ca++ );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf ( "%d",&amp;N );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i &lt;= N; ++ i ) com[i] &amp;= 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 1; i &lt;= N; ++ i ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf ( "%d",&amp;x );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modify ( i, x );&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ask[10];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ( scanf ( "%s",ask ), ask[0] != 'E' ){ &nbsp; &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scanf ( "%d%d",&amp;x,&amp;y );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switch ( ask[0] ){</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'A': modify ( x, y ); break;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'S': modify ( x, -y ); break;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'Q': printf ( "%d\n",quy ( y ) - quy ( x-1 ) ); break;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><div><br></div><p>&#160;</p></span></span></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124725.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 19:54 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124725.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>极度 YM....自己用trie写了个伪 map....( HDU 1075 )</title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124721.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 11:41:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124721.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124721.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124721.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124721.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124721.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font color="#800080"><span style="font-family: Verdana, Helvetica, Arial, sans-serif; color: rgb(68, 68, 68); line-height: 22px; font-size: 14px; border-collapse: collapse; ">这几天一直很YM..... 纠结.....&nbsp;&nbsp;做个水题 1075......WA N次.....YM.<br style="word-wrap: break-word; line-height: normal; ">(&nbsp;&nbsp;很简单的一题,&nbsp;&nbsp;检索输入的串是否存在, 存在就替换输出, 不存在直接输出就可以了 )<br style="word-wrap: break-word; line-height: normal; ">最后无奈, 纯C++ STL..过了, 时间竟然用了 1600+MS 还是 C++交的,&nbsp;&nbsp;G++要3400+MS,&nbsp;&nbsp;不<br style="word-wrap: break-word; line-height: normal; ">明白为什么时间差那么多,&nbsp;&nbsp;用trie 做了次, 250MS...<img src="http://www.cppleyuan.com/images/smilies/yangcong/98.gif" smilieid="188" border="0" alt="" style="word-wrap: break-word; line-height: normal; ">&nbsp; &nbsp;改了好几次效率都上不来&nbsp;&nbsp;<img src="http://www.cppleyuan.com/images/smilies/yangcong/54.gif" smilieid="140" border="0" alt="" style="word-wrap: break-word; line-height: normal; ">&nbsp;....&nbsp;&nbsp;YM 中 把trie 写成了 伪 map ,只有一点简单的功能,<br style="word-wrap: break-word; line-height: normal; ">其他的不想写了.&nbsp;&nbsp;写得好复杂....................<img src="http://www.cppleyuan.com/images/smilies/yangcong/95.gif" smilieid="185" border="0" alt="" style="word-wrap: break-word; line-height: normal; ">&nbsp;<br style="word-wrap: break-word; line-height: normal; ">伪 map 代码如下 :&nbsp;&nbsp;好长.....................</span></font></div><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font color="#800080"><div>/*</div><div>MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</div><div>Author By : MiYu</div><div>Test &nbsp; &nbsp; &nbsp;: 4</div><div>Program &nbsp; : 1075</div><div>*/</div><div><br></div><div>#include &lt;iostream&gt;</div><div>#include &lt;string&gt;</div><div>using namespace std;</div><div>typedef struct dict DIC;</div><div>DIC *root = NULL;</div><div>string ext = "";</div><div>struct dict {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; dict (){ str = "";memset ( child , 0 , sizeof ( child ) ); }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; ~dict () { }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; void del ( DIC * node );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; string &amp; insert ( char *ins,char *key = NULL );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; string &amp; find ( char *ins );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; string &amp; operator [] ( char *a );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; string &amp; operator = ( char *a );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; DIC *child[26];</div><div>&nbsp;&nbsp; &nbsp; &nbsp; string str;&nbsp;</div><div>};</div><div>void dict::del ( DIC * node )</div><div>{</div><div>&nbsp;&nbsp; &nbsp; if ( node == NULL ) return;</div><div>&nbsp;&nbsp; &nbsp; for ( int i = 0; i != 26; ++ i )</div><div>&nbsp;&nbsp; &nbsp; {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( node-&gt;child[i] != NULL ) &nbsp;del ( node-&gt;child[i] );</div><div>&nbsp;&nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp; node-&gt;str = "";</div><div>&nbsp;&nbsp; &nbsp; free ( node ); &nbsp;</div><div>}</div><div>string &amp; dict::operator [] ( char *a )</div><div>{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; string &amp;str = find ( a );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; if ( str == "" )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &nbsp;insert ( a ); &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; return str;</div><div>}</div><div>string &amp; dict::operator = ( char *a )</div><div>{</div><div>&nbsp;&nbsp; &nbsp; &nbsp;return this-&gt;str = a;&nbsp;</div><div>}</div><div>string &amp; dict::insert ( char *ins,char *key )</div><div>{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DIC *cur = this,*now;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int len = strlen ( ins );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( int i = 0; i != len; ++ i )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( cur-&gt;child[ ins[i] - 'a' ] != NULL )&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cur = cur-&gt;child[ ins[i] - 'a' ];</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; now = new DIC;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cur-&gt;child[ ins[i] - 'a' ] = now;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cur = now;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return cur-&gt;str;</div><div>}</div><div>string &amp; dict::find ( char *ins )</div><div>{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DIC *cur = this;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int len = strlen ( ins );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( int i = 0; i != len; ++ i )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( cur-&gt;child[ ins[i] - 'a' ] != NULL )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur = cur-&gt;child[ ins[i] - 'a' ]; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return ext;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return cur-&gt;str;</div><div>}</div><div>char words[3010],temp[12],t[12];</div><div>int main ()</div><div>{</div><div>&nbsp;&nbsp; &nbsp;DIC dict;</div><div>&nbsp;&nbsp; &nbsp;root = &amp;dict;</div><div>&nbsp;&nbsp; &nbsp;scanf ( "%s", t );</div><div>&nbsp;&nbsp; &nbsp;while ( scanf ( "%s", t ), strcmp ( t, "END" ) != 0 )</div><div>&nbsp;&nbsp; &nbsp;{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf ( "%s", temp );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dict[temp] = t;</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;scanf ( "%s", t );</div><div>&nbsp;&nbsp; &nbsp;getchar();</div><div>&nbsp;&nbsp; &nbsp;while ( gets ( words ) &amp;&amp; strcmp ( words, "END" ) != 0 )</div><div>&nbsp;&nbsp; &nbsp;{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memset ( temp, 0, sizeof ( temp ) );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int len = strlen ( words );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0,j = 0; i != len; ++ i )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( isalpha ( words[i] ) )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp[j++] = words[i];</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp[j] = '\0';</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string str = dict[ temp ];</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( str == "" )</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf ( "%s",temp );&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf ( "%s",str.c_str() );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; putchar ( words[i] );</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = 0;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; putchar ( 10 );</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;return 0;</div><div>}</div><div><br></div></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;<font color="#800080">STL 代码如下 : &nbsp;好短....<span style="font-family: 'Courier New'; color: rgb(0, 0, 0); "><img src="http://www.cnblogs.com/Emoticons/baimantou/134950908.gif" alt=""></span></font></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;/*</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Author By : MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Test &nbsp; &nbsp; &nbsp;: 1</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Program &nbsp; : 1075</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">*/</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><br></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;iostream&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;string&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;map&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">using namespace std;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">string words,temp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">map &lt; string , string &gt; mp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int main ()</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;cin &gt;&gt; words;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( cin &gt;&gt; words, words != "END" )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cin &gt;&gt; temp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mp[ temp ] = words;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;cin &gt;&gt; words;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;getchar();</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( getline ( cin, words ) &amp;&amp; words != "END" )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string out = "";</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int len = words.size();</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i != len; ++ i )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( isalpha ( words[i] ) )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out += words[i];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( mp[out] == "" )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &lt;&lt; out;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &lt;&lt; mp[out];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout &lt;&lt; words[i];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = "";&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout &lt;&lt; endl;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><div><br></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124721.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 19:41 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124721.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 1800 HDU 1800 Phone List ACM 1800 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124657.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 03:54:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124657.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124657.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124657.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124657.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124657.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目地址:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;&nbsp;<a href="http://acm.hdu.edu.cn/showproblem.php?pid=1800">&nbsp;http://acm.hdu.edu.cn/showproblem.php?pid=1800&nbsp;</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目描述:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font color="#800080"><span style="font-family: 'Times New Roman'; color: rgb(0, 0, 0); line-height: normal; font-size: medium; "><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><center><img src="http://acm.hdu.edu.cn/data/images/1800-1.jpg" alt="" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; "></center><br>In the year 8888, the Earth is ruled by the PPF Empire . As the population growing , PPF needs to find more land for the newborns . Finally , PPF decides to attack Kscinow who ruling the Mars . Here the problem comes! How can the soldiers reach the Mars ? PPF convokes his soldiers and asks for their suggestions . &#8220;Rush &#8230; &#8221; one soldier answers. &#8220;Shut up ! Do I have to remind you that there isn&#8217;t any road to the Mars from here!&#8221; PPF replies. &#8220;Fly !&#8221; another answers. PPF smiles :&#8220;Clever guy ! Although we haven&#8217;t got wings , I can buy some magic broomsticks from HARRY POTTER to help you .&#8221; Now , it&#8217;s time to learn to fly on a broomstick ! we assume that one soldier has one level number indicating his degree. The soldier who has a higher level could teach the lower , that is to say the former&#8217;s level &gt; the latter&#8217;s . But the lower can&#8217;t teach the higher. One soldier can have only one teacher at most , certainly , having no teacher is also legal. Similarly one soldier can have only one student at most while having no student is also possible. Teacher can teach his student on the same broomstick .Certainly , all the soldier must have practiced on the broomstick before they fly to the Mars! Magic broomstick is expensive !So , can you help PPF to calculate the minimum number of the broomstick needed .<br>For example :&nbsp;<br>There are 5 soldiers (A B C D E)with level numbers : 2 4 5 6 4;<br>One method :<br>C could teach B; B could teach A; So , A B C are eligible to study on the same broomstick.<br>D could teach E;So D E are eligible to study on the same broomstick;<br>Using this method , we need 2 broomsticks.<br>Another method:<br>D could teach A; So A D are eligible to study on the same broomstick.<br>C could teach B; So B C are eligible to study on the same broomstick.<br>E with no teacher or student are eligible to study on one broomstick.<br>Using the method ,we need 3 broomsticks.<br>&#8230;&#8230;<br><br>After checking up all possible method, we found that 2 is the minimum number of broomsticks needed.&nbsp;<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">Input file contains multiple test cases.&nbsp;<br>In a test case,the first line contains a single positive number N indicating the number of soldiers.(0&lt;=N&lt;=3000)<br>Next N lines :There is only one nonnegative integer on each line , indicating the level number for each soldier.( less than 30 digits);<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">For each case, output the minimum number of broomsticks on a single line.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">4
10
20
30
04
5
2
3
4
3
4</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">1
2</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><div><br></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目分析:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">相对来说这题用字典树的效率比较高些.....遗憾的是我现在只会做一些字典树的 水题....对字典树的理解现在还不是很好......所以,呵呵</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">这题我就直接筛选过了........</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">代码如下:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;/*</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Author By : MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Test &nbsp; &nbsp; &nbsp;: 1</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Program &nbsp; : 1800</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">*/</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><br></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;iostream&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;algorithm&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">using namespace std;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int num[3002];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">bool hash[3002];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">bool cmp ( const int &amp;a, const &nbsp;int &amp;b )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; return a &gt; b;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int main ()</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;int N;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( scanf ( "%d", &amp;N ) != EOF )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memset ( num, 0, 12008 );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memset ( hash, 0, 3002 );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i != N; ++ i )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf ( "%d",num + i );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sort ( num, num + N, cmp );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nCount = 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i &lt; N; ++ i )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( !hash[i] )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nCount ++;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int j = i ,k = j+1;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hash[j] = true;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while ( k &lt; N )&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( !hash[k] &amp;&amp; num[k] &lt; num[j] )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hash[k] = true;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = k;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k++;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf ( "%d\n",nCount );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><div><br></div></span></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124657.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 11:54 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124657.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 1247 HDU 1247 Hat’s Words ACM 1247 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124658.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 03:54:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124658.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124658.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124658.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124658.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124658.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自&nbsp;<a href="http://www.cnblogs.com/MiYu"><font color="#000000">______________白白の屋</font></a>&nbsp;&nbsp;<img src="http://www.cnblogs.com/Emoticons/baimantou/223332482.gif" alt="">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目地址 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;&nbsp;<a href="http://acm.hdu.edu.cn/showproblem.php?pid=1247">http://acm.hdu.edu.cn/showproblem.php?pid=1247&nbsp;</a></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目描述:</p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 13px; border-left-color: rgb(204, 204, 204); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; word-break: break-all; overflow-x: auto; overflow-y: auto; "><div><font color="#800080"><span style="font-family: 'Times New Roman'; color: rgb(0, 0, 0); line-height: normal; font-size: medium; "><h1 style="color: rgb(26, 92, 200); text-align: center; ">Hat&#8217;s Words</h1><font><strong><span style="font-family: Arial; font-size: 12px; font-weight: bold; color: green; ">Time Limit: 2000/1000 MS (Java/Others)&nbsp;&nbsp;&nbsp;&nbsp;Memory Limit: 65536/32768 K (Java/Others)<br>Total Submission(s): 1384&nbsp;&nbsp;&nbsp;&nbsp;Accepted Submission(s): 506<br></span></strong></font><br><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">A hat&#8217;s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.<br>You are to find all the hat&#8217;s words in a dictionary.<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.<br>Only one case.<br></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; ">Your output should contain all the hat&#8217;s words, one per line, in alphabetical order.</div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">a
ahat
hat
hatword
hziee
word</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><br><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; padding-top: 0px; padding-right: 14px; padding-bottom: 0px; padding-left: 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat; ">Sample Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 20px; padding-bottom: 0px; padding-left: 20px; font-size: 14px; font-family: 'Times New Roman'; text-align: left; background-repeat: no-repeat repeat; "><pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; "><div style="font-family: 'Courier New', Courier, monospace; ">ahat
hatword</div></pre></div><div class="panel_bottom" style="height: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0% 0%; background-repeat: no-repeat no-repeat; ">&nbsp;</div><div><br></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">题目分析 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;字典树的题目, 这个题的方法比较暴力, &nbsp;在输入的时候将每个单词都加入字典树, &nbsp;并用数组将所有的串都存起来来.</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">在输入完成后, &nbsp;对每个单词进行拆分, 也就是暴力枚举单词不同位置的前后部分, 看在字典树中是否存在, 存在即输出.</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">不过貌似数据比较弱, 说是按字典顺序输出, 但其实他的输入本就是按字典顺序输入的, 所以将排序也面了 , 呵呵.</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;另外,其实这题用STL 做更简单, 当然追求效率除外.</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">trie 代码:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;/*</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Author By : MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Test &nbsp; &nbsp; &nbsp;: 1</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Program &nbsp; : 1247</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">*/</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><br></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;iostream&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;algorithm&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;string&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">using namespace std;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">typedef struct dictor DIC;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">DIC *root = NULL;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">struct dictor {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; dictor (){ exist = false; memset ( child , 0 , sizeof ( child ) ); }</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; void insert ( char *ins );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; bool find ( const char *ins );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">private:</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; DIC *child[26];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; bool exist;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">};</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">void dictor::insert ( char *ins )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DIC *cur = root,*now;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int len = strlen ( ins );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( int i = 0; i &lt; len; ++ i )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( cur-&gt;child[ ins[i] - 'a' ] != NULL )&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cur = cur-&gt;child[ ins[i] - 'a' ];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; now = new DIC;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cur-&gt;child[ ins[i] - 'a' ] = now;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cur = now;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur-&gt;exist = true;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">bool dictor::find ( const char *ins )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DIC *cur = root;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int len = strlen ( ins );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( int i = 0; i &lt; len; ++ i )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( cur-&gt;child[ ins[i] - 'a' ] != NULL )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur = cur-&gt;child[ ins[i] - 'a' ]; &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return cur-&gt;exist;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">char words[50050][100];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">char s1[100],s2[100];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">DIC dict;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int main ()</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;root = &amp;dict;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;int n = 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( scanf ( "%s",words[n] ) != EOF )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dict.insert ( words[n++] );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;for ( int i = 0; i &lt; n; ++ i )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int len = strlen ( words[i] );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( int j = 1; j &lt; len; ++ j )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memset ( s1, 0, sizeof ( s1 ) );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memset ( s2, 0, sizeof ( s2 ) );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;strncpy ( s1, words[i], j );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;strcpy ( s2, words[i]+j );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( dict.find ( s1 ) &amp;&amp; dict.find ( s2 ) )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf ( "%s\n", words[i] );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;} &nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;//system ( "pause" );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><div><br></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">STL &nbsp;代码 :</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;/*</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">MiYu原创, 转帖请注明 : 转载自 ______________白白の屋</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.cnblog.com/MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Author By : MiYu</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Test &nbsp; &nbsp; &nbsp;: 2</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">Program &nbsp; : 1247</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">*/</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "><br></p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;iostream&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;string&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">#include &lt;map&gt;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">using namespace std;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">map &lt; string , int &gt; mp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">string str[50005];</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">int main ()</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;int n = 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;while ( cin &gt;&gt; str[n] ) mp[ str[n++] ] = 1;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;for ( int i = 0; i &lt; n; ++ i )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;unsigned len = str[i].size ();</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( unsigned j = 1; j &lt; len; ++ j )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string s1 ( str[i], 0, j );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string s2 ( str[i], j );</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( mp[s1] == 1 &amp;&amp; mp[s2] == 1 )</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &lt;&lt; str[i] &lt;&lt; endl;&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;}</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;&nbsp; &nbsp;return 0;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">}</p><div><br></div></span></font></div></div><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p><p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; ">&nbsp;</p></span>
<img src ="http://www.cppblog.com/MiYu/aggbug/124658.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 11:54 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124658.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDOJ 1671 HDU 1671 Phone List ACM 1671 IN HDU</title><link>http://www.cppblog.com/MiYu/archive/2010/08/25/124655.html</link><dc:creator>MiYu</dc:creator><author>MiYu</author><pubDate>Wed, 25 Aug 2010 03:39:00 GMT</pubDate><guid>http://www.cppblog.com/MiYu/archive/2010/08/25/124655.html</guid><wfw:comment>http://www.cppblog.com/MiYu/comments/124655.html</wfw:comment><comments>http://www.cppblog.com/MiYu/archive/2010/08/25/124655.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MiYu/comments/commentRss/124655.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MiYu/services/trackbacks/124655.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: MiYu原创, 转帖请注明 : 转载自&nbsp;______________白白の屋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题目地址:&nbsp;&nbsp; &nbsp; &nbsp;http://acm.hdu.edu.cn/showproblem.php?pid=1671&nbsp;题目描述:Phone ListTime Limit: 3000/1000 MS (Jav...&nbsp;&nbsp;<a href='http://www.cppblog.com/MiYu/archive/2010/08/25/124655.html'>阅读全文</a><img src ="http://www.cppblog.com/MiYu/aggbug/124655.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MiYu/" target="_blank">MiYu</a> 2010-08-25 11:39 <a href="http://www.cppblog.com/MiYu/archive/2010/08/25/124655.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>