练下线段树。。。明天加油(准确的说是后天吧。。。)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #include<iostream> 6 #define inc(i,l,r) for(i=l;i<=r;i++) 7 #define dec(i,l,r) for(i=l;i>=r;i--) 8 #define inf 1e9 9 #define mem(a) memset(a,0,sizeof(a)) 10 #define ll long long 11 #define succ(x) (1<<x) 12 #define NM 600000+5 13 using namespace std; 14 int read(){ 15 int x=0,f=1;char ch=getchar(); 16 while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();} 17 while(isdigit(ch))x=x*10+ch-‘0‘,ch=getchar(); 18 return x*f; 19 } 20 struct info{ 21 int max,min,l,r; 22 info(int x=0):max(x),min(x),r(0),l(0){} 23 }T[NM],null; 24 int n,m,_x,_y; 25 info operator+(const info&x,const info&y){ 26 info f; 27 f.min=min(x.min,y.min); 28 f.max=max(x.max,y.max); 29 f.l=max(x.l,y.l);f.r=max(x.r,y.r); 30 f.l=max(f.l,y.max-x.min);f.r=max(f.r,x.max-y.min); 31 return f; 32 } 33 void build(int i,int x,int y){ 34 int t=x+y>>1; 35 if(x==y){ 36 T[i]=info(read()); 37 return; 38 } 39 build(i<<1,x,t);build(i<<1|1,t+1,y); 40 T[i]=T[i<<1]+T[i<<1|1]; 41 } 42 info ask(int i,int x,int y){ 43 int t=x+y>>1; 44 if(_x>y||_y<x)return null; 45 if(_x<=x&&y<=_y)return T[i]; 46 return ask(i<<1,x,t)+ask(i<<1|1,t+1,y); 47 } 48 int main(){ 49 n=read(); 50 build(1,1,n); 51 null.l=null.r=null.max=-inf;null.min=inf; 52 m=read(); 53 while(m--){ 54 _x=read();_y=read(); 55 if(_x<=_y)printf("%d\n",ask(1,1,n).l); 56 else{ 57 swap(_x,_y); 58 printf("%d\n",ask(1,1,n).r); 59 } 60 } 61 return 0; 62 }
时间: 2024-08-09 07:39:46