POJ 3264 Balanced Lineup【線段樹區間查詢求最大值和最小值】

Balanced Lineup

Time Limit: 5000MS?Memory Limit: 65536K
Total Submissions: 53703?Accepted: 25237
Case Time Limit: 2000MS

Description

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ ABN), representing the range of cows from A to B inclusive.

Output

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

Source

USACO 2007 January Silver
題目鏈接:http://poj.org/problem?id=3264
分析:線段樹求最大值和最小值,然后最大值減去最小值即為正解!貌似這題好像有暴力寫法?
下面給出AC代碼:
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 #define maxsize 200020
 6 typedef struct
 7 {
 8     int left,right;
 9     int maxn;
10     int minn;
11 }Node;
12 int n,m;
13 int Max,Min;
14 int num[maxsize];
15 Node tree[maxsize*20];
16 inline void buildtree(int root,int left,int right)// 構建線段樹
17 {
18     int mid;
19     tree[root].left=left;
20     tree[root].right=right;// 當前節點所表示的區間
21     if(left==right)// 左右區間相同,則此節點為葉子,max 應儲存對應某個學生的值
22     {
23         tree[root].maxn=num[left];
24         tree[root].minn=num[left];
25         return;
26     }
27     mid=(left+right)/2;
28     //int a,b;// 遞歸建立左右子樹,并從子樹中獲得最大值
29     buildtree(2*root,left,mid);
30     buildtree(2*root+1,mid+1,right);
31     tree[root].maxn=max(tree[root*2].maxn,tree[root*2+1].maxn);
32     tree[root].minn=min(tree[root*2].minn,tree[root*2+1].minn);
33 }
34 inline void find(int root,int left,int right)// 從節點 root 開始,查找 left 和 right 之間的最大值
35 {
36     int mid;
37     //if(tree[root].left>right||tree[root].right<left)// 若此區間與 root 所管理的區間無交集
38         //return;
39     if(left==tree[root].left&&tree[root].right==right)// 若此區間包含 root 所管理的區間
40     {
41         Max=max(tree[root].maxn,Max);
42         Min=min(tree[root].minn,Min);
43         return;
44     }
45     mid=(tree[root].left+tree[root].right)/2;
46     if(right<=mid)
47         find(root*2,left,right);
48     else if(left>mid)
49         find(root*2+1,left,right);
50     else
51     {
52         find(root*2,left,mid);
53         find(root*2+1,mid+1,right);
54         //tree[root].maxn=max(tree[root*2].maxn,tree[root*2+1].maxn);
55         //tree[root].minn=min(tree[root*2].minn,tree[root*2+1].minn);
56         //return;
57     }
58 }
59 
60 int main()
61 {
62     //char c;
63     int i;
64     int x,y;
65     //scanf("d%d",&n,&m);
66     while(scanf("%d%d",&n,&m)!=EOF)
67     {
68         for(i=1;i<=n;i++)
69             scanf("%d",&num[i]);
70         buildtree(1,1,n);
71         for(i=1;i<=m;i++)
72         {
73             //getchar();
74             Max=-99999999999;
75             Min= 99999999999;
76             scanf("%d%d",&x,&y);
77             //if(c=='Q')
78                 //printf("%d\n",find(1,x,y));
79             //else
80             //{
81                // num[x]=y;
82                // update(1,x,y);
83             //}
84             find(1,x,y);
85             printf("%d\n",Max-Min);
86         }
87     }
88     return 0;
89 }

?

轉載于:https://www.cnblogs.com/ECJTUACM-873284962/p/7133096.html

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

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

相關文章

halcon測試一張圖片是否過曝或過暗

read_image (Image, 1.bmp) count_obj (Image, Number) if(Number<0)return() endif min_max_gray (Image, Image, 0, Min, Max, Range) if(Min<1)*圖像過暗 endif if(Max>254)*圖像過曝 endif

真的要做一輩子的程序員嗎?來自10年程序員的心聲

經常聽一些同學說&#xff1a;不知道下一份工作該去哪類公司做些什么&#xff0c;我的職場人際一團糟老板不重視我&#xff0c;我現在成長的非常慢所以又想跳槽了&#xff0c;我看不到公司的發展前景好迷茫&#xff0c;其實這一切的困惑都來源于沒有做好職業規劃或者你根本就沒…

網絡編程之 TCP / UDP 及其流程比較

TCP與UDP的區別 1、基于連接與無連接 2、對系統資源的要求&#xff08;TCP較多&#xff0c;UDP少&#xff09;3、UDP程序結構較簡單 流模式與數據報模式 4、TCP保證數據正確性&#xff0c;UDP可能丟包 5、TCP保證數據順序&#xff0c;UDP不保證具體編程時的區別 1、socket()的參…

Tomcat在Linux上的安裝與配置

Tomcat在Linux上的安裝與配置 1、 jdk下載地址&#xff1a; http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html tomcat下載地址:http://tomcat.apache.org/download-70.cg 2、jdk安裝與配置.&#xff08;rpm包&#xff09; (1)jdk安裝…

Spring在3.1版本后的bean獲取方法的改變

xml配置不變&#xff0c;如下 <?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://…

使用halcon選擇點擬合成直線求直線角度

原圖 源碼 read_image (Image, 0.bmp) dev_clear_window () dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) dev_display (Image)binary_threshold (Image, Region, max_separability, dark, UsedThreshold) connection (Region, ConnectedRegions) select_s…

Linux網絡/firewalld和netfilter/netfilter/iptables語法

為什么80%的碼農都做不了架構師&#xff1f;>>> linux網絡相關 查看網卡網絡信息 ifconfig 命令查看網卡網絡信息&#xff0c;比如ip、網關、子網掩碼等&#xff0c;但是安裝centos7的版本或者某些未知原因&#xff0c;此命令提示找不到&#xff0c;我們可以使用Yu…

Chrome開發者工具詳解(4)-Profiles面板

Chrome開發者工具詳解(4)-Profiles面板 如果上篇中的Timeline面板所提供的信息不能滿足你的要求&#xff0c;你可以使用Profiles面板&#xff0c;利用這個面板你可以追蹤網頁程序的內存泄漏問題&#xff0c;進一步提升程序的JavaScript執行性能。 概述 當前使用的Chrome最新版為…

etcd raft library設計原理和使用

早在2013年11月份&#xff0c;在raft論文還只能在網上下載到草稿版時&#xff0c;我曾經寫過一篇blog對其進行簡要分析。4年過去了&#xff0c;各種raft協議的講解鋪天蓋地&#xff0c;raft也確實得到了廣泛的應用。其中最知名的應用莫過于etcd。etcd將raft協議本身實現為一個l…

halcon通過點擬合圓形,鼠標選點

原圖 源碼 read_image (Image, 0.bmp) dev_clear_window () dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) dev_display (Image)binary_threshold (Image, Region, max_separability, dark, UsedThreshold) connection (Region, ConnectedRegions) select_s…

JDBC事務--軟件開發三層架構--ThreadLocal

JDBC事務--軟件開發三層架構--ThreadLocal 一.JDBC事務 1.概述: 事務是指邏輯上的一組操作!這一組操作,通常認為是一個整體,不可拆分! 特點:同生共死;事務內的這一組操作要么全部成功,要么全部失敗! 作用:保證邏輯操作的完整性,安全性! 2.使用(3種方式) 1)面向數據庫,使用S…

LINUX多播編程

一.單播&#xff0c;廣播和多播 1.單播用于兩個主機之間的端對端通信&#xff0c;廣播用于一個主機對整個局域網上所有主機上的數據通信。單播和廣播是兩個極端&#xff0c;要么對一個主機進行通信&#xff0c;要么對整個局域網上的主機進行通信。實際情況下&#xff0c;經常需…

cas單點登錄搭建

Cas Server下載&#xff1a;http://developer.jasig.org/cas/ Cas Client下載&#xff1a;http://developer.jasig.org/cas-clients/ 測試環境&#xff1a; jdk&#xff1a;java version "1.8.0_60" tomcat&#xff1a;apache-tomcat-7.0.65 mysql&#xff1a;mysql5…

新CIO:Mark Schwartz認為的領先IT

美國公民及移民服務局前任CIO&#xff0c;現任AWS企業戰略師Mark Schwartz在倫敦舉行的DevOps企業峰會上介紹了什么是領先的IT。\\Schwartz介紹說&#xff0c;老舊、傳統的模型將業務和IT完全分開&#xff0c;他又提出了一種新的模型&#xff0c;在這種模型中&#xff0c;CIO擔…

689D Magic Odd Square 奇數幻方

1 奇數階幻方構造法 (1) 將1放在第一行中間一列; (2) 從2開始直到nn止各數依次按下列規則存放&#xff1a;按 45方向行走&#xff0c;向右上&#xff0c;即每一個數存放的行比前一個數的行數減1&#xff0c;列數加1 (3) 如果行列范圍超出矩陣范圍&#xff0c;則回繞。例如1在第…

Java單例的常見形式

2019獨角獸企業重金招聘Python工程師標準>>> Java單例的常見形式 本文目的&#xff1a;總結Java中的單例模式 本文定位&#xff1a;學習筆記 學習過程記錄&#xff0c;加深理解&#xff0c;便于回顧。也希望能給學習的同學一些靈感 一、非延遲加載單例類 public cla…

運動控制卡的基類函數與實現例子

基類 namespace MotionCardDll {public abstract class IMotionCard{public Int32 m_Mode;public Int32 m_BoardId;//Card 號public Int32 m_Card_name;public Int32 m_StartAxisID

U-Boot啟動過程完全分析

1.1 U-Boot 工作過程 U-Boot啟動內核的過程可以分為兩個階段&#xff0c;兩個階段的功能如下&#xff1a; &#xff08;1&#xff09;第一階段的功能 硬件設備初始化 加載U-Boot第二階段代碼到RAM空間 設置好棧 跳轉到第二階段代碼入口 &#xff08;2&#x…

CJOJ 2171 火車站開飯店(樹型動態規劃)

CJOJ 2171 火車站開飯店&#xff08;樹型動態規劃&#xff09; Description 政府邀請了你在火車站開飯店&#xff0c;但不允許同時在兩個相連的火車站開。任意兩個火車站有且只有一條路徑&#xff0c;每個火車站最多有 50 個和它相連接的火車站。 告訴你每個火車站的利潤&#…

JavaWeb總結(十五)

AJAX&#xff08;Asynchronous JavaScript and XML&#xff08;異步的 JavaScript 和 XML&#xff09;&#xff09; AJAX的作用是什么&#xff1f; 在無需重新加載整個網頁的情況下&#xff0c;能夠更新部分網頁的技術 是一種用于創建快速動態網頁的技術 通過在后臺與服務器進行…