poj 3264 Balanced Lineup 线段树
来源:互联网 发布:现在开淘宝店还挣钱吗 编辑:程序博客网 时间:2024/06/15 14:24
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int N=200000;const int inf=0xffffff0;int maxv,minv;struct node{ int l,r; int minv,maxv;}tree[5*N+50];void build(int root ,int l,int r){ tree[root].l=l; tree[root].r=r; tree[root].maxv=-inf; tree[root].minv=inf; if(l!=r) { build(2*root+1,l,(l+r)/2); build(2*root+2,(l+r)/2+1,r); }}void update(int root,int i ,int v)//将第i个且值为v的数插入线段树{ if(tree[root].l==tree[root].r) { tree[root].minv=tree[root].maxv=v; return ; } tree[root].minv=min(tree[root].minv,v); tree[root].maxv=max(tree[root].maxv,v); int mid=(tree[root].l+tree[root].r)/2; if(i<=mid) update(root*2+1,i,v); else update(root*2+2,i,v);}void query(int root,int l,int r){ if(tree[root].minv>=minv&&tree[root].maxv<=maxv) return ; if(tree[root].l==l&&tree[root].r==r) { minv=min(tree[root].minv,minv); maxv=max(tree[root].maxv,maxv); return ; } int mid=(tree[root].l+tree[root].r)/2; if(r<=mid) { query(2*root+1,l,r); } else if(l>mid) { query(2*root+2,l,r); } else { query(2*root+1,l,mid); query(2*root+2,mid+1,r); }}int main(){ int i,j,n,q,a,b,v; while(~scanf("%d%d",&n,&q)) { build(0,1,n); for(i=1;i<=n;i++) { scanf("%d",&v); update(0,i,v); } for(i=0;i<q;i++) { scanf("%d%d",&a,&b); maxv=-inf; minv=inf; query(0,a,b); printf("%d\n",maxv-minv); } } return 0;}
0 0
- 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(线段树)
- 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( 线段树&&RMQ )
- POJ 3264 Balanced Lineup 线段树
- 字符串训练 ----- UVA 644题目 Immediate Decodability
- 【JavaWeb】(11)Java实现验证码制作
- 【Unity】讲解如何在Unity的Inspector面板实现类似摄像机层次遮罩的多选效果
- 定制自己的工具LogUtil
- DX学习笔记--(1)不能将参数 2 从“const char *”转换为“LPCWSTR”
- poj 3264 Balanced Lineup 线段树
- 链表类操作
- 认识Toast
- 命令行DOS下无法使用ping等命令
- Red Hat Enterprise Linux 7(RHEL7)卸载openjdk安装sun jdk
- 设计模式之-----抽象工厂模式
- leetcode之路012 Integer to Roman
- 笔试题汇集之常见算法篇(C/C++)
- 读书笔记MoreEffectiveC++(22)