poj 3264 Balanced Lineup(线段树)
来源:互联网 发布:算法导论第四版百度云 编辑:程序博客网 时间:2024/06/11 05:32
题目链接:http://poj.org/problem?id=3264
思路:
线段树,维护最大值最小值之差。
一开始蠢得要死,写了两个Query维护最大最小值,后来在网上参考大神的优化了一下。
#include <limits.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <algorithm>#include <iostream>#include <iterator>#include <sstream>#include <queue>#include <stack>#include <string>#include <vector>#include <map>#include <set>//#define ONLINE_JUDGE#define eps 1e-6#define INF 0x7fffffff //INT_MAX#define inf 0x3f3f3f3f //int??????????????????#define FOR(i,a) for((i)=0;i<(a);(i)++) //[i,a);#define MEM(a) (memset((a),0,sizeof(a)))#define sfs(a) scanf("%s",a)#define sf(a) scanf("%d",&a)#define sfI(a) scanf("%I64d",&a)#define pf(a) printf("%d\n",a)#define pfI(a) printf("%I64d\n",a)#define pfs(a) printf("%s\n",a)#define sfd(a,b) scanf("%d%d",&a,&b)#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)#define for1(i,a,b) for(int i=(a);i<b;i++)#define for2(i,a,b) for(int i=(a);i<=b;i++)#define for3(i,a,b)for(int i=(b);i>=a;i--)#define MEM1(a) memset(a,0,sizeof(a))#define MEM2(a) memset(a,-1,sizeof(a))#define MEM3(a) memset(a,0x3f,sizeof(a))#define MEMS(a) memset(a,'\0',sizeof(a))#define LL __int64using namespace std;const double PI = acos(-1.0);template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }template<class T>T Mint(T a, T b, T c) { if (a>b) { if (c>b) return b; return c; } if (c>a) return a; return c;}template<class T>T Maxt(T a, T b, T c) { if (a>b) { if (c>a) return c; return a; } else if (c > b) return c; return b;} const int maxn=200010;int T,n,m;int maxnum,minnum;int a[maxn]; struct segTree{ int l,r; int v,u;}node[maxn*3+10]; void build(int id,int left,int right){ node[id].l=left; node[id].r=right; node[id].v=node[id].u=0; if(left==right){ node[id].v=node[id].u=a[left]; return ; } int mid=(left+right)/2; build(id<<1,left,mid); build(id<<1|1,mid+1,right); node[id].v=max(node[id<<1].v,node[id<<1|1].v); node[id].u=min(node[id<<1].u,node[id<<1|1].u);} void Query(int id,int Qleft,int Qright){ int l=node[id].l,r=node[id].r; if(node[id].v<=maxnum&&node[id].u>=minnum) return ; if(l>=Qleft&&r<=Qright){ maxnum=max(maxnum,node[id].v); minnum=min(minnum,node[id].u); return ; } int mid=(l+r)/2; if(mid>=Qright) Query(id<<1,Qleft,Qright); else if(mid<Qleft) Query(id<<1|1,Qleft,Qright); else{ Query(id<<1,Qleft,mid); Query(id<<1|1,mid+1,Qright); }} int main(){ while(~sfd(n,m)){ for2(i,1,n) sf(a[i]); build(1,1,n); int p,q; while(m--){ sfd(p,q); minnum=inf,maxnum=-inf; Query(1,p,q); pf(maxnum-minnum); } } return 0;}
0 0
- POJ 3264 - Balanced Lineup(线段树)
- Balanced Lineup(POJ-3264)(线段树)
- POJ - 3264 - Balanced Lineup (线段树)
- poj 3264 Balanced Lineup(线段树)
- POJ 3264Balanced Lineup(线段树)
- poj 3264 Balanced Lineup(线段树)
- POJ 3264Balanced Lineup (线段树)
- poj 3264 Balanced Lineup rmq/线段树
- POJ 3264 Balanced Lineup RMQ / 线段树
- POJ 3264 Balanced Lineup 线段树基础
- poj 3264 Balanced Lineup[线段树,,水]
- 【线段树】 POJ 3264 Balanced Lineup
- poj 3264 Balanced Lineup 基础线段树
- poj 3264 Balanced Lineup(线段树)
- POJ 3264 Balanced Lineup 线段树
- Poj 3264 Balanced Lineup - 线段树
- POJ 3264 Balanced Lineup (RMQ线段树)
- POJ 3264 Balanced Lineup 线段树RMQ
- untiy优化—脚本优化
- Django restframework入门示例
- hdu 1754 I Hate It(线段树)
- 关于类的多态思考。
- UVALive 4329 Ping pong(树状数组)
- poj 3264 Balanced Lineup(线段树)
- ProgressBar进度条
- IO基础知识
- 闪动的文字效果(渲染)
- Can't connect to local MySQL server through socket '/tmp/mysql.sock'错误
- Socket传输序列化对象
- StackView堆叠视图
- RSA加密算法操作的java演示
- leetcode笔记:Jump Game II