链接:https://vjudge.net/contest/66989#problem/G
完完全全的水题,还是被坑了,一个return忘了写,de了半天bug!!
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 const int maxn=50010; const ll inf=1e15; ll value1[maxn<<2],value2[maxn<<2]; void pushup(int rt) { value1[rt]=max(value1[rt<<1],value1[rt<<1|1]); value2[rt]=min(value2[rt<<1],value2[rt<<1|1]); } void btree(int l,int r,int rt) { if(l==r) { scanf("%lld",&value1[rt]); value2[rt]=value1[rt]; return ; } int m=(l+r)>>1; btree(ls); btree(rs); pushup(rt); } ll query1(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R)return value1[rt]; int m=(l+r)>>1; ll ans=0; if(L<=m)ans=max(ans,query1(L,R,ls)); if(R>m)ans=max(ans,query1(L,R,rs)); return ans; } ll query2(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R)return value2[rt]; int m=(l+r)>>1; ll ans=inf; if(L<=m)ans=min(ans,query2(L,R,ls)); if(R>m)ans=min(ans,query2(L,R,rs)); return ans; } int main() { int n,m; while(~scanf("%d%d",&n,&m)){ btree(1,n,1); while(m--){ int l,r; scanf("%d%d",&l,&r); printf("%lld ",query1(l,r,1,n,1)-query2(l,r,1,n,1)); } } }