XMUT acdream数据结构专场E题

来源:互联网 发布:自学php能找到工作吗 编辑:程序博客网 时间:2024/05/22 14:14


E - 爱爬山的小Z

Problem Description

从前有一座ACdream王国,这个王国被群山环绕,因此外面的人很少有人知道它的存在。

这个王国里,有一位很喜欢爬山的小伙子,小Z,他觉得在爬山的过程中,能够有一种征服自然的感觉。

小Z定义一条登山路径的困难程度,为整个路径中经过的山的高度的最大值。

然而ACdream王国是一个很神奇的王国,山的高度经常会改变,因此小Z不得不随时重新测量山的高度。

小Z疲惫于测量高度,所以没有精力考虑每条路径的困难程度了。所以想请你帮忙。

Input

多组数据,每组数据首先是一个整数N(N<=100000),表示围绕着ACdream王国的山的数量

接下来是N个整数,表示每一座山的高度h[i](1<=h[i]<=100000),编号从1开始,注意第1座山和第N座山相邻。

接下来是一个整数Q(Q<=100000),表示小Z进行的操作

接下来是Q行,每行是三个整数a,b,c,如果a为0,则代表小Z重新测量第b座山,高度为c,(1<=b<=N,1<=c<=100000)

如果a为1,则代表小Z想知道从b到c的最小困难程度。(1<=b,c<=N)

Output

对于每一次小Z的询问,输出一个整数,表示路径上的最小困难程度。

Sample Input

61 10 2 20 3 3031 1 50 6 31 1 5

Sample Output

203

Hint

故事是这样的,小Z养了一只兔子和一只乌龟,受到小Z的兴趣的影响,兔子和乌龟也喜欢爬山~

有一天,兔子和乌龟要比赛,规定从1号山作为起点,5号山作为终点

小Z说“预备,起~”后,聪明的兔子就沿着1-2-3-4-5的顺序拼命跑。

这是专业坑队友的小Z发现自己的地图的6号山的高度写错了,多写了一个0,赶紧改了~

然而这时兔子已经不知道去哪了,于是乌龟就慢条斯理地沿着1-6-5散步,最终乌龟获得胜利~!

《ACdream童话故事——龟兔赛跑》完



编程思想:线段树区间求最值。


AC  code:

#include <stdio.h>#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<cmath>#include<queue>#include<vector>#define LL long long#define MAXN 1000100using namespace std;const int mod=1000000007;#define maxn 222222#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int MAX[maxn<<2];int max(int a,int b){    return a>b? a:b;}void PushUP(int rt){    MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);}void Build(int l,int r,int rt){    if(l==r)    {        scanf("%d",&MAX[rt]);        return;    }    int m=(l+r)>>1;    Build(lson);    Build(rson);    PushUP(rt);}void Update(int p,int add,int l,int r,int rt){    if(l==r)    {        MAX[rt]=add;        return;    }    int m=(l+r)>>1;    if(p<=m)        Update(p,add,lson);    else        Update(p,add,rson);    PushUP(rt);}int Query(int L,int R,int l,int r,int rt){    if(L<=l&&R>=r)        return MAX[rt];    int m=(l+r)>>1;    int ret=0;    if(L<=m)   ret=max(ret,Query(L,R,lson));    if(R>m)    ret=max(ret,Query(L,R,rson));    return ret;}int ans;int main(){    int N,M,fg;    while(~scanf("%d",&N))    {        Build(1,N,1);        scanf("%d",&M);        while(M--)        {            int a,b;            scanf("%d%d%d",&fg,&a,&b);//注意坑点:输入的a、b不一定a<b!!!            if(fg==1)            {            if(a>b)            {            int t=a;            a=b;            b=t;}ans=min(Query(a,b,1,N,1),max(Query(1,a,1,N,1),Query(b,N,1,N,1)));printf("%d\n",ans);    }            else                 Update(a,b,1,N,1);        }    }    return 0;}


0 0