标签:while first 图片 lan rip namespace ati n+1 etc
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9378 | Accepted: 4420 |
Description
Input
Output
Sample Input
5 3 1 5 1 2 6 3 1 3 5 2 7 7 2 4 6 1 9 9 8 6 5 0 6 9 3 9 1 2
Sample Output
5
二维线段树,也可以一维+RMQ
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 #define lch (x<<1) 8 #define rch (x<<1|1) 9 #define mid (l+r)/2 10 int const N=250+10; 11 int t[N<<2][N<<2],s[N<<2][N<<2],n,b,k,a[N][N],ansx,ansy; 12 13 void buildy(int o,int x,int l,int r,int ll,int rr){ 14 if(l==r){ 15 if(ll==rr) { 16 t[o][x]=s[o][x]=a[ll][l]; 17 }else { 18 t[o][x]=min(t[o*2][x],t[o*2+1][x]); 19 s[o][x]=max(s[o*2][x],s[o*2+1][x]); 20 } 21 //cout<<o<<" "<<x<<" "<<t[o][x]<<" "<<s[o][x]<<endl; 22 return ; 23 } 24 buildy(o,lch,l,mid,ll,rr); 25 buildy(o,rch,mid+1,r,ll,rr); 26 t[o][x]=min(t[o][lch],t[o][rch]); 27 s[o][x]=max(s[o][lch],s[o][rch]); 28 } 29 void buildx(int x,int l,int r){ 30 if(l!=r){ 31 buildx(lch,l,mid); 32 buildx(rch,mid+1,r); 33 } 34 buildy(x,1,1,n,l,r); 35 } 36 void queryy(int o,int x,int l,int r,int y1,int y2){ 37 if(y1<=l && r<=y2){ 38 ansx=min(ansx,t[o][x]); 39 ansy=max(ansy,s[o][x]); 40 return; 41 } 42 if(y1<=mid) queryy(o,lch,l,mid,y1,y2); 43 if(y2>mid) queryy(o,rch,mid+1,r,y1,y2); 44 } 45 void queryx(int x,int l,int r,int x1,int x2,int y1,int y2){ 46 if(x1<=l && r<=x2) { 47 queryy(x,1,1,n,y1,y2); 48 return ; 49 } 50 if(x1<=mid) queryx(lch,l,mid,x1,x2,y1,y2); 51 if(x2>mid) queryx(rch,mid+1,r,x1,x2,y1,y2); 52 } 53 int main(){ 54 scanf("%d%d%d",&n,&b,&k); 55 for(int i=1;i<=n;i++) 56 for(int j=1;j<=n;j++) 57 scanf("%d",&a[i][j]); 58 buildx(1,1,n); 59 //cout<<"***"<<endl; 60 while (k--){ 61 int x,y; 62 scanf("%d%d",&x,&y); 63 ansx=300,ansy=-1; 64 queryx(1,1,n,x,x+b-1,y,y+b-1); 65 printf("%d\n",ansy-ansx); 66 } 67 return 0; 68 }
标签:while first 图片 lan rip namespace ati n+1 etc
原文地址:https://www.cnblogs.com/ZJXXCN/p/12239461.html