POJ 3264 Balanced Lineup 線段樹入門(點的查詢)
這道題的意思是:給你n個點,每個數出現的位置對應著改點數的大小。然後給出m個區間輸出每個區間上的最大值與最小值的差。
典型的線段樹啊,自己寫的那個程式碼很搓,然後嘯爺教了我,他怎麼寫的,就學習了一下。以後儘量寫的好一點啊。
主要是線段樹的建立與查詢。
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 31222 | Accepted: 14709 | |
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
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 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Sample Input
6 3 1 7 3 4 2 5 1 5 4 6 2 2
Sample Output
6 3 0
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 10001000
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 50100;
using namespace std;
struct node
{
int a, b;
int Max, Min;
} f[4*maxn];
int cnum[maxn];
void Bulid(int l, int r, int site)
{
if(l == r)
{
f[site].a = l;
f[site].b = r;
f[site].Max = cnum[l];
f[site].Min = cnum[l];
return ;
}
int mid = (l+r)/2;
Bulid(l, mid, site*2);
Bulid(mid+1, r, site*2+1);
f[site].Max = max(f[site*2].Max, f[site*2+1].Max);
f[site].Min = min(f[site*2].Min, f[site*2+1].Min);
f[site].a = l;
f[site].b = r;
}
node Search(int l, int r, int site)
{
if(l == f[site].a && r == f[site].b)
return f[site];
int mid = (f[site].a+f[site].b)/2;
if(r <= mid)
{
return Search(l, r, 2*site);
}
else if(l > mid)
{
return Search(l, r, site*2+1);
}
else
{
node p1 = Search(l, mid, site*2);
node p2 = Search(mid+1, r, site*2+1);
node p;
p.Max = max(p1.Max, p2.Max);
p.Min = min(p1.Min, p2.Min);
return p;
}
}
void Updata(int m, int d, int site)
{
if(f[site].a == f[site].b)
{
f[site].Max = f[site].Min = d;
return;
}
int mid = (f[site].a+f[site].b)/2;
if(m <= mid)
{
Updata(m, d, 2*site);
}
else
{
Updata(m, d, 2*site+1);
}
f[site].Max = max(f[site*2].Max, f[site*2+1].Max);
f[site].Min = min(f[site*2].Min, f[site*2+1].Min);
}
int main()
{
int n, m;
while(scanf("%d %d",&n, &m) != EOF)
{
for(int i = 1; i <= n; i++)
scanf("%d",&cnum[i]);
Bulid(1, n, 1);
int a, b;
while(m--)
{
scanf("%d %d",&a, &b);
node p;
p = Search(a, b, 1);
printf("%d\n",p.Max-p.Min);
}
}
return 0;
}
相關文章
- POJ 3264-Balanced Lineup詳解(線段樹區間求值)
- 【RMQ】poj 3264 Balanced LineupMQ
- POJ3264 Balanced Lineup【RMQ】MQ
- POJ 3264 Balanced Lineup【RMQ問題】MQ
- POJ 3264 Balanced Lineup(簡單的RMQ)MQ
- POJ 3264-Balanced Lineup(RMQ-ST演算法)MQ演算法
- POJ 2828 Buy Tickets 線段樹入門(建樹稍微有點抽象)抽象
- poj--3264Balanced Lineup+ST演算法求區間最大最小值演算法
- POJ 2777 Count Color 線段樹入門題
- 線段樹入門
- POJ 2777-Count Color(線段樹-區間染色查詢)
- 線段樹(1)建樹、單點修改、單點查詢、區間查詢和例題
- POJ 2582 Mayor's posters 線段樹入門題+離散化
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- 線段樹入門理解
- 線段樹入門(Segment Tree)
- 線段樹模版:從入門到入墳
- HDU 1754 I Hate It 線段樹入門
- hdu 1394 Minimum Inversion Number 【線段樹查詢】
- HDU1754 I Hate It 【線段樹基礎:點修改+區間查詢】
- 1080 線段樹練習 單點修改及區間查詢
- 1081 線段樹練習 2 單點查詢及區間修改
- (hdu 1166)敵兵佈陣(線段樹入門,單點更新)
- HDU 1556 Color the ball 線段樹入門題
- POJ 3468 A Simple Problem with Integers(線段樹區間操作)
- POJ 2991 Crane(線段樹+計算幾何)
- POJ 3468 A Simple Problem with Integers (線段樹 區間更新)
- 芻議線段樹 2 (區間修改,區間查詢)
- 線段樹的一點總結
- POJ 3468 A Simple Problem with Integers (線段樹 區間共加)
- POJ 3468-A Simple Problem with Integers(區間更新線段樹)
- 動態開點線段樹
- HDU1166 敵兵佈陣【線段樹基礎:點修改+區間查詢】
- POJ 2886 Who Gets the Most Candies?(線段樹+反素數)
- 線段樹知識點總結
- 分組查詢連線號段
- 線~段~樹
- 線段樹