hdu 1754 I Hate It (线段树)
I Hate It
**Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 53991 Accepted Submission(s): 21180
**
Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input
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
Sample Output
5 6 5 9
Hint
Huge input,the C function scanf() will work better than cin
套的适牛模板.感谢适牛
<span style="color: #008080;"> 1</span> <span style="color: #008000;">/*</span><span style="color: #008000;">************************************************************************
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;"> > File Name: code/hdu/1754.cpp
</span><span style="color: #008080;"> 3</span> <span style="color: #008000;"> > Author: 111qqz
</span><span style="color: #008080;"> 4</span> <span style="color: #008000;"> > Email: rkz2013@126.com
</span><span style="color: #008080;"> 5</span> <span style="color: #008000;"> > Created Time: 2015年10月28日 星期三 20时34分38秒
</span><span style="color: #008080;"> 6</span> <span style="color: #008000;"> ***********************************************************************</span><span style="color: #008000;">*/</span>
<span style="color: #008080;"> 7</span>
<span style="color: #008080;"> 8</span> #include<iostream>
<span style="color: #008080;"> 9</span> #include<iomanip>
<span style="color: #008080;"> 10</span> #include<cstdio>
<span style="color: #008080;"> 11</span> #include<algorithm>
<span style="color: #008080;"> 12</span> #include<cmath>
<span style="color: #008080;"> 13</span> #include<cstring>
<span style="color: #008080;"> 14</span> #include<<span style="color: #0000ff;">string</span>>
<span style="color: #008080;"> 15</span> #include<map>
<span style="color: #008080;"> 16</span> #include<<span style="color: #0000ff;">set</span>>
<span style="color: #008080;"> 17</span> #include<queue>
<span style="color: #008080;"> 18</span> #include<vector>
<span style="color: #008080;"> 19</span> #include<stack>
<span style="color: #008080;"> 20</span> #include<cctype>
<span style="color: #008080;"> 21</span>
<span style="color: #008080;"> 22</span> <span style="color: #0000ff;">#define</span> yn hez111qqz
<span style="color: #008080;"> 23</span> <span style="color: #0000ff;">#define</span> j1 cute111qqz
<span style="color: #008080;"> 24</span> <span style="color: #0000ff;">#define</span> ms(a,x) memset(a,x,sizeof(a))
<span style="color: #008080;"> 25</span> <span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> std;
</span><span style="color: #008080;"> 26</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> dx4[<span style="color: #800080;">4</span>]={<span style="color: #800080;">1</span>,<span style="color: #800080;">0</span>,<span style="color: #800080;">0</span>,-<span style="color: #800080;">1</span><span style="color: #000000;">};
</span><span style="color: #008080;"> 27</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> dy4[<span style="color: #800080;">4</span>]={<span style="color: #800080;">0</span>,-<span style="color: #800080;">1</span>,<span style="color: #800080;">1</span>,<span style="color: #800080;">0</span><span style="color: #000000;">};
</span><span style="color: #008080;"> 28</span> typedef <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span><span style="color: #000000;"> LL;
</span><span style="color: #008080;"> 29</span> typedef <span style="color: #0000ff;">double</span><span style="color: #000000;"> DB;
</span><span style="color: #008080;"> 30</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> inf = <span style="color: #800080;">0x3f3f3f3f</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 31</span> <span style="color: #0000ff;">#define</span> lson l,m,rt<<1
<span style="color: #008080;"> 32</span> <span style="color: #0000ff;">#define</span> rson m+1 , r , rt<<1|1
<span style="color: #008080;"> 33</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> N=2E5+<span style="color: #800080;">7</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 34</span>
<span style="color: #008080;"> 35</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> n,m;
</span><span style="color: #008080;"> 36</span> <span style="color: #0000ff;">int</span> tree[N<<<span style="color: #800080;">2</span><span style="color: #000000;">];
</span><span style="color: #008080;"> 37</span> <span style="color: #0000ff;">void</span> PushUp( <span style="color: #0000ff;">int</span><span style="color: #000000;"> rt)
</span><span style="color: #008080;"> 38</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 39</span> tree[rt] = max(tree[rt<<<span style="color: #800080;">1</span>],tree[rt<<<span style="color: #800080;">1</span>|<span style="color: #800080;">1</span><span style="color: #000000;">]);
</span><span style="color: #008080;"> 40</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 41</span> <span style="color: #0000ff;">void</span> build ( <span style="color: #0000ff;">int</span> l,<span style="color: #0000ff;">int</span> r,<span style="color: #0000ff;">int</span><span style="color: #000000;"> rt)
</span><span style="color: #008080;"> 42</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 43</span> <span style="color: #0000ff;">if</span> (l==<span style="color: #000000;">r)
</span><span style="color: #008080;"> 44</span> <span style="color: #000000;"> {
</span><span style="color: #008080;"> 45</span> scanf(<span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>,&<span style="color: #000000;">tree[rt]);
</span><span style="color: #008080;"> 46</span> <span style="color: #0000ff;">return</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 47</span> <span style="color: #000000;"> }
</span><span style="color: #008080;"> 48</span> <span style="color: #0000ff;">int</span> m = (l+r) >> <span style="color: #800080;">1</span><span style="color: #000000;"> ;
</span><span style="color: #008080;"> 49</span> <span style="color: #000000;"> build (lson);
</span><span style="color: #008080;"> 50</span> <span style="color: #000000;"> build(rson);
</span><span style="color: #008080;"> 51</span> <span style="color: #000000;"> PushUp(rt);
</span><span style="color: #008080;"> 52</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 53</span>
<span style="color: #008080;"> 54</span> <span style="color: #0000ff;">void</span> update( <span style="color: #0000ff;">int</span> p,<span style="color: #0000ff;">int</span> sc,<span style="color: #0000ff;">int</span> l,<span style="color: #0000ff;">int</span> r,<span style="color: #0000ff;">int</span><span style="color: #000000;"> rt)
</span><span style="color: #008080;"> 55</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 56</span> <span style="color: #0000ff;">if</span> (l==<span style="color: #000000;">r)
</span><span style="color: #008080;"> 57</span> <span style="color: #000000;"> {
</span><span style="color: #008080;"> 58</span> tree[rt] =<span style="color: #000000;"> sc;
</span><span style="color: #008080;"> 59</span> <span style="color: #0000ff;">return</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 60</span> <span style="color: #000000;"> }
</span><span style="color: #008080;"> 61</span> <span style="color: #0000ff;">int</span> m =(l+r)>><span style="color: #800080;">1</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 62</span> <span style="color: #0000ff;">if</span> (p<=<span style="color: #000000;">m) update(p,sc,lson);
</span><span style="color: #008080;"> 63</span> <span style="color: #0000ff;">else</span><span style="color: #000000;"> update(p,sc,rson);
</span><span style="color: #008080;"> 64</span> <span style="color: #000000;"> PushUp(rt);
</span><span style="color: #008080;"> 65</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 66</span>
<span style="color: #008080;"> 67</span> <span style="color: #0000ff;">int</span> query(<span style="color: #0000ff;">int</span> L ,<span style="color: #0000ff;">int</span> R, <span style="color: #0000ff;">int</span> l,<span style="color: #0000ff;">int</span> r,<span style="color: #0000ff;">int</span><span style="color: #000000;"> rt)
</span><span style="color: #008080;"> 68</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 69</span> <span style="color: #0000ff;">if</span> (L<=l&&r;<=<span style="color: #000000;">R)
</span><span style="color: #008080;"> 70</span> <span style="color: #000000;"> {
</span><span style="color: #008080;"> 71</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> tree[rt];
</span><span style="color: #008080;"> 72</span> <span style="color: #000000;"> }
</span><span style="color: #008080;"> 73</span> <span style="color: #0000ff;">int</span> m = (l+r)>><span style="color: #800080;">1</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 74</span> <span style="color: #0000ff;">int</span> ret = <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 75</span> <span style="color: #0000ff;">if</span> (L<=<span style="color: #000000;">m)
</span><span style="color: #008080;"> 76</span> <span style="color: #000000;"> {
</span><span style="color: #008080;"> 77</span> <span style="color: #0000ff;">int</span> res =<span style="color: #000000;"> query(L,R,lson);
</span><span style="color: #008080;"> 78</span> ret =<span style="color: #000000;"> max(ret,res);
</span><span style="color: #008080;"> 79</span> <span style="color: #000000;"> }
</span><span style="color: #008080;"> 80</span> <span style="color: #0000ff;">if</span> (R><span style="color: #000000;">m)
</span><span style="color: #008080;"> 81</span> <span style="color: #000000;"> {
</span><span style="color: #008080;"> 82</span> <span style="color: #0000ff;">int</span> res =<span style="color: #000000;"> query(L,R,rson);
</span><span style="color: #008080;"> 83</span> ret =<span style="color: #000000;"> max(ret,res);
</span><span style="color: #008080;"> 84</span> <span style="color: #000000;"> }
</span><span style="color: #008080;"> 85</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> ret;
</span><span style="color: #008080;"> 86</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 87</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> main()
</span><span style="color: #008080;"> 88</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 89</span> <span style="color: #000000;"> #ifndef ONLINE_JUDGE
</span><span style="color: #008080;"> 90</span> freopen(<span style="color: #800000;">"</span><span style="color: #800000;">in.txt</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">r</span><span style="color: #800000;">"</span><span style="color: #000000;">,stdin);
</span><span style="color: #008080;"> 91</span> <span style="color: #0000ff;">#endif</span>
<span style="color: #008080;"> 92</span>
<span style="color: #008080;"> 93</span> <span style="color: #0000ff;">while</span> (scanf(<span style="color: #800000;">"</span><span style="color: #800000;">%d %d</span><span style="color: #800000;">"</span>,&n;,&m;)!=<span style="color: #000000;">EOF)
</span><span style="color: #008080;"> 94</span> <span style="color: #000000;"> {
</span><span style="color: #008080;"> 95</span> build(<span style="color: #800080;">1</span>,n,<span style="color: #800080;">1</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 96</span> <span style="color: #0000ff;">while</span> (m--<span style="color: #000000;">)
</span><span style="color: #008080;"> 97</span> <span style="color: #000000;"> {
</span><span style="color: #008080;"> 98</span> <span style="color: #0000ff;">char</span> opt[<span style="color: #800080;">10</span><span style="color: #000000;">];
</span><span style="color: #008080;"> 99</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> u,v;
</span><span style="color: #008080;">100</span> scanf(<span style="color: #800000;">"</span><span style="color: #800000;">%s%d%d</span><span style="color: #800000;">"</span>,opt,&u;,&<span style="color: #000000;">v);
</span><span style="color: #008080;">101</span> <span style="color: #0000ff;">if</span> (opt[<span style="color: #800080;">0</span>]==<span style="color: #800000;">'</span><span style="color: #800000;">Q</span><span style="color: #800000;">'</span>) printf(<span style="color: #800000;">"</span><span style="color: #800000;">%dn</span><span style="color: #800000;">"</span>,query(u,v,<span style="color: #800080;">1</span>,n,<span style="color: #800080;">1</span><span style="color: #000000;">));
</span><span style="color: #008080;">102</span> <span style="color: #0000ff;">else</span> update (u,v,<span style="color: #800080;">1</span>,n,<span style="color: #800080;">1</span><span style="color: #000000;">);
</span><span style="color: #008080;">103</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">104</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">105</span>
<span style="color: #008080;">106</span>
<span style="color: #008080;">107</span> <span style="color: #000000;"> #ifndef ONLINE_JUDGE
</span><span style="color: #008080;">108</span> <span style="color: #000000;"> fclose(stdin);
</span><span style="color: #008080;">109</span> <span style="color: #0000ff;">#endif</span>
<span style="color: #008080;">110</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #008080;">111</span> }
View Code