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

线段树啊线段树啊.

Posted by Ysf_fiend at 2011-10-05 16:52:27 on Problem 2777
#include <iostream>
using namespace std;
bool cnt[35];
struct node
{
       int l,r,cl;
}t[400005];
void build(int i,int x,int y)
{
     t[i].cl=1;
     t[i].l=x;
     t[i].r=y;
     if(x>=y-1)return ;
     int mid=(x+y)/2;
     build(i*2,x,mid);
     build(i*2+1,mid,y);
}
void insert(int i,int x,int y,int col)
{
     if(t[i].l>=y||t[i].r<=x)return ;
     if(t[i].l>=x&&t[i].r<=y)
     {
         t[i].cl=col;
     }
     else
     {
         if(t[i].cl!=col)
         {
             if(t[i].cl>0)
             {
                       t[i*2].cl=t[i*2+1].cl=t[i].cl;
             }
             t[i].cl=0;
             insert(i*2,x,y,col);
             insert(i*2+1,x,y,col);
         }
     }
}
void my_cont(int i,int x,int y)
{
     if(t[i].l>=y||t[i].r<=x)return ;
     if(t[i].cl>0)
     cnt[t[i].cl]=1;
     else
     {
         my_cont(i*2,x,y);
         my_cont(i*2+1,x,y);
     }
}
int l,T,o;
int x,y,color;
char ml;
int main()
{
    scanf("%d%d%d",&l,&T,&o);
    build(1,0,l);
    for(int tmp=1;tmp<=o;tmp++)
    {
            cin>>ml;
            if(ml=='C')
            {
                       scanf("%d%d%d",&x,&y,&color);
                       insert(1,x-1,y,color);
            }
            else
            {
                       int ans=0;
                       scanf("%d%d",&x,&y);
                       my_cont(1,x-1,y);
                       for(int k=1;k<=T;k++)
                       if(cnt[k])ans++;
                       printf("%d\n",ans);
                       memset(cnt,0,sizeof(cnt));
            }
    }
}

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