Count Color 線段樹成段更新 hoj &poj

/*注意在查詢的時候要將標記下方。*/
#include <stdio.h>
#include <cstring>
#define maxn 100001
int c[maxn<<2];
bool color[31];
int lazy[maxn<<2];
void swap(int &a,int &b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        c[rt]=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    c[rt]=1;
}
void pushdown(int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1]=lazy[rt];
        lazy[rt<<1|1]=lazy[rt];
        c[rt<<1]=lazy[rt];
        c[rt<<1|1]=lazy[rt];
        lazy[rt]=0;
    }
}
void update(int a,int b,int p,int l,int r,int rt)
{
    if(c[rt]==p) return ;
    if(a<=l&&b>=r)
    {
        c[rt]=p;
        lazy[rt]=p;
        return ;
    }
    pushdown(rt);
    int mid=(l+r)>>1;
    if(a<=mid) update(a,b,p,l,mid,rt<<1);
    if(b>mid) update(a,b,p,mid+1,r,rt<<1|1);
    c[rt]=0;
}
void query(int a,int b,int l,int r,int rt)
{   
    if(a<=l&&b>=r&&c[rt]>0)
    {
        color[c[rt]]=true;
        return ;
    }
    pushdown(rt);
    int mid=(l+r)>>1;
    if(a<=mid) query(a,b,l,mid,rt<<1);
    if(b>mid) query(a,b,mid+1,r,rt<<1|1);
}
int main()
{
    char op;
    int n,t,m;
    int p,q,f;
    while(scanf("%d%d%d",&n,&t,&m)==3)
    {
        memset(lazy,0,sizeof(lazy));
        build(1,n,1);
        while(m--)
        {
            scanf(" %c",&op);
            if(op=='C')
            {
                scanf("%d%d%d",&p,&q,&f);
                if(p>q) swap(p,q);
                update(p,q,f,1,n,1);
            }
            else if(op=='P')
            {
                memset(color,false,sizeof(color));
                scanf("%d%d",&p,&q);
                if(p>q) swap(p,q);
                query(p,q,1,n,1);
                int count=0;
                for(int i=1; i<=30; i++)
                    if(color[i]) count++;
                printf("%d\n",count);
            }
        }
    }
    return 0;
}


發佈了185 篇原創文章 · 獲贊 3 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章