POJ3264 Balanced Lineup 线段树区间最大值 最小值

Q个数

问区间最大值-区间最小值

 1 // #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <sstream>
 6 #include <string>
 7 #include <algorithm>
 8 #include <list>
 9 #include <map>
10 #include <vector>
11 #include <queue>
12 #include <stack>
13 #include <cmath>
14 #include <cstdlib>
15 // #include <conio.h>
16 using namespace std;
17 #define clc(a,b) memset(a,b,sizeof(a))
18 #define inf 0x3f3f3f3f
19 #define lson l,mid,rt<<1
20 #define rson mid+1,r,rt<<1|1
21 const int N=200010;
22 const int MOD = 1e9+7;
23 #define LL long long
24 double const pi = acos(-1);
25 void fre() {
26     freopen("in.txt","r",stdin);
27 }
28 // inline int r() {
29 //     int x=0,f=1;char ch=getchar();
30 //     while(ch>‘9‘||ch<‘0‘) {if(ch==‘-‘) f=-1;ch=getchar();}
31 //     while(ch>=‘0‘&&ch<=‘9‘) { x=x*10+ch-‘0‘;ch=getchar();}return x*f;
32 // }
33 int a[N];
34 int Min,Max;
35 struct Edge{
36     int l,r;
37     int minx,maxx;
38 }e[4*N];
39
40 void pushup(int rt){
41      e[rt].minx=min(e[rt<<1].minx,e[rt<<1|1].minx);
42      e[rt].maxx=max(e[rt<<1].maxx,e[rt<<1|1].maxx);
43 }
44
45 void build(int l,int r,int rt){
46      e[rt].l=l;
47      e[rt].r=r;
48      if(l==r){
49          e[rt].minx=e[rt].maxx=a[l];
50          return;
51      }
52      int mid=(l+r)>>1;
53      build(lson);
54      build(rson);
55      pushup(rt);
56 }
57
58 void query(int l,int r,int rt,int L,int R){
59      if(e[rt].maxx<=Max&&e[rt].minx>=Min) return;
60      if(L<=l&&R>=r){
61          Min=min(e[rt].minx,Min);
62          Max=max(e[rt].maxx,Max);
63          return;
64      }
65      int mid=(l+r)>>1;
66      if(L<=mid) query(l,mid,rt<<1,L,R);
67      if(R>mid) query(mid+1,r,rt<<1|1,L,R);
68      // if(r<=mid) query(lson,L,R);
69      // else if(l>mid) query(rson,L,R);
70      // else{
71      //     query(lson,L,R);
72      //     query(rson,L,R);
73      // }
74 }
75
76 int main(){
77      // fre();
78      int n,q;
79      scanf("%d%d",&n,&q);
80      for(int i=1;i<=n;i++){
81          scanf("%d",&a[i]);
82      }
83      build(1,n,1);
84      while(q--){
85          int L,R;
86          scanf("%d%d",&L,&R);
87          Min=inf,Max=-inf;
88          // cout<<"!!"<<endl;
89          // getch();
90          query(1,n,1,L,R);
91          printf("%d\n",Max-Min);
92      }
93      return 0;
94 }
时间: 2024-10-31 16:52:17

POJ3264 Balanced Lineup 线段树区间最大值 最小值的相关文章

【POJ】3264 Balanced Lineup ——线段树 区间最值

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 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(求区间的最大值与最小值之差)

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

Balanced Lineup(线段树之区间查找最大最小值)

传送门 线段树的区间查找最大最小值模板. 裸的线段树 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> #define ls

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36820   Accepted: 17244 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 (线段树)

Balanced Lineup 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

POJ 3264 Balanced Lineup 线段树 第三题

Balanced Lineup 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