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

%s 超时?

Posted by andone at 2010-07-26 10:52:26 on Problem 2777
#include<iostream>
#define MAXN 200005
using namespace std;
struct segTree
{
    int l,r,c;
}st[MAXN*3];
int res[50];
void build(int l,int r,int num)
{
     st[num].l=l;
     st[num].r=r;
     st[num].c=1;
     if(l==r) return ;
     int mid=(l+r)>>1;
     build(l,mid,num<<1);
     build(mid+1,r,(num<<1)+1);
}
void update(int l,int r,int num,int c)
{
     if((st[num].l==st[num].r) || ((st[num].l==l) && (st[num].r==r)))
     {
          st[num].c=c; 
          return ;
     }
     if(st[num].c>=1)  
     {
          st[num<<1].c=st[(num<<1)+1].c=st[num].c;
          st[num].c=-1;
     } 
     int mid=(st[num].l+st[num].r)>>1;
     if( mid < l)
        update(l,r,(num<<1)+1,c);
     else if(mid >= r)
        update(l,r,num<<1,c);
     else
     {
         update(l,mid,num<<1,c);
         update(mid+1,r,(num<<1)+1,c);
     }
}
void query(int l,int r,int v)
{    
     if(st[v].c>0)
     {   
         res[st[v].c]=1;
         return ;
     }
     if(st[v].l==st[v].r) return ;
   
         int mid=(st[v].l+st[v].r)>>1;
         if(r<=mid)
         {   
             query(l,r,v<<1);
         }
         else if(l>mid)
         {   
             query(l,r,(v<<1)+1);
         }
         else
         {   
             query(l,mid,v<<1);
             query(mid+1,r,(v<<1)+1);
         }
    
}
int main()
{
    int l,o,t,a,b,c,tmp;
    char ch;//[2];
    scanf("%d%d%d",&l,&t,&o);
    build(1,l,1);
    for(int i=0;i<o;i++)
    {
         //scanf("%s",ch);
         getchar();
         ch=getchar();
         if(ch=='C')
         {
              scanf("%d%d%d",&a,&b,&c);
              if(a > b) { a^=b^=a^=b; }
              update(a,b,1,c);
         }
         else
         {
             scanf("%d%d",&a,&b);
             memset(res,0,sizeof(res));
             if(a > b)
             { a^=b^=a^=b; }
             query(a,b,1);
             int ans=0;
             for(int i=1;i<=30;i++)
             {
                 if(res[i])
                    ans++;
             }
             printf("%d\n",ans);
         }
    }
    //system("pause");
    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