• POJ 2777 Count Color(段树)


    职务地址: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
    ",ans);
            }
        }
        return 0;
    }
    


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

  • 相关阅读:
    正则表达式常用公式
    造轮子之--Redis
    SqlServer 查询计划分析
    实现poster,json,base64等编码转码工具
    win nginx + php bat启动/停止脚本
    php设计模式——单例模式
    [php]php设计模式 (总结)
    升级openssl 操作记录
    PHP URL安全的Base64位编码
    php curl使用 常用操作
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4867654.html
Copyright © 2020-2023  润新知