Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

线段树3333ms表示鸭梨很大多!

Posted by wocha at 2012-07-20 16:27:28 on Problem 3264
#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 0xFFFFFF
#define M 50009
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
using namespace std;
struct node
{
	int left,right,min,max;
};
node tree[M*3];
struct C
{
    int height,order;
}cow[M];
void built(int L,int R,int id)
{
    tree[id].left=L;
    tree[id].right=R;
	tree[id].min=INF;
	tree[id].max=-INF;
	if(R==L) return ;	
	int mid=(L+R)>>1;
	built(L,mid,2*id); 
	built(mid+1,R,2*id+1); 
	
}

void insert(int j,int i)
{
    //printf("%d---\n",i);
   // system("pause");
    if(tree[i].left==j&&tree[i].right==j)
    {
                    tree[i].min=cow[j].height;
                    tree[i].max=cow[j].height;
        return;
    }
    int mid=(tree[i].left+tree[i].right)/2;
    if(j<=mid)       insert(j,i*2);
    else if(j>mid)  insert(j,i*2+1);
    else 
    {
                     insert(j,i*2);
                     insert(j,i*2+1);
    }
    tree[i].min=Min(tree[i*2].min,tree[i*2+1].min);
    tree[i].max=Max(tree[i*2].max,tree[i*2+1].max);
    
}
int big=-INF,small=INF;
void  get_ans(int l,int r,int i)
{
    if(tree[i].left>=l&&tree[i].right<=r)
    {
                     big=Max(big,tree[i].max);
                     small=Min(small,tree[i].min);
                     return ;
    }
    int mid=(tree[i].left+tree[i].right)/2;
    if(r<=mid)        get_ans(l,r,i*2);
    else if(l>mid)   get_ans(l,r,i*2+1);
    else
    {
                      get_ans(l,mid,i*2);
                      get_ans(mid+1,r,i*2+1);
    }
    
}
int main()
{
	int n,m;
	 
	while(~scanf("%d%d",&n,&m))
	{
      int a;
      built(1,n,1);
      for(int i=1;i<=n;i++)
      {
            scanf("%d",&cow[i].height);
          
            insert(i,1);
      }
      
      while(m--)
     {
    /*   for(int i=1;i<n*2;i++)
      {
            printf("tree[%d]=%d  %d---%d-----%d\n",i,tree[i].left,tree[i].right,tree[i].min,tree[i].max);
        }
    */
      int b,c;
      scanf("%d%d",&b,&c);
      get_ans(b,c,1);
      printf("%d-----%d\n",big,small);
      big=-INF;
      small=INF;
    }
    }
	return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator