zoukankan      html  css  js  c++  java
  • poj_2777线段树+位运算

    第一次没想到用位运算,不出意料的T了,,,

    PS:在床上呆了接近两个月后,我胡汉三又杀回来刷题啦~~

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    const int maxn=100004;
    using namespace std;
    int sum[maxn<<2];
    int col[maxn<<2];
    void pushDown(int rt)
    {
        if(sum[rt]){
        col[rt<<1]=col[rt<<1|1]=1<<(sum[rt]-1);
        sum[rt<<1]=sum[rt<<1|1]=sum[rt];
        sum[rt]=0;
        }
    }
    void update(int c,int L,int R,int l,int r,int rt)
    {
        if(l>=L&&R>=r)
        {
            sum[rt]=c;
            col[rt]=1<<(sum[rt]-1);
           // cout<<rt<<"##"<<col[rt]<<endl;
            return;
        }
        if(L>r||R<l)
            return ;
        if(sum[rt])
        pushDown(rt);
        int m=(l+r)>>1;
        if(m>=L) update(c,L,R,lson);
        if(m<R) update(c,L,R,rson);
        col[rt]=col[rt<<1]|col[rt<<1|1];
    }
    int query(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R)
        {
            return col[rt];
        }
        if(L>r||R<l)
            return 0;
        pushDown(rt);
        int m=(l+r)>>1;
        return query(L,R,lson)|query(L,R,rson);
        /*if(m>=L) query(L,R,lson);
        if(m<R) query(L,R,rson);
        return col[rt<<1]|col[rt<<1|1];*/
    }
    int main()
    {
        int l,t,o;
        while(scanf("%d%d%d",&l,&t,&o)!=EOF)
        {
            getchar();
            char p;
            int a,b,c;
            sum[1]=1;col[1]=1;
            for(int i=0; i<o; i++)
            {
                scanf("%c",&p);
                getchar();
                if(p=='P')
                {
                    scanf("%d%d",&a,&b);
                    if(a>b)
                    {
                        int x=a;a=b;b=x;
                    }
                    getchar();
                    int tem=query(a,b,1,l,1);
                    int ans=0;
                    //cout<<tem<<"tem"<<endl;
                    while(tem)
                    {
                        ans+=tem&1;
                        tem>>=1;
                    }
                    printf("%d
    ",ans);
                }
                else
                {
                    scanf("%d%d%d",&a,&b,&c);
                     if(a>b)
                    {
                        int x=a;a=b;b=x;
                    }
                    getchar();
                    update(c,a,b,1,l,1);
                    //cout<<col[2]<<"update"<<endl;
                }
            }
        }
        return 0;
    }

  • 相关阅读:
    超简单开发自己的php框架一点都不难
    .htaccess技巧: URL重写(Rewrite)与重定向
    htaccess附录:正则表达式、重定向代码
    编写自己的PHP MVC框架笔记
    微信企业号-上传、获取临时素材文件
    练习6.43:、6.45、6.46
    关于函数的特殊用途的语言特性的注意事项
    练习6.40、6.41
    练习6.39
    练习6.30、6.31、6.32
  • 原文地址:https://www.cnblogs.com/vactor/p/4099981.html
Copyright © 2011-2022 走看看