POJ 2777-Count Color

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 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


只会死套模板的我咋一看没什么想法,结合了之前区间修改的经验,借鉴了题解,发现其实还是个模板题……只不过换了个方式处理区间。

本题中两种操作一种是对特定区间涂色,一种是让算特定区间的颜色种数。解决涂色问题主要在于找出求和与涂色的异同。之前用到的区间求和即是通过将大区间分割,供查询区间求和。而涂色时思想基本相同,亦是将区间二分处理。若区间在修改区间范围内,则颜色变为修改色,若在区间范围外,则继续判断是完全与修改区间无交界,或者有交界。

无交界的结束判断,有交界的继续分割。由于颜色数量很少,最后剩下的颜色即通过数字标为1,不存在的颜色标为0,统计即可。

有一句废话:记得每次清楚标记颜色的数组。本来是个常识但是一下子没意识到这点还wa了几发……

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3

struct Node{
    int left,right,num;
}tree[400005];

int c1[35];

void build(int l,int r,int k){
    tree[k].left=l;
    tree[k].right=r;
    tree[k].num=1;
    if (l>=r) return;
    int m=(l+r)/2;
    build(l, m, k*2);
    build(m+1, r, k*2+1);
}

void update(int l,int r,int k,int color){
    if (tree[k].left>r||tree[k].right<l) {
        return;
    }
    if (l<=tree[k].left&&tree[k].right<=r) {
        tree[k].num=color;
        return;
    }
    if (tree[k].num) {
        tree[k*2].num=tree[k*2+1].num=tree[k].num;
        tree[k].num=0;
    }
    update(l, r, k*2, color);
    update(l, r, k*2+1, color);
}

void search(int l,int r,int k){
    if (tree[k].left>r||tree[k].right<l) {
        return ;
    }
    if (tree[k].num) {
        c1[tree[k].num]=1;
        return;
    }
    search(l, r, k*2);
    search(l, r, k*2+1);
}

int main()
{
    int t,n,o,a,b,c;
    char s[2];
    scanf("%d%d%d",&n,&t,&o);
    build(1, n, 1);
    for (int i=0; i<o; i++) {
        scanf("%s",s);
        if (s[0]=='C') {
            scanf("%d %d %d",&a,&b,&c);
            update(a, b,1, c);
        }
        else {
            scanf("%d %d",&a,&b);
            memset(c1, 0, sizeof(c1));
            int sum=0,t1;
            if (a>b) {
                t1=a;
                a=b;
                b=t1;
            }
            search(a,b,1);
            for (int i=1; i<=t; i++) {
                sum+=c1[i];
            }
            printf("%d\n",sum);
        }
    }
    return 0;
}


你可能感兴趣的:(POJ 2777-Count Color)