int LCA(int u,int v)
{
    int a=rank[u];
    int b=rank[v];
    if(a>b) return root[RMQ(b,a)];
    else return root[RMQ(a,b)];
}
Ejemplo n.º 2
0
void dfs(int x,int y)
{
	int mid,min;
	if(x>=y)
		return ;
    
	min=RMQ(x,y,0);


	mid=RMQ(x,y,1);

	if(mid-min>ans)
		ans=mid-min;

	dfs(x,mid-1);
	dfs(mid+1,y);
	
}
Ejemplo n.º 3
0
int main()
{
    int i,x,y,left,mid,right,ans;
    for(i=0;i<=20;i++)
	   p[i]=(1<<i);
 
    while(EOF!=scanf("%d %d",&n,&m))
    {
          for(i=1;i<=n;i++)
              scanf("%d",&a[i]);
          
          for(i=0;i<=2*INF;i++)
              flag[i]=n+1;
                  
          f[n+1]=n;
          for(i=n;i>=1;i--)
          {       
              f[i]=min(f[i+1],flag[a[i]+INF]-1);           
              flag[a[i]+INF]=i;
          }
          
          for(i=1;i<=n;i++)
          {
              max[i][0]=f[i]-i+1;
          
          }
          
          init();
          
          while(m--)
          {
             scanf("%d %d",&x,&y);
          
             x++,y++;
             
             left=x,right=y;
             while(right-left>1)
             {
                  mid=(left+right)/2;
                  if(f[mid]<y)
                       left=mid;
                  else
                       right=mid;
                  
             }
             
             ans=RMQ(x,left);
             if(y-left>ans)
                 ans=y-left;
             
             printf("%d\n",ans);
          }
    }

    return 0;
}
Ejemplo n.º 4
0
int main()
{
  int t,length,sum;
  while(EOF!=scanf("%d",&t))
  {
  if(t==0)
	  break;
  memset(str,0,sizeof(str));
  getchar();
  gets(str);
  len1=strlen(str);
  length=len1;
  str[len1++]=2;
  gets(str+len1);
  len2=strlen(str+len1);
  n=len1+len2;
  str[n++]=1;
  suffixArray();
  lcp();
  init();
  for(i=2;i<n;i++)
  {
	stack[i-1].index=SA[i]+1;
	if(stack[i-1].index>=9)
		stack[i-1].flag=true;
	else
		stack[i-1].flag=false;
  }
  length=2*length;sum=0;
  for(i=1;i<length;i++)
  {
    j=i+1;
	while(RMQ(stack[i].index,stack[j].index)>=t)
	{
		if(stack[i].flag!=stack[j].flag)
		   sum+=RMQ(stack[i].index,stack[j].index)-t+1;
		j++;
	}
  }
  printf("%d\n",sum);
  }
  return 0;
}
int getMaxAreaRect(int* hist, int* st, int n, int l, int r) {
	if (l > r) return INT_MIN;
	if (l == r) return hist[l];

	int m = RMQ(hist, st, n, l, r);

	return maxVal(getMaxAreaRect(hist, st, n, l, m- 1),
			getMaxAreaRect(hist, st, n, m + 1, r),
			(r-l+1)*hist[m]);
}
Ejemplo n.º 6
0
main()
{
        int N,i,Q,L,R,j;
        N=fastread();
        //scanf("%d",&N);
        int b[N];
        float C[N],maxm,minm;


        for(i=0; i<N; i++) {
                // scanf("%d",&b[i]);
                b[i]=fastread();
        }

        int *st = constructST(b, N);
        int *m_st=constructST_m(b,N);

        // scanf("%d",&Q);
        Q=fastread();

        for(j=0; j<Q; j++) {
                //scanf("%d",&L);
                //scanf("%d",&R);
                L=fastread();
                R=fastread();


                //call RMQ for fast processing
                minm= RMQ(st, N, L, R);

                for(i=0; i<L; i++) {
                        C[i]= (minm+b[i]);
                }
                for(i=R+1; i<N; i++) {
                        C[i]= (minm+b[i]);
                }

                for(i=L; i<=R; i++) {
                        C[i]= (float)(minm+b[i])/(float)(2);
                }

                maxm=C[0];
                for(i=1; i<N; i++) {
                        if(C[i]>maxm)
                                maxm=C[i];
                }
                printf("%.1f\n",maxm);
        }

        return 0;
}
Ejemplo n.º 7
0
// Driver program to test above functions
int main()
{
    int arr[] = {1, 3, 2, 7, 9, 11};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    // Build segment tree from given array
    int *st = constructST(arr, n);
 
    int qs = 1;  // Starting index of query range
    int qe = 5;  // Ending index of query range
 
    // Print minimum value in arr[qs..qe]
    printf("Minimum of values in range [%d, %d] is = %d\n",
                           qs, qe, RMQ(st, n, qs, qe));
 
    return 0;
}
Ejemplo n.º 8
0
int main()
{
 int i,j,t;
 for(i=0;i<=20;i++)
	 p[i]=(1<<i);
 while(EOF!=scanf("%d",&n)&&n)
 {
 scanf("%d",&t);
 for(i=1;i<=n;i++)
 {
	scanf("%d",&h[i]);
	f[i][0]=1;
	right[i][0]=left[i][0]=1;
 }
 init();
 while(t--)
 {
   scanf("%d%d",&i,&j);
   printf("%d\n",RMQ(i,j));
 }
 }
 return 0;
}
Ejemplo n.º 9
0
Archivo: nklineup.c Proyecto: nphuc/alg
int main(){
    long n,q;
    long *arr;   
    n=getLong();
    q=getLong();
    arr=malloc(sizeof(long)*(n+1));
    register long i;
    for(i=0;i<n;++i){
        arr[i]=getLong();
    }
    long *stmin=constructST(arr,n);
    long *stmax=constructST2(arr,n);
    i=q;
    long a,b;
    while(i--){
        a=getLong();
        b=getLong();
        printf("%ld\n",RMQ2(stmax,n,a-1,b-1)-RMQ(stmin,n,a-1,b-1));
    }
    free(arr);
    free(stmin);
    free(stmax);
    return 0;
}
Ejemplo n.º 10
0
int main()
{
 for(i=0;i<=21;i++)
	 p[i]=(1<<i);
 while(EOF!=scanf("%d%d",&n,&t))
 {
  for(i=1;i<=n;i++)
  {
	scanf("%d",&min[i][0]);
	max[i][0]=min[i][0];
  }
  init();
  for(i=1;i<=n-t+1;++i)
   RMQ(i,i+t-1);
  for(i=1;i<n-t+1;++i)
    printf("%d ",y[i]);
    printf("%d\n",y[i]);
  for(i=1;i<n-t+1;++i)
    printf("%d ",x[i]);
  printf("%d\n",x[i]);
 
 }
 return 0;
}
Ejemplo n.º 11
0
int LCA(int u, int v) {
    u = id[u]; v = id[v];
    if (u > v) std::swap (u, v);
    return E[RMQ (u, v)];
}