poj3264 Balanced Lineup

题意:

给定Q (1 ≤ Q ≤ 200,000)个数A1,A2 … AQ,,多次求任一区间Ai – Aj中最大数和最小数的差。

思路:

线段树。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 const int MAXN = 65536;
 7 const int INF = 0x3f3f3f3f;
 8
 9 struct node
10 {
11     int maxn, minn;
12 };
13
14 int a[MAXN + 1];
15 node data[2 * MAXN - 1];
16 int n, q, x, y;
17
18 void init(int n_)
19 {
20     n = 1;
21     while (n < n_)
22     {
23         n *= 2;
24     }
25     for (int i = 0; i < 2 * n - 1; i++)
26     {
27         data[i].maxn = -INF;
28         data[i].minn = INF;
29     }
30 }
31
32 void update(int k, int a)
33 {
34     k += n - 1;
35     data[k].minn = data[k].maxn = a;
36     while (k)
37     {
38         k = (k - 1) / 2;
39         data[k].minn = min(data[2 * k + 1].minn, data[2 * k + 2].minn);
40         data[k].maxn = max(data[2 * k + 1].maxn, data[2 * k + 2].maxn);
41     }
42 }
43
44 int query(int a, int b, int k, int l, int r, int type)
45 {
46     if (r <= a || l >= b)
47     {
48         if (type)
49             return INF;
50         else
51             return -INF;
52     }
53     if (l >= a && r <= b)
54     {
55         if (type)
56             return data[k].minn;
57         else
58             return data[k].maxn;
59     }
60     int x = query(a, b, 2 * k + 1, l, (l + r) / 2, type);
61     int y = query(a, b, 2 * k + 2, (l + r) / 2, r, type);
62     if (type)
63         return min(x, y);
64     return max(x, y);
65 }
66
67 int main()
68 {
69     scanf("%d %d", &n, &q);
70     int n_ = n;
71     init(n);
72     for (int i = 0; i < n_; i++)
73     {
74         scanf("%d", &a[i]);
75         update(i, a[i]);
76     }
77     for (int i = 0; i < q; i++)
78     {
79         scanf("%d %d", &x, &y);
80         int s = query(x - 1, y, 0, 0, n, 1);
81         int t = query(x - 1, y, 0, 0, n, 0);
82         printf("%d\n", t - s);
83     }
84     return 0;
85 }
时间: 2024-10-13 10:33:43

poj3264 Balanced Lineup的相关文章

POJ3264 Balanced Lineup RMQ 线段树

求区间内最大数和最小数的差,用两棵线段树,一个维护区间最大值,一个维护区间最小值. #include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string> #include <iostream> #include <queue> #include <list> #include <algori

POJ3264——Balanced Lineup(线段树)

本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中,取一段区间,然后在区间中找出最大的数和最小的数字,求这两个数字的差. 分析:按区间取值,很明显使用的线段树.区间大小取200000 * 4 = 8 * 10 ^5; 进行查询的时候,注意直接判断l, r 与mid的关系即可,一开始写的时候直接与tree[root].L判断,多余了, 逻辑不正确. #include <iostream> #include <stdio.h> #inc

poj3264 Balanced Lineup(求区间的最大值与最小值之差)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 37869   Accepted: 17751 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 Joh

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 36813 Accepted: 17237 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 de

POJ-3264 Balanced Lineup(线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 47042   Accepted: 22071 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 Joh

poj3264 Balanced Lineup 2011-12-20

Balanced Lineup Time Limit: 5000MSMemory Limit: 65536K Total Submissions: 20569Accepted: 9552 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 decid

POJ3264 Balanced Lineup 【线段树】+【单点更新】

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32778   Accepted: 15425 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 Joh

poj3264 Balanced Lineup(树状数组)

题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: 30135 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 Farm

poj--3264 Balanced Lineup(裸的RMQ)

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 rang