码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2777线段树染色+lazy

时间:2014-08-23 11:20:40      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   ar   2014   问题   html   log   amp   

第一次写这样的题目,反正出现了各种坑爹问题,调了老半天,除了延迟标记外,这题还要开一个cnt数组用来存储各个区间内颜色的种类数,

每次改变颜色时,更新一次。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
using namespace std;
#define INF 0xfffffff
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define maxn 100005
int col[maxn<<2],cnt[maxn<<2];
void pushdown(int rt)
{
    if(col[rt]!=-1)
    {
        cnt[rt<<1]=cnt[rt<<1|1]=1<<(col[rt]-1);
        col[rt<<1]=col[rt<<1|1]=col[rt];
        col[rt]=-1;
    }
}
void build()
{
    col[1]=1;
    cnt[1]=1;
}
void update(int L,int R,int cha,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        col[rt]=cha;
        cnt[rt]=1<<(cha-1);
        return ;
    }
    if(L>r||R<l) return ;
    pushdown(rt);
    int mid=(l+r)>>1;
    update(L,R,cha,lson);
    update(L,R,cha,rson);
    cnt[rt]=cnt[rt<<1]|cnt[rt<<1|1];
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return cnt[rt];
    }
    if(L>r||R<l) return 0;
    pushdown(rt);
    int mid=(l+r)>>1;
    return query(L,R,lson)|query(L,R,rson);
}
int main()
{
    int n,t,o;
    char str[100];
    int a,b,c;
    build();
    scanf("%d%d%d",&n,&t,&o);
    while(o--)
    {
        scanf("%s",str);
        if(str[0]=='C')
        {
            scanf("%d%d%d",&a,&b,&c);
            if(a>b) swap(a,b);
            update(a,b,c,1,n,1);
        }
        else
        {
            int ans=0;
            scanf("%d %d",&a,&b);
            if(a>b) swap(a,b);
            int temp=query(a,b,1,n,1);
            while(temp)
            {
                if(temp&1) ans++;
                temp=temp>>1;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}


POJ2777线段树染色+lazy

标签:blog   os   io   ar   2014   问题   html   log   amp   

原文地址:http://blog.csdn.net/u013748887/article/details/38776825

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!