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

我实在不知道我WA在哪个细节了,我Check好几遍了。真心求指教

Posted by yihuikang at 2012-03-07 22:26:32 on Problem 2777
#include <iostream>
using namespace std;

#define size 100001

struct seg_tree{
    int l,r,color;
}a[size*5];

bool bo[32];

void build(int i,int j,int k)
{
     a[k].l=i; a[k].r=j;
     a[k].color=1;
     if (i==j) return ;
     int m=(i+j)/2;
     build(i,m,k*2);
     build(m+1,j,k*2+1);
}

void update(int i,int j,int w,int k)
{
     if (a[k].l==i && a[k].r==j)
     {
         a[k].color=w;
         return ;
     }
     int m=(a[k].l+a[k].r)/2;
     if (j<=m)
     {
         if (a[k].color)
         {
             a[k*2].color=a[k].color;
             a[k*2+1].color=a[k].color;
         }   
         update(i,j,w,k*2);
         a[k].color=0;
     }
     else if (i>m)
     {
          if (a[k].color)
          {
              a[k*2].color=a[k].color;
              a[k*2+1].color=a[k].color;
          }
          update(i,j,w,k*2+1);
          a[k].color=0;
     }
     else
     {
         if (a[k].color)
         {
             a[k*2].color=a[k].color;
             a[k*2+1].color=a[k].color;
         }
         update(i,m,w,k*2);
         update(m+1,j,w,k*2+1);
         a[k].color=0;
     }
}

int total;
void query(int i,int j,int k)
{
     int x;
     if (a[k].l<=i && a[k].r>=j)
     {
         if ((x=a[k].color) && !bo[x])
         {
             total++;
             bo[x]=true;
             return ;
         }
     }
     if (a[k].l==a[k].r) return ;
     int m=(a[k].l+a[k].r)/2;
     if (j<=m) 
         query(i,j,k*2);
     else if (i>m)
         query(i,j,k*2+1);
     else
     {
         query(i,m,k*2);
         query(m+1,j,k*2+1);
     }
}

void print(int k)
{
     printf("(%d,%d)=%d\n",a[k].l,a[k].r,a[k].color);
     if (a[k].l==a[k].r) return ;
     print(k*2);
     print(k*2+1);
}

void swap(int &a,int &b)
{
     int x=a;
     a=b;
     b=x;
}

int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    
    int l,t,e,a,b,c;
    char ch;
    scanf("%d%d%d",&l,&t,&e);
    build(1,l,1);
    while (e--)
    {
          cin>>ch;
          if (ch=='C')
          {
              scanf("%d%d%d",&a,&b,&c);
              if (a>b) swap(a,b);
              update(a,b,c,1);
              //print(1); printf("\n");
          }
          else
          {
              scanf("%d%d",&a,&b);
              if (a>b) swap(a,b);
              total=0;
              memset(bo,0,sizeof(bo));
              query(a,b,1);
              printf("%d\n",total);
          }
    }
    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