zoukankan      html  css  js  c++  java
  • poj 2777 Count Color

    Count Color
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 41291   Accepted: 12481

    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


    还是基础的线段树题目,和hdu 1698 非常类似。

    题意:输入板子长度,颜色数量,操作次数。输入C,再输入a,b,c,表示从a到b的段刷c颜色,输入P,再输入a,b,表示询问从a到b一共有几种不同的颜色。初始全部颜色为1。

    附上代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #define M 100005
     5 using namespace std;
     6 struct node
     7 {
     8     int l,r,s;
     9 } ss[M*3];
    10 int sum[52];
    11 
    12 void build(int l,int r,int k)
    13 {
    14     ss[k].l=l;
    15     ss[k].r=r;
    16     ss[k].s=1;
    17     if(l==r) return;
    18     int mid=(l+r)/2;
    19     build(l,mid,2*k);
    20     build(mid+1,r,2*k+1);
    21 }
    22 
    23 void insert(int l,int r,int x,int k)
    24 {
    25     if(ss[k].s==x) return;
    26     if(ss[k].l==l&&ss[k].r==r)
    27     {
    28         ss[k].s=x;
    29         return;
    30     }
    31     if(ss[k].s!=-1)
    32     {
    33         ss[k*2].s=ss[k*2+1].s=ss[k].s;
    34         ss[k].s=-1;
    35     }
    36     int mid=(ss[k].l+ss[k].r)/2;
    37     if(mid>=r) insert(l,r,x,k*2);
    38     else if(mid<l) insert(l,r,x,k*2+1);
    39     else
    40     {
    41         insert(l,mid,x,k*2);
    42         insert(mid+1,r,x,k*2+1);
    43     }
    44 }
    45 void search(int l,int r,int k)
    46 {
    47     if(ss[k].s!=-1)
    48     {
    49         sum[ss[k].s]=1;
    50         return;
    51     }
    52     else
    53     {
    54         int mid=(ss[k].l+ss[k].r)/2;
    55         if(r<=mid) search(l,r,2*k);
    56         else if(l>mid) search(l,r,2*k+1);
    57         else
    58         {
    59             search(l,mid,2*k);
    60             search(mid+1,r,2*k+1);
    61         }
    62     }
    63 }
    64 
    65 
    66 int main()
    67 {
    68     char ch;
    69     int a,b,c,t,o,n,i;
    70     while(~scanf("%d%d%d",&n,&t,&o))
    71     {
    72         build(1,n,1);
    73         while(o--)
    74         {
    75             getchar();
    76             scanf("%c",&ch);
    77             if(ch=='C')
    78             {
    79                 scanf("%d%d%d",&a,&b,&c);
    80                 insert(a,b,c,1);
    81             }
    82             else
    83             {
    84                 scanf("%d%d",&a,&b);
    85                 memset(sum,0,sizeof(sum));
    86                 int ans=0;
    87                 search(a,b,1);
    88                 for(i=1; i<=t; i++)
    89                     if(sum[i]) ans++;
    90                 printf("%d
    ",ans);
    91             }
    92         }
    93     }
    94     return 0;
    95 }

      
  • 相关阅读:
    java server: all kinds of errors
    fragment使用的错误
    unity3d+vuforia开发增强现实例子编译
    android遇到的几个问题
    cocos2dx 特效
    cchttpclient中停止网络请求的方法
    cocos2dx 2.2.5 hitWidget->onTouchEnded(pTouch, pEvent); 异常
    将博客搬至CSDN
    ffmpeg 编译Android
    常用注解
  • 原文地址:https://www.cnblogs.com/pshw/p/5333136.html
Copyright © 2011-2022 走看看