poj 3264 线段树

线段树太弱了,题目逼格一高连代码都读不懂,今天开始重刷线段树,每天一题,风格用kuangbin大神和以前的,两种都写一遍

RMQ做法:poj3264

 1 /*
 2 POJ 3264  Balanced Lineup
 3 题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,
 4 多次求任一区间Ai-Aj中最大数和最小数的差
 5 */
 6 #include<stdio.h>
 7 #include<algorithm>
 8 #include<iostream>
 9 using namespace std;
10 #define MAXN 200005
11 #define INF 10000000
12 int nMax,nMin;//记录最大最小值
13 struct Node
14 {
15     int l,r;//区间的左右端点
16     int nMin,nMax;//区间的最小值和最大值
17 }segTree[MAXN*3];
18 int a[MAXN];
19 void Build(int i,int l,int r)//在结点i上建立区间为(l,r)
20 {
21     segTree[i].l=l;
22     segTree[i].r=r;
23     if(l==r)//叶子结点
24     {
25         segTree[i].nMin=segTree[i].nMax=a[l];
26         return;
27     }
28     int mid=(l+r)>>1;
29     Build(i<<1,l,mid);
30     Build(i<<1|1,mid+1,r);
31     segTree[i].nMin=min(segTree[i<<1].nMin,segTree[i<<1|1].nMin);
32     segTree[i].nMax=max(segTree[i<<1].nMax,segTree[i<<1|1].nMax);
33 }
34 void Query(int i,int l,int r)//查询结点i上l-r的最大值和最小值
35 {
36     if(segTree[i].nMax<=nMax&&segTree[i].nMin>=nMin)  return;
37     if(segTree[i].l==l&&segTree[i].r==r)
38     {
39         nMax=max(segTree[i].nMax,nMax);
40         nMin=min(segTree[i].nMin,nMin);
41         return;
42     }
43     int mid=(segTree[i].l+segTree[i].r)>>1;
44     if(r<=mid)   Query(i<<1,l,r);
45     else if(l>mid)  Query(i<<1|1,l,r);
46     else
47     {
48         Query(i<<1,l,mid);
49         Query(i<<1|1,mid+1,r);
50     }
51 }
52 int main()
53 {
54     int n,q;
55     int l,r;
56     int i;
57     while(scanf("%d%d",&n,&q)!=EOF)
58     {
59         for(i=1;i<=n;i++)
60           scanf("%d",&a[i]);
61         Build(1,1,n);
62         for(i=1;i<=q;i++)
63         {
64             scanf("%d%d",&l,&r);
65             nMax=-INF;nMin=INF;
66             Query(1,l,r);
67             printf("%d\n",nMax-nMin);
68         }
69     }
70     return 0;
71 }
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 int n,m,t;
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 const int maxn=55555;
12 int cow[maxn];
13 int maxx[maxn<<2],minn[maxn<<2];
14 int mmax=-1,mmin=9999999;
15 void pushup(int rt){
16     maxx[rt]=max(maxx[rt<<1],maxx[rt<<1|1]);
17     minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);
18 }
19 void build(int l,int r,int rt){
20     if(l==r){
21         scanf("%d",&minn[rt]);
22         maxx[rt]=minn[rt];
23         return;
24     }
25     int m=(l+r)>>1;
26     build(lson);
27     build(rson);
28     pushup(rt);
29 }
30 void query(int L,int R,int l,int r,int rt) {
31     if (L<=l&&r<=R){
32         mmax=max(mmax,maxx[rt]);
33         mmin=min(mmin,minn[rt]);
34         return;
35     }
36     int m=(l+r)>>1;
37     int ret=0;
38     if(L<=m)  query(L,R,lson);
39     if(R>m)  query(L,R ,rson);
40 }
41 int main()
42 {
43     int i,j,k;
44     #ifndef ONLINE_JUDGE
45     freopen("1.in","r",stdin);
46     #endif
47     int q;
48     while(scanf("%d%d",&n,&q)!=EOF)
49     {
50         build(1,n,1);
51         for(i=1;i<=q;i++)
52         {
53             int a,b;
54             scanf("%d%d",&a,&b);
55             query(a,b,1,n,1);
56             printf("%d\n",mmax-mmin);
57             mmax=-1,mmin=9999999;
58         }
59     }
60     return 0;
61 }
时间: 2024-08-08 13:53:07

poj 3264 线段树的相关文章

POJ&mdash;&mdash;3264线段树

题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,000 设计每次查询的复杂度为logm. 例如, 输入: 6 3 1 7 3 4 2 5 1 5 4 6 2 2 输出: 6 3 0     分析:这道题是典型的空间换时间的题.一看到是区间的查找,我们应该想到线段树,这里有一篇我觉得写得挺好的介绍线段树的博客和大家分享一下:http://www.cnb

POJ - 3264(线段树实现)

题目:poj.org/problem?id=3264 题意:求一段区间内最大值与最小值的差. 看到区间最值首先想到RMQ--ST算法.但本题出现在了kuangbin专题的线段树里. 用线段树也无思维难点,但有两个坑: 1. 查询函数中,区间不包含时的返回值. 2.用cin,cout会TLE.用c的输入方式. 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<string&g

poj 3264 线段树 求区间最大最小值

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

G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)

G - Balanced Lineup Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3264 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 线段树+二分

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

POJ 3264 线段树模板题

之前做的那道是区间求和的,这道题是求区间最大值和最小值之差的,感觉这道题更简单.只需在插入时把每个区间的最大值最小值求出来保存在根节点上就可以啦~\(^o^)/ Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 39475   Accepted: 18524 Case Time Limit: 2000MS Description For the daily milking, Farmer Jo

POJ 2528 (线段树+离散化) Mayor&#39;s posters

因为将每个单位都作为一个最小单元的话会爆内存的 所以,将海报的每个端点进行排序,将这些端点最为最小的区间. 毕竟是刚刚接触线段树,理解起来还有些吃力,还是那句话,题做多了慢慢就好了. 萌萌的AC代码君贴上. 1 //#define LOCAL 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 int n; 8 struct CPost 9

poj 2777 线段树的区间更新

Count Color Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Chosen Problem Solving and Program design as an optional course, you are required to solve al

转载::POJ 2991 线段树+计算几何(有c++结构体操作)

POJ 2991 线段树+计算几何 (2011-02-27 21:13:44) 转载▼ 标签: 杂谈 分类: OI 话说这一题真的是很恶心很恶心,不过确实改变了我对线段树的一些看法,算是很经典的题目. 题意:有一个吊车由很多个不同长度的线段组成,一开始是一条长直线起点在(0,0),尾节点在(0,sum[n]),每条线段之间的夹角的初始值是180度.然后有一些操作a. b将第a条线段和a+1之间的夹角变成b度,经过每一次操作都要求出尾节点的坐标. 首先要用到一个计算几何的知识(没学过..请教而来)