• 线段树(状态压缩二进制)


    http://poj.org/problem?id=2777

    题意:给出n(1e5)的长度的木棒,初始的颜色都为1,给出颜色种类t(1<=t<=30),给出q(1e5)个操作, C l r x 将l到r内的所有颜色更改为x , P l r 问在l到r内有多少种颜色。

    解法:观察发现颜色种类不多,将颜色转为二进制,每一位代表一种颜色,1表示有改颜色。

    用线段树存储下当前的每一段的颜色,更改用数组lazy标记,将颜色转化为二进制数,统计一段颜色时,对每一段可以对它的左右子树取 | 这样就可以统计这一段中的颜色出现的种类。

    //#include<bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    typedef long long ll ;
    #define int ll
    #define mod 1000000007
    #define gcd __gcd
    #define rep(i , j , n) for(int i = j ; i <= n ; i++)
    #define red(i , n , j)  for(int i = n ; i >= j ; i--)
    #define ME(x , y) memset(x , y , sizeof(x))
    //ll lcm(ll a , ll b){return a*b/gcd(a,b);}
    ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
    //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
    //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
    #define SC scanf
    #define INF  0x3f3f3f3f
    #define PI acos(-1)
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    using namespace std;
    const int N = 1e6+100;
    const int maxn = 1e5+9;
    int n ,  t , q ;
    int cl[maxn<<2] , lazy[maxn<<2];
    char s[5];
    
    void pushup(int rt){
        cl[rt] = cl[rt<<1] | cl[rt<<1|1];
    }
    void pushdown(int rt){
        lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
        cl[rt<<1] = cl[rt<<1|1] = lazy[rt];
        lazy[rt] = 0;
    }
    
    void build(int l , int r , int rt){
        cl[rt] = lazy[rt] = 0;
        if(l == r){
            cl[rt] = 1 ;
            return ;
        }
        int mid = (l + r) >> 1 ;
        build(lson);
        build(rson);
        pushup(rt);
    }
    void update(int L , int R , int l , int r , int rt , int c){
        if(l >= L && r <= R){
            cl[rt] = lazy[rt] = 1 << (c - 1) ;
            return ;
        }
        if(lazy[rt]) pushdown(rt);
        int mid = (l + r) >> 1;
        if(mid >= L) update(L , R , lson , c);
        if(mid < R) update(L , R , rson , c);
        pushup(rt);
    }
    int query(int L , int R , int l , int r , int rt){
        int sum = 0 ;
        if(l >= L && r <= R){
            return cl[rt];
        }
        if(lazy[rt]) pushdown(rt);
        int mid = (l + r) >> 1;
        if(mid >= L) sum |= query(L , R , lson);
        if(mid < R) sum |= query(L , R , rson);
        return sum;
    }
    int ans(int sum){
        int cnt = 0 ;
        while(sum){
            cnt += sum & 1 ;
            sum >>= 1 ;
        }
        return cnt ;
    }
    
    void solve(){
        scanf("%lld%lld%lld" , &n , &t , &q);
        build(1 , n , 1);
        while(q--){
            scanf("%s" , s);
            if(s[0] == 'C'){
                int l , r , c ;
                scanf("%lld%lld%lld" , &l , &r , &c);
                if(l > r) swap(l , r);
                update(l , r , 1 , n , 1 , c);
            }else{
                int l , r ;
                scanf("%lld%lld" , &l , &r);
                if(l > r) swap(l , r);
                cout << ans(query(l , r , 1 , n , 1)) << endl;
            }
    
        }
    
    }
    
    signed main()
    {
        //ios::sync_with_stdio(false);
        //cin.tie(0); cout.tie(0);
    
        solve();
    
    }
    
  • 相关阅读:
    Express ejs 3.* layout.ejs
    old header
    mac 命令行 安装 需要管理员 权限
    Understanding the Debug Log
    insufficient_access_on_cross_reference_entity APEX / Salesforce
    custom list view
    exam help
    Backbone.js Wine Cellar 教程
    理解RESTful架构
    SpringCloud入门之应用程序上下文服务(Spring Cloud Context)详解
  • 原文地址:https://www.cnblogs.com/nonames/p/12490279.html
Copyright © 2020-2023  润新知