poj 2777 Count Color(线段树)

题目地址:http://poj.org/problem?id=2777

Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 30995 Accepted: 9285

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

【题解】:典型线段树区间更新

【code】:

  1 /**
  2 result:Accepted        memory:4264K
  3 time:375MS       language:C++
  4 code lenght: 2147B   Acmer:cj
  5 */
  6 
  7 #include<iostream>
  8 #include<stdio.h>
  9 #include<string.h>
 10 #include<algorithm>
 11 
 12 #define N 100010
 13 #define lson p<<1
 14 #define rson p<<1|1
 15 using namespace std;
 16 
 17 struct Nod
 18 {
 19     int l,r;
 20     int color;  //颜色
 21     int flag;   //[l,r]区间是否同色
 22 }node[N<<2];
 23 
 24 int mark[50];  //颜色统计标记
 25 
 26 void building(int l,int r,int p)  //建树
 27 {
 28     node[p].l = l;
 29     node[p].r = r;
 30     node[p].color = 1;
 31     node[p].flag = 1;
 32     if(l==r)    return;
 33     int mid = (l+r)>>1;
 34     building(l,mid,lson);
 35     building(mid+1,r,rson);
 36 }
 37 
 38 void update(int l,int r,int p,int c)
 39 {
 40     if(node[p].l==l&&node[p].r==r)
 41     {
 42         node[p].flag  = 1;
 43         node[p].color = c;
 44         return;
 45     }
 46     if(node[p].color!=c&&node[p].flag)  //向下更新
 47     {
 48         node[p].flag = 0;
 49         node[lson].flag=node[rson].flag = 1;
 50         node[lson].color=node[rson].color = node[p].color;
 51     }
 52     int mid = (node[p].l+node[p].r)>>1;
 53     if(r<=mid)  update(l,r,lson,c);
 54     else if(l>mid)  update(l,r,rson,c);
 55     else
 56     {
 57         update(l,mid,lson,c);
 58         update(mid+1,r,rson,c);
 59     }
 60     if(node[lson].flag&&node[rson].flag&&node[lson].color==node[rson].color)  //向上更新
 61     {
 62         node[p].flag = 1;
 63         node[p].color = node[lson].color;
 64     }
 65 }
 66 
 67 void query(int l,int r,int p)
 68 {
 69      if(node[p].flag)  //区间同色
 70      {
 71          mark[node[p].color]=1;  //标记颜色出现过
 72          return;
 73      }
 74      int mid = (node[p].l+node[p].r)>>1;
 75      if(r<=mid)  query(l,r,lson);
 76      else if(l>mid)  query(l,r,rson);
 77      else
 78      {
 79          query(l,mid,lson);
 80          query(mid+1,r,rson);
 81      }
 82 }
 83 
 84 int main()
 85 {
 86     int L,T,O;
 87     scanf("%d%d%d",&L,&T,&O);
 88     building(1,L,1);
 89     while(O--)
 90     {
 91         char op[5];
 92         int a,b,c;
 93         scanf("%s",op);
 94         if(op[0]=='C')
 95         {
 96             scanf("%d%d%d",&a,&b,&c);
 97             update(a,b,1,c);
 98         }
 99         else if(op[0]=='P')
100         {
101             int i;
102             scanf("%d%d",&a,&b);
103             memset(mark,0,sizeof(mark));  //初始化颜色标记
104             query(a,b,1);
105             int ans = 0;
106             for(i=1;i<=T;i++)  if(mark[i])  ans++;
107             printf("%d\n",ans);
108         }
109     }
110     return 0;
111 }

 

转载于:https://www.cnblogs.com/crazyapple/p/3236516.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值