题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,
多次求任一区间Ai-Aj中最大数和最小数的差
线段树太弱了,题目逼格一高连代码都读不懂,今天开始重刷线段树,每天一题,风格用kuangbin大神和以前的,两种都写一遍
2015-05-18:擦,太水了,当年居然这种题都做不出来
RMQ做法:poj3264
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #define lson l,mid,rt<<1 8 #define rson mid+1,r,rt<<1|1 9 #define root 1,n,1 10 #define mid ((l+r)>>1) 11 #define ll long long 12 #define cl(a) memset(a,0,sizeof(a)) 13 #define ts printf("***** "); 14 using namespace std; 15 const int MAXN=55555; 16 int Max[MAXN<<2],Min[MAXN<<2],num[MAXN<<2]; 17 int n,m,t; 18 void pushup(int rt) 19 { 20 Max[rt]=max(Max[rt<<1],Max[rt<<1|1]); 21 Min[rt]=min(Min[rt<<1],Min[rt<<1|1]); 22 } 23 void build(int l,int r,int rt) 24 { 25 if(l==r) 26 { 27 scanf("%d",&num[rt]); 28 Max[rt]=Min[rt]=num[rt]; 29 return; 30 } 31 build(lson); 32 build(rson); 33 pushup(rt); 34 } 35 int Maxx=-9999; 36 int Minn=99999; 37 void query(int L,int R,int l,int r,int rt) 38 { 39 if(L<=l&&R>=r) 40 { 41 Maxx=max(Maxx,Max[rt]); 42 Minn=min(Minn,Min[rt]); 43 return; 44 } 45 if(L<=mid) query(L,R,lson); 46 if(R>mid) query(L,R ,rson); 47 } 48 int main() 49 { 50 int i,j,k,q; 51 #ifndef ONLINE_JUDGE 52 freopen("1.in","r",stdin); 53 #endif 54 while(~scanf("%d%d",&n,&m)) 55 { 56 build(root); 57 int u,v; 58 for(i=0;i<m;i++) 59 { 60 Maxx=-99999; 61 Minn=9999999; 62 scanf("%d%d",&u,&v); 63 query(u,v,root); 64 printf("%d ",Maxx-Minn); 65 } 66 } 67 return 0; 68 }