poj(3264+线段树)
来源:互联网 发布:abc优化算法流程图 编辑:程序博客网 时间:2024/04/29 12:30
题目链接:http://poj.org/problem?id=3264
有n个数字,编号1...N,做K次查询,每次查询输出编号A...B之间最大的一个和最小的一个的差值。
代码:
#include<stdio.h>#define MAXN 50005struct Node{int left;int right;int max;int min;}node[MAXN * 4];int hei[MAXN];int N,Q;int ansmax,ansmin;int Max(int a,int b){return a>b ? a : b ;}int Min(int a,int b){return a<b ? a : b ;}void BuildTree(int left,int right,int tot){ node[tot].left = left;node[tot].right = right;if(left == right){node[tot].max = node[tot].min = hei[left];return ;} BuildTree(left,(left+right)/2,tot*2);BuildTree((left+right)/2+1,right,tot*2+1);node[tot].max = Max(node[tot*2].max,node[tot*2+1].max);node[tot].min = Min(node[tot*2].min,node[tot*2+1].min);}void Query(int left,int right,int tot){if(node[tot].left == left && node[tot].right == right){ansmax = Max(ansmax,node[tot].max);ansmin = Min(ansmin,node[tot].min);return ;} if(left <= node[tot*2].right){int r = Min(right,node[tot*2].right);Query(left,r,tot*2);}if(right >= node[tot*2+1].left){int l = Max(left,node[tot*2+1].left);Query(l,right,tot*2+1);}}int main(){ while(scanf("%d%d",&N,&Q)!=EOF) { int i,s,t; for(i=1;i<=N;i++) { scanf("%d",&hei[i]); } BuildTree(1,N,1); while(Q--) { scanf("%d%d",&s,&t); ansmax = 0; ansmin = 1000000; Query(s,t,1); printf("%d\n",ansmax-ansmin); } } return 0;}
- POJ 3264 线段树
- poj(3264+线段树)
- POJ 3264 线段树
- POJ 3264 线段树
- POJ 3264 线段树
- poj 3264 线段树
- poj-3264-线段树
- POJ 3264 简单线段树
- poj 3264 线段树方法
- poj 3264 ( 线段树)
- poj 3264线段树基础
- POJ 3264 线段树 ST
- poj 3264 线段树 入门
- poj 3264 & poj 3468(线段树)
- poj 3264 Balanced Lineup rmq/线段树
- POJ 3264 Balanced Lineup RMQ / 线段树
- poj 3264 RMQ 线段树模版题
- POJ 3264 Balanced Lineup 线段树基础
- 数据库索引原理(1)-----TokuDB中的COLA-Tree
- 延时调用的时候要注意
- 网页设计中很重要的概念div+浮动..
- C++ 迭代器失效原因
- (AutoIt 实例)图片批量重命名小工具,支持jpg、jpeg、png、gif格式
- poj(3264+线段树)
- JTextField内容有效性验证几种方式
- VB中调用SQL的一些小问题
- Word 2010插入超链接——使用相对路径
- Android在Adapter里面调用Activity的方法/变量
- 手机首次充电方法及关于锂电池充电的正确知识ZZ
- 如何在Spring框架中解决多数据源的问题
- [Android实例] 仿Windows 文件浏览 文件选择器
- Android特效(持续更新)