线段树学习小记 Hdu 1754+Poj 3264 (区间最值)
来源:互联网 发布:starbound mac 编辑:程序博客网 时间:2024/06/05 03:29
很基础的数据结构,一些高级算法比如树链剖分什么的都要用到它来实现。
学习资料:
数据结构之线段树 | 董的博客
http://dongxicheng.org/structure/segment-tree/
NotOnlySuccess | 线段树专辑
http://www.notonlysuccess.com/index.php/segment-tree/
Hdu 1754
#include <cstdio>#define min(x,y) ((x)<(y)?(x):(y))#define max(x,y) ((x)>(y)?(x):(y))const int INF=0x7fffffff;const int NUM=200005*3;struct Node{int nmax,left,right;}a[NUM];int MAX;void Build (int t,int left,int right){a[t].left=left;a[t].right=right;a[t].nmax=-INF;if (left==right)return;int mid=(left+right)>>1;Build(t*2,left,mid);Build(t*2+1,mid+1,right);}void Insert (int t,int target,int val){if (a[t].left==target && a[t].right==target){a[t].nmax=val;return;}a[t].nmax=max(a[t].nmax,val);int mid=(a[t].left+a[t].right)>>1;if (target<=mid)Insert(t*2,target,val);elseInsert(t*2+1,target,val);}void Query (int t,int left,int right){if (a[t].left==left && a[t].right==right){MAX=max(MAX,a[t].nmax);return;}int mid=(a[t].left+a[t].right)>>1;if (right<=mid)Query(t*2,left,right);else if (left>mid)Query(t*2+1,left,right);else{Query(t*2,left,mid);Query(t*2+1,mid+1,right);}}int main (){#ifdef ONLINE_JUDGE#else freopen("read.txt","r",stdin);#endifint N,M;while (~scanf("%d%d",&N,&M)){Build (1,1,N);int i,score;for (i=1;i<=N;i++){scanf("%d",&score);Insert(1,i,score);}int left,right;char str[5];for (i=1;i<=M;i++){scanf("%s%d%d",str,&left,&right);if (str[0]=='Q'){MAX=-INF;Query (1,left,right);printf ("%d\n",MAX);}else{Insert(1,left,right);}}}return 0;}
Poj 3264
给出n个数的数列,Q次查询,每次查询某个区间的最大最小值之差。
#include <cstdio>#define min(x,y) ((x)<(y)?(x):(y))#define max(x,y) ((x)>(y)?(x):(y))const int INF=0x7fffffff;const int NUM=200005;struct Node{int nmax,nmin,left,right;}a[NUM];int MAX,MIN;void Build (int t,int left,int right){a[t].left=left;a[t].right=right;a[t].nmin=INF;a[t].nmax=-INF;if (left==right)return;int mid=(left+right)>>1;Build(t*2,left,mid);Build(t*2+1,mid+1,right);}void Insert (int t,int target,int val){if (a[t].left==target && a[t].right==target){a[t].nmax=a[t].nmin=val;return;}a[t].nmax=max(a[t].nmax,val);a[t].nmin=min(a[t].nmin,val);int mid=(a[t].left+a[t].right)>>1;if (target<=mid)Insert(t*2,target,val);elseInsert(t*2+1,target,val);}void Query (int t,int left,int right){if (a[t].left==left && a[t].right==right){MIN=min(MIN,a[t].nmin);MAX=max(MAX,a[t].nmax);return;}int mid=(a[t].left+a[t].right)>>1;if (right<=mid)Query(t*2,left,right);else if (left>mid)Query(t*2+1,left,right);else{Query(t*2,left,mid);Query(t*2+1,mid+1,right);}}int main (){#ifdef ONLINE_JUDGE#else freopen("read.txt","r",stdin);#endifint N,Q;while (~scanf("%d%d",&N,&Q)){Build (1,1,N);int i,high;for (i=1;i<=N;i++){scanf("%d",&high);Insert(1,i,high);}int left,right;for (i=1;i<=Q;i++){scanf("%d%d",&left,&right);if (left == right) //左右值相等则区间内只有一个数,差值必为0printf("0\n");else{MAX=-INF;MIN=INF;Query (1,left,right);printf ("%d\n",MAX-MIN);}}}return 0;}
- 线段树学习小记 Hdu 1754+Poj 3264 (区间最值)
- POJ 3264 Balanced Lineup 求线段树区间最值
- poj 3264 Balanced Lineup(线段树 区间最值)
- HDU 1754(zkw线段树-区间最值)
- HDU 1754 线段树单点更新 区间最值
- HDU 1754(线段树区间最值)
- hdu 1754 I Hate It 线段树-区间最值
- HDU-1754I Hate It 线段树区间最值
- hdu 1754 线段树入门(单点替换,区间最值)
- 线段树区间染色 浮水法 学习小记 Poj 2777 + Poj 2528
- poj 2823 线段树求区间最值
- poj 3875 RMQ或线段树求区间最值
- poj Balanced Lineup (线段树区间最值+优化)
- HDU 5316 线段树区间最值问题
- [线段树 区间最值操作] HDU 5306 Gorgeous Sequence
- 划分树学习小记 Poj 2104+Poj 2761+Hdu 2665 (区间第k大数)
- 区间最值线段树
- 线段树,区间最值
- java多线程总结一: 线程的两种创建方式及优劣比较
- 鼠标移上去之后,显示提示信息
- tetris 2*2
- SAP HANA efashion案例(eFashion on HANA)
- ural 1036
- 线段树学习小记 Hdu 1754+Poj 3264 (区间最值)
- [Euler]Problem 32 - Pandigital products
- 浅谈微信对运营商的冲击
- 给年轻程序员的建议
- Android的Activity的onCreate()多次调用的问题
- 几个开源GIS软件
- VI的配置
- 一个游戏程序员的学习资料
- hdu 2922 Relax! It’s just a game