标签:poi 图片 .com set out output ssi freopen pre
题意:
思路:
C++
1 #include<map> 2 #include<set> 3 #include<cmath> 4 #include<cstdio> 5 #include<vector> 6 #include<cstring> 7 #include<cstdlib> 8 #include<iostream> 9 #include<algorithm> 10 #define M 1000000 11 #define MAXN 210000 12 using namespace std; 13 14 int g[MAXN][20]; 15 int last[M*3]; 16 int a[MAXN],st[MAXN],f[MAXN]; 17 18 int ask(int x,int y) 19 { 20 if(x>y) return 0; 21 int len=y-x+1; int l=log(len)/log(2); 22 return max(g[x][l],g[y-(1<<l)+1][l]); 23 } 24 25 int main() 26 { 27 freopen("data.in","r",stdin); 28 freopen("1.out","w",stdout); 29 int n,m,x,y; 30 scanf("%d%d",&n,&m); 31 for(int i=1;i<=n;i++) 32 { 33 scanf("%d",&a[i]); 34 a[i]+=M; 35 st[i]=max(st[i-1],last[a[i]]+1); 36 last[a[i]]=i; 37 g[i][0]=f[i]=i-st[i]+1; 38 } 39 int l=log(n)/log(2); 40 for(int i=1;i<=l;i++) 41 for(int j=1;j+(1<<(i-1))<=n;j++) 42 g[j][i]=max(g[j][i-1],g[j+(1<<(i-1))][i-1]); 43 for(int i=1;i<=m;i++) 44 { 45 scanf("%d%d",&x,&y); 46 x++; y++; 47 int l=x; int r=y; 48 int tmp=x; 49 while(l<=r) 50 { 51 int mid=(l+r)/2; 52 if(st[mid]<x) {tmp=mid;l=mid+1;} 53 else r=mid-1; 54 } 55 int ans=max(tmp-x+1,ask(tmp+1,y)); 56 printf("%d\n",ans); 57 } 58 return 0; 59 } 60
pascal
1 const maxn=200000; 2 max=1000000; 3 var n,m,i,x,y,t,j,ans,keypoint,l,r:longint; 4 a,opt,pre:array[1..maxn]of longint; 5 rmq:array[1..maxn,0..20]of longint; 6 f:array[-max..max]of longint; 7 ch:char; 8 function find(x,y:longint):longint; 9 var left,right,mid:longint; 10 begin 11 left:=x;right:=y; 12 while left<=right do begin 13 mid:=(left+right)shr 1; 14 if pre[mid]<=y then left:=mid+1 15 else right:=mid-1; 16 end; 17 find:=left; 18 end; 19 function findmax(x,y:longint):longint; 20 var i,j:longint; 21 begin 22 i:=0;j:=1; 23 while 2*j<=y-x+1 do begin 24 inc(i); 25 j:=j*2; 26 end; 27 if rmq[x,i]>rmq[y+1-j,i] then findmax:=rmq[x,i] 28 else findmax:=rmq[y+1-j,i]; 29 end; 30 begin 31 assign(input,‘data.in‘); reset(input); 32 assign(output,‘right.out‘); rewrite(output); 33 readln(n,m); 34 for i:=1 to n do read(a[i]); 35 fillchar(f,sizeof(f),0); 36 opt[n]:=1; 37 pre[n]:=n; 38 f[a[n]]:=n; 39 for i:=n-1 downto 1 do begin 40 opt[i]:=opt[i+1]+1; 41 pre[i]:=pre[i+1]; 42 if (f[a[i]]>0)and(f[a[i]]-i<opt[i]) then begin 43 opt[i]:=f[a[i]]-i; 44 pre[i]:=f[a[i]]-1; 45 end; 46 f[a[i]]:=i; 47 end; 48 fillchar(rmq,sizeof(rmq),0); 49 for i:=1 to n do rmq[i,0]:=opt[i]; 50 i:=1;t:=2; 51 while t<=n do begin 52 for j:=1 to n-t+1 do begin 53 if rmq[j,i-1]>rmq[j+(1 shl (i-1)),i-1] then rmq[j,i]:=rmq[j,i-1] 54 else rmq[j,i]:=rmq[j+(1 shl (i-1)),i-1] 55 end; 56 inc(i); 57 t:=t*2; 58 end; 59 for i:=1 to m do begin 60 readln(l,r); 61 inc(l);inc(r); 62 if l>r then begin 63 j:=l;l:=r;r:=j; 64 end; 65 keypoint:=find(l,r); 66 ans:=0; 67 if keypoint>l then ans:=findmax(l,keypoint-1); 68 if (keypoint<=r)and(r-keypoint+1>ans) then begin 69 ans:=r-keypoint+1; 70 end; 71 writeln(ans); 72 end; 73 close(input); 74 close(output); 75 end.
标签:poi 图片 .com set out output ssi freopen pre
原文地址:https://www.cnblogs.com/myx12345/p/9283577.html