zl程序教程

您现在的位置是:首页 >  其它

当前栏目

POJ 2777 Count Color(段树)

poj count Color
2023-09-14 09:08:03 时间

职务地址:POJ 2777

我去。。

延迟标记写错了。标记到了叶子节点上。。

这根本就没延迟嘛。。

。怪不得一直TLE。。。

这题就是利用二进制来标记颜色的种类。然后利用或|这个符号来统计每一个区间不同颜色种数。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
int sum[410000];
int lazy[410000];
int pow1[40];
void PushUp(int rt)
{
    sum[rt]=sum[rt<<1]|sum[rt<<1|1];
}
void PushDown(int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1]=lazy[rt];
        lazy[rt<<1|1]=lazy[rt];
        sum[rt<<1]=lazy[rt];
        sum[rt<<1|1]=lazy[rt];
        lazy[rt]=0;
    }
}
void update(int ll, int rr, int c, int l, int r, int rt)
{
    if(ll<=l&&rr>=r)
    {
        sum[rt]=c;
        lazy[rt]=c;
        return ;
    }
    PushDown(rt);
    int mid=l+r>>1;
    if(ll<=mid) update(ll,rr,c,lson);
    if(rr>mid) update(ll,rr,c,rson);
    PushUp(rt);
}
int query(int ll, int rr, int l, int r, int rt)
{
    if(ll<=l&&rr>=r)
    {
        return sum[rt];
    }
    PushDown(rt);
    int ans=0;
    int mid=l+r>>1;
    if(ll<=mid) ans=ans|query(ll,rr,lson);
    if(rr>mid) ans=ans|query(ll,rr,rson);
    return ans;
}
int get(int x)
{
    int ans=0, y;
    while(x)
    {
        y=x%2;
        if(y)
            ans++;
        x/=2;
    }
    return ans;
}
int main()
{
    int n, t, q, a, b, c, i;
    char ch;
    pow1[0]=1;
    for(i=1; i<=30; i++)
    {
        pow1[i]=pow1[i-1]*2;
    }
    scanf("%d%d%d",&n,&t,&q);
    memset(lazy,0,sizeof(lazy));
    for(i=1;i<=3*n;i++)
    {
        sum[i]=1;
    }
    while(q--)
    {
        getchar();
        scanf("%c",&ch);
        if(ch=='C')
        {
            scanf("%d%d%d",&a,&b,&c);
            if(a>b)
            {
                int tt=a;
                a=b;
                b=tt;
            }
            update(a,b,pow1[c-1],1,n,1);
        }
        else
        {
            scanf("%d%d",&a,&b);
            if(a>b)
            {
                int tt=a;
                a=b;
                b=tt;
            }
            int ans=get(query(a,b,1,n,1));
            printf("%d\n",ans);
        }
    }
    return 0;
}


版权声明:本文博主原创文章。博客,未经同意不得转载。