zoukankan      html  css  js  c++  java
  • POJ2777 Count Color[线段树求整段区间]

    Count Color
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 27655   Accepted: 8263

    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

    题意:整段区间染色

    code:

      1 #include<iostream>
      2 using namespace std;
      3 
      4 int l,t,o;
      5 
      6 struct Node
      7 {
      8     int l,r;
      9     int col;
     10 };
     11 struct Node node[100010*4];
     12 
     13 void build(int left,int right,int cur=1)
     14 {
     15     node[cur].l=left;
     16     node[cur].r=right;
     17     node[cur].col=0;
     18     if(left==right)
     19         return;
     20     int mid=(left+right)/2;
     21     build(left,mid,cur*2);
     22     build(mid+1,right,cur*2+1);
     23 }
     24 
     25 void update(int left,int right,int col,int cur=1)
     26 {
     27     if(left<=node[cur].l&&right>=node[cur].r)
     28     {
     29         node[cur].col=col;
     30         return;
     31     }
     32     if(node[cur].col>0)
     33     {
     34         node[cur*2].col=node[cur*2+1].col=node[cur].col;
     35         node[cur].col=0;
     36     }
     37     int mid=(node[cur].l+node[cur].r)/2;
     38     if(left>mid)
     39         update(left,right,col,cur*2+1);
     40     else if(right<=mid)
     41         update(left,right,col,cur*2);
     42     else
     43     {
     44         update(left,mid,col,cur*2);
     45         update(mid+1,right,col,cur*2+1);
     46     }
     47 }
     48 
     49 bool vst[40];
     50 int query(int left,int right,int cur=1)
     51 {
     52     int sum=0;
     53     if(node[cur].col)
     54     {
     55         if(!vst[node[cur].col])
     56         {
     57             sum++;
     58             vst[node[cur].col]=true;
     59         }
     60         return sum;
     61     }
     62     int mid=(node[cur].l+node[cur].r)/2;
     63     if(left>mid)
     64         sum+=query(left,right,cur*2+1);
     65     else if(right<=mid)
     66         sum+=query(left,right,cur*2);
     67     else
     68     {
     69         sum+=query(left,right,cur*2);
     70         sum+=query(left,right,cur*2+1);
     71     }
     72     return sum;
     73 }
     74 
     75 int main()
     76 {
     77     scanf("%d%d%d",&l,&t,&o);
     78     build(1,l);
     79     node[1].col=1;
     80     while(o--)
     81     {
     82         int a,b;
     83         char ch;
     84         getchar();
     85         scanf("%c%d%d",&ch,&a,&b);
     86         if(a>b)
     87         {
     88             int temp=a;
     89             a=b;
     90             b=temp;
     91         }
     92         if(ch=='C')
     93         {
     94             int c;
     95             scanf("%d",&c);
     96             update(a,b,c);
     97         }
     98         else
     99         {
    100             memset(vst,false,sizeof(vst));
    101             printf("%d\n",query(a,b));
    102         }
    103     }
    104     return 0;
    105 }
  • 相关阅读:
    cad是什么意思?教你快速把cad转换成pdf格式
    为什么街上的商贩更喜欢用微信支付,而不是支付宝,看完长知识了
    音乐剪辑软件怎么用?教你一个快速编辑音频的方法
    电脑如何录制视频?安利两种电脑录屏的方法
    被称为逆天改命的5大中国工程,曾轰动世界,你知道几个?
    如何使用音乐格式转换器?快速编辑音频文件的方法
    PPT结尾只会说“谢谢”?学会这些PPT结尾,观众主动为你鼓掌
    经典PHP面试题(冒泡排序),当场就被打脸,卧槽什么冒泡?为啥还排序?
    千万不要再搞混了,函数empty( var );输出的判断值是false : true
    PHP删除数组中空数组
  • 原文地址:https://www.cnblogs.com/XBWer/p/2665149.html
Copyright © 2011-2022 走看看