最近OI颓废了。。。文化课也貌似要颓废了QAQ两者有点难调节啊。。。
先刷个水题。。。初看就想用线段树或BIT,其实可以用RMQ那。。。于是代码短了点
1 #include<cstdio> 2 #include<queue> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstring> 6 #include<cmath> 7 #define inc(i,l,r) for(i=l;i<=r;i++) 8 #define dec(i,l,r) for(i=l;i>=r;i--) 9 #define inf 1e9 10 #define mem(a) memset(a,0,sizeof(a)) 11 #define ll long long 12 #define succ(x) (1<<x) 13 #define NM 50000+5 14 using namespace std; 15 int read(){ 16 int x=0,f=1;char ch=getchar(); 17 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 18 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 19 return x*f; 20 } 21 int n,m,x,y,k,i,j,f[NM][25],d[NM][25],p=20; 22 int main(){ 23 n=read();m=read(); 24 inc(i,1,n)d[i][0]=f[i][0]=read(); 25 inc(j,1,p) 26 inc(i,1,n){ 27 f[i][j]=f[i][j-1];d[i][j]=d[i][j-1]; 28 if(i+succ(j-1)<=n){ 29 f[i][j]=max(f[i][j],f[i+succ(j-1)][j-1]); 30 d[i][j]=min(d[i][j],d[i+succ(j-1)][j-1]); 31 } 32 } 33 inc(i,1,m){ 34 x=read();y=read(); 35 k=log(y-x+1.0)/log(2.0); 36 printf("%d ",max(f[x][k],f[y-succ(k)+1][k])-min(d[x][k],d[y-succ(k)+1][k])); 37 } 38 return 0; 39 }