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 }
  • 相关阅读:
    Informix日期获取上周上月昨天去年SQL
    PDI-KETTLE-4 使用Kettle完成通用DB生成指定文件并通过FTP上传
    日常问题解决记录二:DOS下切换盘符和工作目录
    PDI-KETTLE-3:数据库连接
    window下安装node.js
    【原创】正则断言的使用--为自动生成的get方法添加注解字段
    【原创】文本工具的使用--根据数据库字段快速生成该表对应的Model类属性
    【原创】字符串工具类--驼峰法与下划线法互转
    【原创】字符串工具类--获取汉字对应的拼音(全拼或首字母)
    【原创】关于oracle11G空表无法导出问题的解决方法
  • 原文地址:https://www.cnblogs.com/XBWer/p/2665149.html
Copyright © 2011-2022 走看看