标签:整数 freopen out ons getchar 输出 continue using namespace
★★☆ 输入文件:climb.in
输出文件:climb.out
简单对比
时间限制:1 s 内存限制:512 MB
[1,n]
//RMQ裸题 #include<cstdio> #include<iostream> using namespace std; int read(){ int 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; } const int N=1e6+5; int n,m,p[21],l[N],f[N][21]; void pre_deal(){ for(int i=0;i<=20;i++){ p[i]=1<<i; if(p[i]<=n) l[p[i]]=i; } for(int i=2,near;i<=n;i++){ if(l[i]) near=l[i]; else l[i]=near; } } void RMQ(){ for(int j=1;j<=20;j++){ for(int i=1;i+p[j]-1<=n;i++){ f[i][j]=max(f[i][j-1],f[i+p[j-1]][j-1]); } } } int query(int i,int j){ int k=l[j-i+1]; return max(f[i][k],f[j-p[k]+1][k]); } int main(){ freopen("climb.in","r",stdin); freopen("climb.out","w",stdout); n=read(); for(int i=0;i<=n;i++) f[i][0]=read(); pre_deal(); RMQ(); m=read(); for(int i=1,x,y;i<=m;i++){ x=read();y=read(); if(!x&&!y){puts("0");continue;} if(!x) ++x; printf("%d\n",query(x,y)); } return 0; }
[0,n]
//RMQ裸题 #include<cstdio> #include<iostream> using namespace std; int read(){ int 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; } const int N=1e6+5; int n,m,p[21],l[N],f[N][21]; void pre_deal(){ for(int i=0;i<=20;i++){ p[i]=1<<i; if(p[i]<=n) l[p[i]]=i; } for(int i=2,near;i<=n+1;i++){//1W:10-0+1==11,so n+1 if(l[i]) near=l[i]; else l[i]=near; } } void RMQ(){ for(int j=1;j<=20;j++){ for(int i=0;i+p[j]-1<=n;i++){ f[i][j]=max(f[i][j-1],f[i+p[j-1]][j-1]); } } } int query(int i,int j){ int k=l[j-i+1]; return max(f[i][k],f[j-p[k]+1][k]); } int main(){ freopen("climb.in","r",stdin); freopen("climb.out","w",stdout); n=read(); for(int i=0;i<=n;i++) f[i][0]=read(); pre_deal(); RMQ(); m=read(); for(int i=1,x,y;i<=m;i++){ x=read();y=read(); printf("%d\n",query(x,y)); } return 0; }
标签:整数 freopen out ons getchar 输出 continue using namespace
原文地址:http://www.cnblogs.com/shenben/p/6368123.html