POJ 2777 Count Color

C - Count Color

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice POJ
2777

Appoint description: 
System Crawler  (2015-07-22)

Description

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment
with only one color. We can do following two operations on the board:

1. "C A B C" Color the board from segment A to segment B with color C.

2. "P A B" Output the number of different colors painted between segment A and segment B (including).

In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the
beginning, the board was painted in color 1. Now the rest of problem is left to your.

Input

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation
defined previously.

Output

Ouput results of the output operation in order, each line contains a number.

Sample Input

2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

Sample Output

2
1

有两种方法:一种是每次查询时都要统计对应区间延迟标记上颜色的种类,可以用set或简单哈希来实现。

一种是用二进制表示对应的区间涂了第几种颜色,这样每个区间除了延迟标记外,可以再开一个数组统计当前涂了哪几种颜色。这样就和一般的线段树一样了。

最后再统计一下1的数目。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
typedef long long LL;
int sum[maxn<<2],col[maxn<<2],ll[maxn<<2],rr[maxn<<2];
inline void pushup(int i){
    sum[i]=sum[i<<1]|sum[i<<1|1];
}
inline void pushdown(int i){
    if(col[i]){
        col[i<<1]=col[i<<1|1]=col[i];
        sum[i<<1]=col[i];
        sum[i<<1|1]=col[i];
        col[i]=0;
    }
}
void build(int l,int r,int i){
    ll[i]=l;
    rr[i]=r;
    col[i]=1;
    if(l==r)return;
    int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;
    build(l,m,ls);
    build(m+1,r,rs);
    pushup(i);
}
void update(int l,int r,int v,int i){
    if(l<=ll[i]&&rr[i]<=r){
        col[i]=1<<(v-1);
        sum[i]=col[i];
        return ;
    }
    pushdown(i);
    int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;
    if(l<=m)update(l,r,v,ls);
    if(m<r)update(l,r,v,rs);
    pushup(i);
}
int query(int l,int r,int i){
    if(l<=ll[i]&&rr[i]<=r){
        return sum[i];
    }
    pushdown(i);
    int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;
    int ans=0;
    if(l<=m)ans=ans|query(l,r,ls);
    if(m<r)ans=ans|query(l,r,rs);
    return ans;
}
int main()
{
    int l,t,o,a,b,c;
    char q[2];
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&l,&t,&o)){
        build(1,l,1);
        for(int i=0;i<o;i++){
            scanf("%s%d%d",q,&a,&b);
            if(a>b)swap(a,b);
            if(q[0]=='C'){
                scanf("%d",&c);
                update(a,b,c,1);
            }
            else {
                int res=query(a,b,1);
                int ans=0;
                while(res){
                    if(res&1)ans++;
                    res=res>>1;
                }
                printf("%d\n",ans);
            }
        }
    }
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
typedef long long LL;
int ll[maxn<<2],rr[maxn<<2],col[maxn<<2],vis[32];
int ans;
inline void pushdown(int i,int m){
    if(col[i]){
        col[i<<1]=col[i<<1|1]=col[i];
        col[i]=0;
    }
}
void build(int l,int r,int i){
   ll[i]=l;
   rr[i]=r;
   col[i]=1;
   if(l==r){
     return ;
   }
   int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;
   build(l,m,ls);
   build(m+1,r,rs);
}
void update(int l,int r,int v,int i){
   if(l<=ll[i]&&rr[i]<=r){
    col[i]=v;
    return ;
   }
   pushdown(i,rr[i]-ll[i]+1);
   int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;
   if(l<=m)update(l,r,v,ls);
   if(m<r) update(l,r,v,rs);
}
void query(int l,int r,int i){
   if(col[i]){
      if(!vis[col[i]]){
        ans++;
        vis[col[i]]=1;
      }
      return ;
   }
   pushdown(i,rr[i]-ll[i]+1);
   int m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;
   if(l<=m)query(l,r,ls);
   if(m<r)query(l,r,rs);
}
int main()
{
    int l,t,o,a,b,c;
    char q[2];
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&l,&t,&o)){
        build(1,l,1);
        for(int i=0;i<o;i++){
            scanf("%s%d%d",q,&a,&b);
            if(a>b)swap(a,b);
            if(q[0]=='C'){
                scanf("%d",&c);
                update(a,b,c,1);
            }
            else {
                ans=0;
                memset(vis,0,sizeof vis);
                query(a,b,1);
                printf("%d\n",ans);
            }
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-23 20:26:02

POJ 2777 Count Color的相关文章

POJ 2777 Count Color (线段树区间更新加查询)

Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly d

poj 2777 Count Color(线段树区间修改)

题目链接:http://poj.org/problem?id=2777 题目意思:就是问你在询问的区间里有几种不同的颜色 思路:这题和一般的区间修改差不多,但是唯一不同的就是我们要怎么计算有种颜色,所以这时候我们就需要把延时标记赋予不同的意义,当某段区间有多种颜色时就赋值为-1,当为一种颜色时就把它赋值为这个颜色的号数.这儿我们要怎么统计询问区间不同的颜色数叻,为了不重复计算同一种颜色,那么我们就需要用一个数组来标记计算过的颜色,当我们下次遇到时就不需要再次计算了.... 代码核心处就在计数那儿

poj 2777 Count Color【线段树段更新】

题目:poj 2777 Count Color 题意:给出一段1 * n 的栅栏,有两种操作,第一种:把 l -- r 全部染成同一颜色t,第二种,查询 l---r 一共有多少种颜色. 分类:线段树 分析:我们可以给每个节点加一个标记,标记当前节点是否只有一种颜色,然后对只有一种颜色的节点如果要染色的话,那么他会变成几种颜色的,这时候记得向下更新一次就好,统计的时候统计节点有单个颜色的颜色就好. 代码: #include <cstdio> #include <cstring> #i

POJ 2777 Count Color(线段树)

题目地址:POJ 2777 我去..延迟标记写错了.标记到了叶子节点上....这根本就没延迟嘛...怪不得一直TLE... 这题就是利用二进制来标记颜色的种类.然后利用或|这个符号来统计每个区间不同颜色种数. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h

POJ 2777 Count Color(线段树)

POJ 2777 Count Color 题目链接 就一个线段树,颜色二进制表示就可以,成段更新成段查询延迟操作 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define lson(x) ((x<<1)+1) #define rson(x) ((x<<1)+2) const int N = 100005; struct No

POJ 2777 Count Color (线段树+位运算)

题意很简单了,对一个区间有两种操作: 1. "C A B C" Color the board from segment A to segment B with color C. //A~B涂上颜色C 2. "P A B" Output the number of different colors painted between segment A and segment B (including). //输出A~B间颜色的种类数 题目链接:http://poj.o

POJ - 2777——Count Color(懒标记线段树二进制)

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53639   Accepted: 16153 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

poj 2777 Count Color (线段树区间更新)

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37647   Accepted: 11315 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

POJ 2777 Count Color(线段树 )

Language: Default Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36180   Accepted: 10927 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we