poj-3264 线段数
来源:互联网 发布:梧桐叶落天下知秋 编辑:程序博客网 时间:2024/05/21 10:20
#include<iostream>
#include <stdio.h>
using namespace std;
int MIN,MAX;
int h[50010],j=1;
int min(int a,int b) { return a<b? a:b; }
int max(int a,int b) { return a>b? a:b; }
typedef struct node
{
int L,R; //区间的起点和终点
int tmin,tmax; //本区间内的最大最小值
node *lchild,*rchild;//指向该节点的左右孩子
}*BTree;
void creatBTree(BTree &T,int L,int R)
{
T=new node;
T->L=L;
T->R=R;
T->tmin=10000000;
T->tmax=0;
T->tmin=min(T->tmin,h[j]);
T->tmax=max(T->tmax,h[j]);
if(L==R) T->tmin=T->tmax=h[j++];
else if(L!=R)
{
creatBTree(T->lchild,L,(L+R)/2);
creatBTree(T->rchild,(L+R)/2+1,R);
if (T->tmin>min(T->lchild->tmin,T->rchild->tmin))
T->tmin=min(T->lchild->tmin,T->rchild->tmin);
if (T->tmax<max(T->lchild->tmax,T->rchild->tmax))
T->tmax=max(T->lchild->tmax,T->rchild->tmax);
}
}
//线段树的查询
void query(BTree T,int s,int e)
{
if(T->tmin>=MIN && T->tmax<=MAX) return;
if (s==T->L && e==T->R)
{
MIN=min(T->tmin,MIN);
MAX=max(T->tmax,MAX);
return;
}
if (e<=(T->L+T->R)/2)query(T->lchild,s,e);
else if(s>=(((T->L+T->R)/2)+1))query(T->rchild,s,e);
else
{
query(T->lchild,s,(T->L+T->R)/2);
query(T->rchild,(T->L+T->R)/2+1,e);
}
}
int main()
{
int n,m;
int i,k;
int s,e;
BTree T;
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
scanf("%d",&h[i]); //初始化数组
creatBTree(T,1,n); //建立数T是根节点.1,n是范围
for(i=0;i<m;i++)
{
scanf("%d%d",&s,&e);
if(s==e)
{
printf("%d\n",0);
continue;
}
MIN=10000000;
MAX=0;//记录初始化
query(T,s,e);
k=MAX-MIN;
printf("%d\n",k);
}
return 0;
}
#include <stdio.h>
using namespace std;
int MIN,MAX;
int h[50010],j=1;
int min(int a,int b) { return a<b? a:b; }
int max(int a,int b) { return a>b? a:b; }
typedef struct node
{
int L,R; //区间的起点和终点
int tmin,tmax; //本区间内的最大最小值
node *lchild,*rchild;//指向该节点的左右孩子
}*BTree;
void creatBTree(BTree &T,int L,int R)
{
T=new node;
T->L=L;
T->R=R;
T->tmin=10000000;
T->tmax=0;
T->tmin=min(T->tmin,h[j]);
T->tmax=max(T->tmax,h[j]);
if(L==R) T->tmin=T->tmax=h[j++];
else if(L!=R)
{
creatBTree(T->lchild,L,(L+R)/2);
creatBTree(T->rchild,(L+R)/2+1,R);
if (T->tmin>min(T->lchild->tmin,T->rchild->tmin))
T->tmin=min(T->lchild->tmin,T->rchild->tmin);
if (T->tmax<max(T->lchild->tmax,T->rchild->tmax))
T->tmax=max(T->lchild->tmax,T->rchild->tmax);
}
}
//线段树的查询
void query(BTree T,int s,int e)
{
if(T->tmin>=MIN && T->tmax<=MAX) return;
if (s==T->L && e==T->R)
{
MIN=min(T->tmin,MIN);
MAX=max(T->tmax,MAX);
return;
}
if (e<=(T->L+T->R)/2)query(T->lchild,s,e);
else if(s>=(((T->L+T->R)/2)+1))query(T->rchild,s,e);
else
{
query(T->lchild,s,(T->L+T->R)/2);
query(T->rchild,(T->L+T->R)/2+1,e);
}
}
int main()
{
int n,m;
int i,k;
int s,e;
BTree T;
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
scanf("%d",&h[i]); //初始化数组
creatBTree(T,1,n); //建立数T是根节点.1,n是范围
for(i=0;i<m;i++)
{
scanf("%d%d",&s,&e);
if(s==e)
{
printf("%d\n",0);
continue;
}
MIN=10000000;
MAX=0;//记录初始化
query(T,s,e);
k=MAX-MIN;
printf("%d\n",k);
}
return 0;
}
- poj-3264 线段数
- POJ 2299 线段树求逆序数
- POJ 3468-线段数区间更新
- POJ 3264 线段树
- poj(3264+线段树)
- POJ 3264 线段树
- POJ 3264 线段树
- POJ 3264 线段树
- poj 3264 线段树
- poj 3264 线段树水题
- poj-3264-线段树
- 线段树求逆序数(离散化)POJ 2299
- poj 2985 线段树求第k大的数
- poj 2274 The Race(逆序数+线段树)
- poj 2299 Ultra-QuickSort 二分+线段树求逆序数
- poj 2528 Mayor's posters 线段数+离散化
- POJ 2299 线段树/树状数组求逆序数
- poj 2528 数的离散化+线段树
- poj-1703 并查集
- poj-2524 并查集
- 2011-第36届ACM/ICPC亚洲区中国大陆5个赛区主办方网络赛和现场赛时间安排
- 创建基于.NET的通用数据库访问层
- CGAL-vc90-mt-sgd.lib
- poj-3264 线段数
- android 之 binder
- 用三个多小时才搞明白的错误:用SqlDataAdapter更新数据
- Equinox:配置Parent classloader
- java线程并发库
- Cache优化的魔力
- “当前上下文中不存在名称”解决
- joj1313
- asp.net调试之-无法在Web服务器上启动调试。未能启动ASP.NET或ATL Server调试