题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差
//1085422276 #include <iostream> #include <stdio.h> #include <vector> #include <algorithm> #include <string> #include <stack> #include <math.h> #include <vector> #include <string.h> using namespace std; typedef __int64 ll; const int inf = (int)1E9+10; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } //******************************* struct ss { int l,r,mn,ma; }tr[800001]; int n,q,a[200001],nn,mm; void build(int k,int s,int t) { tr[k].l=s; tr[k].r=t; if(s==t){ tr[k].mn=tr[k].ma=a[s]; return ; } int mid=(s+t)>>1; build(k<<1,s,mid); build(k<<1|1,mid+1,t); tr[k].ma=max(tr[k<<1].ma,tr[k<<1|1].ma); tr[k].mn=min(tr[k<<1].mn,tr[k<<1|1].mn); } void ask(int k,int s,int t) { if(tr[k].ma<=nn&&tr[k].mn>=mm)return; if(s==tr[k].l&&t==tr[k].r) { nn=max(tr[k].ma,nn); mm=min(tr[k].mn,mm); return; } int mid=(tr[k].l+tr[k].r)>>1; if(t<=mid) ask(k<<1,s,t); else if(s>mid) ask(k<<1|1,s,t); else { ask(k<<1,s,mid); ask(k<<1|1,mid+1,t); } } int main() { while(scanf("%d%d",&n,&q)!=EOF) { for(int i=1;i<=n;i++){ scanf("%d",&a[i]); }build(1,1,n);int x,y; for(int i=1;i<=q;i++){ nn=-inf; mm=inf; scanf("%d%d",&x,&y); ask(1,x,y); printf("%d\n",nn-mm); } } return 0; }
时间: 2024-10-13 03:15:20