(线段树统计区间种数)Count Color

Count Color

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 54359 Accepted: 16337

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

Source

POJ Monthly--2006.03.26,dodo

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

题意:操作1:将区间 a - b 都变成 c

           操作2:找出区间 a - b 的种类个数。

题解:变成二进制分析要变成某个值的颜色,将该结点更新为 (1<<(vel-1),初始全为 1。

坑点:1, a 不一定比 b 小;

           2,记得将 v[](下拉标记清0)

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=(tree[rt<<1],tree[rt<<1|1]);
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1)  (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
int tree[N<<2],v[N<<2];

void sett(int l,int r,int rt){
    if(l==r){
        tree[rt]=1;
        return ;
    }
    int mid=l+r>>1;
    sett(l,mid,rt<<1);
    sett(mid+1,r,rt<<1|1);
    tree[rt]=tree[rt<<1]|tree[rt<<1|1];
}

void fun(int l,int r,int rt){
    if(v[rt]){
        v[rt<<1]=v[rt];
        v[rt<<1|1]=v[rt];
        tree[rt<<1]=tree[rt];
        tree[rt<<1|1]=tree[rt];
        v[rt]=0;
    }
}

void upset(int x,int y,int vel,int l,int r,int rt){
    if(x<=l&&y>=r){
        v[rt]=1<<(vel-1);
        tree[rt]=1<<(vel-1);
        return ;
    }
    fun(l,r,rt);
    int mid=l+r>>1;
    if(x<=mid)
        upset(x,y,vel,l,mid,rt<<1);
    if(y>mid)
        upset(x,y,vel,mid+1,r,rt<<1|1);
    tree[rt]=tree[rt<<1]|tree[rt<<1|1];
}

int findd(int x,int y,int l,int r,int rt){
    if(x<=l&&y>=r)
        return tree[rt];
    fun(l,r,rt);
    int ans=0;
    int mid=l+r>>1;
    if(x<=mid)
        ans|=findd(x,y,l,mid,rt<<1);
    if(y>mid)
        ans|=findd(x,y,mid+1,r,rt<<1|1);
    return ans;
}
int main(){
    int n,t,m;
    scanf("%d %d %d",&n,&t,&m);
    getchar();
    sett(1,n,1);
    char c; int a,b,d;
    while(m--){
        scanf(" %c",&c);
        if(c=='C'){
            scanf("%d %d %d",&a,&b,&d);
            if(b<a) swap(a,b);
            upset(a,b,d,1,n,1);
        }
        else{
            scanf("%d %d",&a,&b);
            if(b<a) swap(a,b);
            int ans=findd(a,b,1,n,1);
           // printf("ans == %d\n",ans);
            bitset<32>bit(ans);
            printf("%d\n",bit.count());
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值