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

Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 43629 Accepted: 13205

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


题意就是有L个排成一排的气球和T种颜色,对于气球有2种操作,分别是把从A到B的气球都染成颜色C,第二种是询问从A到B的气球有多少种不同的颜色,一共有O次操作。说实话刚刚看题觉得题挺简单的,就是一个裸的线段树区间延迟查询,但是但是坑点真的

除了要注意A和B的大小不确定外,就是数据的贼多,试了几发都是TLE,网上一查原来是要用二进制来记录颜色,不过实在不会,所以就找了了一种方法来减少递归的次数:就是你要查询 3 ~ 4时,而 如果1~8 都是同一种颜色, 3~4是 1~8的子区间,所以你只要在1~8区间的时候判断一下颜色是否出现过就行,并不用递归到 3~4区间,这样就减少了递归的次数!不懂延迟查询的自己学,这里就不再多说。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int big = 1e5 + 7;
struct node
{
    int l, r, data, mid;
} a[big<<2];
bool vis[55];
int hehe;
void Buildtree(int key, int l, int r)
{
    a[key].l = l;
    a[key].r = r;
    a[key].data = 1;
    a[key].mid = (l + r)>>1;
    if(l == r) return;
    Buildtree( key<<1, l, a[key].mid);
    Buildtree( key<<1|1, a[key].mid + 1, r);
}
void delay(int key)
{
    a[key<<1].data = a[key<<1|1].data = a[key].data;
    a[key].data = 0;
}
void update(int key, int l, int r, int w)
{
    if(a[key].l == l && a[key].r == r)
    {
        a[key].data = w;
        return;
    }
    if(a[key].data == w) return;
    if(a[key].data) delay(key);
    if(r <= a[key].mid) update( key<<1, l, r, w);
    else if( l > a[key].mid) update( key<<1|1, l, r, w);
    else
    {
        update( key<<1, l, a[key].mid, w);
        update( key<<1|1, a[key].mid + 1, r, w);
    }
}
void Show(int key, int l, int r)
{
    if(a[key].data != 0)
    {
        vis[a[key].data] = true;
        return;
    }
    if(r <= a[key].mid) Show( key<<1, l, r);
    else if(l > a[key].mid) Show( key<<1|1, l, r);
    else
    {
        Show( key<<1, l, a[key].mid);
        Show( key<<1|1, a[key].mid + 1, r);
    }
}
int main()
{
    int n, m, t, x, y, z;
    char k[20];
    scanf("%d %d %d", &n, &t, &m);
    Buildtree( 1, 1, n);
    for(int i = 1; i <= m; i ++)
    {
        scanf("%s", k);
        if(k[0] == 'C')
        {
            scanf("%d %d %d", &x, &y, &z);
            if(x > y) swap( x, y);
            update( 1, x, y, z);
        }
        else
        {
            memset( vis, false, sizeof( vis));
            scanf("%d %d", &x, &y);
            if(x > y) swap( x, y);
            Show( 1, x, y);
            hehe = 0;
            for(int i = 1; i <= t; i ++) if(vis[i]) hehe ++;
            printf("%d\n", hehe);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值