随机乱搞,调调参数勉强过
代码
1 #include<cstdio> 2 #include<vector> 3 #include<ctime> 4 #include<cstdlib> 5 #define pb push_back 6 using namespace std; 7 const int N =610100; 8 int n,m,i,j,a,b,c,v[N],ans,vis[N],stack[N],top; 9 vector<int> vec[N]; 10 int ef(int x,int y) 11 { 12 int l,r,m; 13 l=0; 14 r=vec[y].size()-1; 15 while (l<=r) 16 { 17 m=(l+r)>>1; 18 if (vec[y][m]>x) r=m-1;else l=m+1; 19 } 20 return r; 21 } 22 int main() 23 { 24 srand(12345678); 25 scanf("%d%d",&n,&m); 26 for (i=1;i<=n;i++) 27 { 28 scanf("%d",&v[i]); 29 vec[v[i]].pb(i); 30 } 31 for (i=1;i<=m;i++) 32 { 33 scanf("%d%d",&a,&b); 34 35 ans=0;top=0; 36 for (j=1;j<=25;j++) 37 { 38 c=v[a+rand()%(b-a+1)]; 39 if (!vis[c]) 40 { 41 vis[c]=1;stack[++top]=c; 42 if (ef(b,c)-ef(a-1,c)>(b-a+1)/2) 43 { 44 ans=c;break; 45 } 46 } 47 } 48 for (j=1;j<=top;j++) vis[stack[j]]=0; 49 printf("%d ",ans); 50 } 51 }