HDUOJ---1754 I Hate It (線段樹之單點更新查區間最大值)

I Hate It

Time Limit: 9000/3000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33469????Accepted Submission(s): 13168


Problem Description
很多學校流行一種比較的習慣。老師們很喜歡詢問,從某某到某某當中,分數最高的是多少。
這讓很多學生很反感。

不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程序,模擬老師的詢問。當然,老師有時候需要更新某位同學的成績。

?

Input
本題目包含多組測試,請處理到文件結束。
在每個測試的第一行,有兩個正整數 N 和 M ( 0<N<=200000,0<M<5000 ),分別代表學生的數目和操作的數目。
學生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
代碼: 線段樹之單點更新
代碼:
 1 #include<stdio.h>
 2 #define maxn 200001
 3 int aa[maxn<<2];
 4 struct node
 5 {
 6   int lef,rig,maxc;
 7   int mid(){ return lef+((rig-lef)>>1); }
 8 };
 9 node seg[maxn<<2];
10 void build(int left ,int right ,int p)
11 {
12     seg[p].lef=left;
13     seg[p].rig=right;
14     seg[p].maxc=0;
15     while(left==right)
16     {
17       seg[p].maxc=aa[left];
18       return ;
19     }
20     int mid=seg[p].mid();
21     build(left,mid,p<<1);
22     build(mid+1 ,right,p<<1|1);
23     if(seg[p<<1].maxc<seg[p<<1|1].maxc)
24         seg[p].maxc=seg[p<<1|1].maxc;
25     else
26         seg[p].maxc=seg[p<<1].maxc;
27 }
28 void updata(int pos ,int p,int val)
29 {
30     if(seg[p].lef==seg[p].rig)
31          {
32            seg[p].maxc=val ;
33            return ;
34          }
35     int mid=seg[p].mid();
36     if(pos<=mid) updata(pos,p<<1,val);
37     else if(pos>mid) updata(pos,p<<1|1,val);
38     if(seg[p<<1].maxc<seg[p<<1|1].maxc)
39           seg[p].maxc=seg[p<<1|1].maxc;
40     else seg[p].maxc=seg[p<<1].maxc;
41 }
42 int query(int be,int en ,int p)
43 {
44     if(be<=seg[p].lef&&seg[p].rig<=en)
45           return seg[p].maxc;
46     int res2=0,res1=0;
47     int mid=seg[p].mid();
48     if(be<=mid) res1=query(be,en ,p<<1);
49     if(en>mid)  res2=query(be,en ,p<<1|1);
50     if(res1<res2) return res2;
51     else  return res1;
52 }
53 int main()
54 {
55     int nn,mm,i,j,sa,sb;
56     char str[3];
57     while(scanf("%d%d",&nn,&mm)!=EOF)
58     {
59         for(i=1;i<=nn;i++)
60           scanf("%d",&aa[i]);
61           build(1,nn,1);
62         while(mm--)
63         {
64             scanf("%s%d%d",str,&sa,&sb);
65             if(*str=='Q')
66               printf("%d\n",query(sa,sb,1));
67             else  updata(sa,1,sb);
68         }
69     }
70   return 0;
71 }
View Code

?

代碼:

 1 #include<cstdio>
 2 #include<cstring>
 3 const int maxn=200005;
 4 int aa[maxn];
 5 struct node
 6 {
 7     int lef,rig;
 8     int max;
 9     int mid(){
10       return lef+((rig-lef)>>1);
11     }
12 };
13  node str[maxn<<2];
14 inline int max(int a,int b)
15 {
16    return a>b?a:b;
17 }
18  void Build(int left ,int right,int pos)
19  {
20     str[pos].lef=left;
21     str[pos].rig=right;
22     str[pos].max=0;
23     if(left==right){
24       str[pos].max=aa[left];
25       return ;
26     }
27     int mid=str[pos].mid();
28     Build(left,mid,pos<<1);
29     Build(mid+1,right,pos<<1|1);
30     str[pos].max = max( str[pos<<1].max , str[pos<<1|1].max );
31  }
32  void Update(int ps,int pos,int val)
33  {
34      if(str[pos].lef==ps&&str[pos].rig==ps){
35         str[pos].max=val;    //將這個數值更新
36        return ;
37      }
38      int mid=str[pos].mid();
39      if(mid>=ps) Update(ps,pos<<1,val);
40      else  if(mid<ps)  Update(ps,pos<<1|1,val);
41      str[pos].max=max(str[pos<<1].max,str[pos<<1|1].max);
42  }
43  int  Query(int st,int en,int pos)
44  {
45     if(str[pos].lef>=st&&str[pos].rig<=en){
46         return str[pos].max;
47      }
48     int mid=str[pos].mid();
49     int p1=0,p2=0;
50     if(mid>=st) p1=Query(st,en,pos<<1);
51     if(mid<en)  p2=Query(st,en,pos<<1|1);
52     return max(p1,p2);
53  }
54 
55  int main()
56  {
57    int n , m,cc,dd;
58    char ss[2];
59    while(scanf("%d%d",&n,&m)!=EOF)
60    {
61       for(int i=1;i<=n;i++)
62        scanf("%d",aa+i);
63        Build(1,n,1);
64        while(m--)
65        {
66         scanf("%s %d%d",ss,&cc,&dd);
67         if(*ss=='Q')
68           printf("%d\n",Query(cc,dd,1));
69         else Update(cc,1,dd);
70        }
71    }
72    return 0;
73  }
View Code

?

轉載于:https://www.cnblogs.com/gongxijun/p/3686014.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377114.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377114.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377114.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

WEG的完整形式是什么?

WEG&#xff1a;邪惡邪惡的咧嘴 (WEG: Wicked Evil Grin) WEG is an abbreviation of "Wicked Evil Grin". WEG是“ Wicked Evil Grin”的縮寫 。 It is also known as EWG (Evil Wicked Grin) "Grin" refers to a broad smile. "Wicked" refer…

C# 把數字轉換成鏈表

例如&#xff1a;123456轉換成 1 -> 2 -> 3-> 4-> 5-> 6 View Code static LinkedList<int> CovertIntToLinkedList(int num){Stack<int> stack new Stack<int>();LinkedList<int> result new LinkedList<int>();while (num!0…

《MySQL 8.0.22執行器源碼分析(4.1)Item_sum類以及聚合》

Item_sum類用于SQL聚合函數的特殊表達式基類。 這些表達式是在聚合函數&#xff08;sum、max&#xff09;等幫助下形成的。item_sum類也是window函數的基類。 聚合函數&#xff08;Aggregate Function&#xff09;實現的大部分代碼在item_sum.h和item_sum.cc 聚合函數限制 不…

Java 性能優化實戰記錄(2)---句柄泄漏和監控

前言: Java不存在內存泄漏, 但存在過期引用以及資源泄漏. (個人看法, 請大牛指正) 這邊對文件句柄泄漏的場景進行下模擬, 并對此做下簡單的分析.如下代碼為模擬一個服務進程, 忽略了句柄關閉, 造成不能繼續正常服務的小場景. 1 public class FileHandleLeakExample {2 3 p…

什么是Java文件?

Java文件 (Java files) The file is a class of java.io package. 該文件是java.io包的類。 If we create a file then we need to remember one thing before creating a file. First, we need to check whether a file exists of the same name or not. If a file of the sa…

繞過本地驗證提交HTML數據

我們在入侵一個網站,比如上傳或者自己定義提交的文件時,會在本地的代碼中遇到阻礙,,也就是過 濾,過濾有兩種,一種是在遠程服務器的腳本上進行的過濾,這段代碼是在服務器上運行后產生作用的,這種過 濾方式叫做遠程過濾;另一種是在我們的IE瀏覽器里執行的腳本過濾,就是說是在我們…

《dp補卡——343. 整數拆分、96. 不同的二叉搜索樹》

343. 整數拆分 1、確定dp數組以及下標含義。 dp[i]&#xff1a;分拆數字i&#xff0c;可以得到的最大的乘積 2、確定遞推公式&#xff1a; dp[i]最大乘積出處&#xff1a;從1遍歷j到i&#xff0c;j * dp[i-j] 與 j * (i-j)取最大值。( 拆分j的情況&#xff0c;在遍歷j的過程…

Adroid學習之 從源碼角度分析-禁止使用回退按鈕方案

有時候&#xff0c;不能讓用戶進行回退操作&#xff0c;如何處理&#xff1f; 查看返回鍵觸發了哪些方法。在打開程序后把這個方法禁止了。問題&#xff1a;程序在后臺駐留&#xff0c;這樣就會出現&#xff0c;其他時候也不能使用回退按鈕。如何處理&#xff0c;在onpase()時方…

騎士游歷問題問題_騎士步行問題

騎士游歷問題問題Problem Statement: 問題陳述&#xff1a; There is a chessboard of size NM and starting position (sx, sy) and destination position (dx,dy). You have to find out how many minimum numbers of moves a knight goes to that destination position? 有…

Android基礎之用Eclipse搭建Android開發環境和創建第一個Android項目(Windows平臺)...

一、搭建Android開發環境 準備工作&#xff1a;下載Eclipse、JDK、Android SDK、ADT插件 下載地址&#xff1a;Eclipse:http://www.eclipse.org/downloads/ JDK&#xff1a;http://www.oracle.com/technetwork/java/javase/downloads/jdk7u9-downloads-1859576.html Android SD…

《dp補卡——01背包問題》

目錄01背包[416. 分割等和子集](https://leetcode-cn.com/problems/partition-equal-subset-sum/)[1049. 最后一塊石頭的重量 II](https://leetcode-cn.com/problems/last-stone-weight-ii/)[494. 目標和](https://leetcode-cn.com/problems/target-sum/)01背包 1、dp數組以及…

用JavaScript往DIV動態添加內容

參考&#xff1a;http://zhidao.baidu.com/link?url6jSchyqPiEYCBoKdOmv52YHz9r7MTBms2pK1N6ptOX1kaR2eg320mlW1Sr6n36hpOeOadBxC2rWWGuhZPbms-K <div id"show"></div>要填充的數據為: 這是一個測試例子.jquery&#xff1a;$(function(){ var data …

《dp補卡——完全背包問題》

N件物品和一個最多能背重量為W的背包。第i件物品的重量為weight[i]&#xff0c;得到的價值是value[i]。每件物品都有無限個(可以放入背包多次)&#xff0c;求解將哪些物品裝入背包里物品價值總和最大。 01背包和完全背包唯一不同在于遍歷順序上。 01背包的核心代碼&#xff1a…

Java中的類型轉換

類型轉換 (Typecasting) Typecasting is a term which is introduced in all the language similar to java. Typecasting是一個用與Java類似的所有語言引入的術語。 When we assign primitive datatype to another datatype. 當我們將原始數據類型分配給另一個數據類型時。 I…

讓crash文件中的內存地址變成函數名稱,

假如程序員編譯了inhouse給測試。 如果在測試過程中出現奔潰現象&#xff0c;我想程序員一般會來看Device Log 也就是 crash文件 如果crash文件遇到如下的情況&#xff0c;在重要的地方看不到函數名稱。我想是一件很奔潰的事情。 1 Exception Type: EXC_BAD_ACCESS (SIGSEGV)2…

《dp補卡——多重背包》

多重背包簡介&#xff1a; 有N種物品和一個容量為V的背包。第i種物品最多有Mi件可用&#xff0c;每件耗費的空間為Ci&#xff0c;價值為Wi。求解將哪些物品裝入背包可使得這些物品耗費的空間總和不超過背包容量&#xff0c;且價值總和最大。 將Mi件攤開&#xff0c;就是一個01背…

kafka消息確認ack_什么是確認(ACK)? ACK代表什么?

kafka消息確認ackACK&#xff1a;致謝 (ACK: Acknowledgment) An acknowledgment (ACK) is a signal that is passed among the communicating processes, computers, or devices to indicate acknowledgment, or delivery of the message, as a component of a communications…

CocoaAsyncSocket 套接字

CocoaAsyncSocket 套接字 https://github.com/robbiehanson/CocoaAsyncSocket Asynchronous socket networking library for Mac and iOS 用于iOS以及Mac的異步套接字網絡庫。 TCP GCDAsyncSocket and AsyncSocket are TCP/IP socket networking libraries. Here are the key…

谷歌瀏覽器設置緩存方法

谷歌瀏覽器設置緩存方法&#xff1a; 1、在桌面Google Chrome快捷方式&#xff0c;目標&#xff1a;找到 C:\Users\Splendid\AppData\Local\…\Application\chrome.exe 在這后面加上-Disk-Cache-Dir”Z:\TEMP” 注意: -Disk前面有空格&#xff0c;”Z:\TEMP” 是文件存放在Z盤T…

《dp補卡——買賣股票問題》

目錄121. 買賣股票的最佳時機貪心dp思路滾動數組優化122. 買賣股票的最佳時機 II123. 買賣股票的最佳時機 III188. 買賣股票的最佳時機 IV309. 最佳買賣股票時機含冷凍期714. 買賣股票的最佳時機含手續費121. 買賣股票的最佳時機 貪心 取最左最小值&#xff0c;取最右最大值&…