【POJ 2777】 Count Color

【题目链接】

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

【算法】

            线段树

【代码】

            

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h>  
using namespace std;
#define MAXN 100010

int N,T,Q,l,r,x,v;
char op[5];

struct SegmentTree
{
        struct Node
        {
                int l,r,s,tag;
        } Tree[MAXN<<2];
        inline void build(int index,int l,int r)
        {
                Tree[index].l = l;
                Tree[index].r = r;
                Tree[index].s = 1;
                if (l == r) return;
                int mid = (l + r) >> 1;
                build(index<<1,l,mid);
                build(index<<1|1,mid+1,r);
        }
        inline void pushdown(int index)
        {
                if (Tree[index].tag)
                {
                        Tree[index<<1].s = Tree[index<<1|1].s = 1 << (Tree[index].tag - 1);
                        Tree[index<<1].tag = Tree[index<<1|1].tag = Tree[index].tag;    
                        Tree[index].tag = 0;
                }    
        }        
        inline void update(int index)
        {    
                Tree[index].s = Tree[index<<1].s | Tree[index<<1|1].s;
        }
        inline void modify(int index,int l,int r,int val)
        {
                if (Tree[index].l == l && Tree[index].r == r)
                {    
                        Tree[index].s = 1 << (val - 1);
                        Tree[index].tag = val;
                        return;
                }
                pushdown(index);
                int mid = (Tree[index].l + Tree[index].r) >> 1;
                if (mid >= r) modify(index<<1,l,r,val);
                else if (mid + 1 <= l) modify(index<<1|1,l,r,val);
                else
                {
                        modify(index<<1,l,mid,val);
                        modify(index<<1|1,mid+1,r,val);
                }
                update(index);
        }
        inline int query(int index,int l,int r)
        {
                if (Tree[index].l == l && Tree[index].r == r) return Tree[index].s;
                pushdown(index);
                int mid = (Tree[index].l + Tree[index].r) >> 1;
                if (mid >= r) return query(index<<1,l,r);
                else if (mid + 1 <= l) return query(index<<1|1,l,r);
                else return query(index<<1,l,mid) | query(index<<1|1,mid+1,r);
        }
} S;
inline int calc(int x)
{
        int ret = 0;
        while (x)
        {
                if (x & 1) ret++;
                x >>= 1;            
        }
        return ret;
}

int main() 
{
    
        scanf("%d%d%d",&N,&T,&Q);
        S.build(1,1,N);
        while (Q--)
        {
                scanf("%s",&op);
                if (op[0] == 'C')
                {
                      scanf("%d%d%d",&l,&r,&x);
                      if (l > r) swap(l,r);
                      S.modify(1,l,r,x);
                }    else
                {
                        scanf("%d%d",&l,&r);
                        if (l > r) swap(l,r);
                        v = S.query(1,l,r);
                        printf("%d\n",calc(v));
                }
        }
        
        return 0;
    
}

 

posted @ 2018-06-27 13:50  evenbao  阅读(188)  评论(0编辑  收藏  举报