zoukankan      html  css  js  c++  java
  • POJ 2777 Count Color(线段树之成段更新)

    Count Color

    Time Limit: 1000MS
    Memory Limit: 65536K

    Total Submissions: 33311
    Accepted: 10058

    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

                                                                                               [Submit]   [Go Back]   [Status]   [Discuss]

    ::会线段树的很容易知道怎么做这道题,这道题关键要解决重复计算的颜色,我用have[]来表示该颜色是否已经计算过

       1: #include <iostream>
       2: #include <cstdio>
       3: #include <cstring>
       4: #include <cmath>
       5: #include <algorithm>
       6: using namespace std;
       7: typedef long long ll;
       8: #define lson l,m,rt<<1
       9: #define rson m+1,r,rt<<1|1
      10: const int N=100000;
      11: int col[N<<2],t,sum;
      12: bool have[50];
      13:  
      14: void Down(int rt)
      15: {
      16:     if(col[rt]){
      17:         col[rt<<1]=col[rt<<1|1]=col[rt];
      18:         col[rt]=0;
      19:     }
      20: }
      21:  
      22: void build(int l,int r,int rt)
      23: {
      24:     col[rt]=1;
      25:     if(l==r) return;
      26:     int m=(l+r)>>1;
      27:     build(lson);
      28:     build(rson);
      29: }
      30:  
      31: void update(int L,int R,int c,int l,int r,int rt)
      32: {
      33:     if(L<=l&&R>=r){
      34:         col[rt]=c;
      35:         return;
      36:     }
      37:     Down(rt);
      38:     int m=(l+r)>>1;
      39:     if(L<=m) update(L,R,c,lson);
      40:     if(R>m) update(L,R,c,rson);
      41: }
      42:  
      43: void query(int L,int R,int l,int r,int rt)
      44: {
      45:     if(sum==t) return;
      46:     if(col[rt]) {
      47:         if(!have[col[rt]]){
      48:             have[col[rt]]=true;
      49:             sum++;
      50:         }
      51:         return;
      52:     }
      53:     Down(rt);
      54:     int m=(l+r)>>1;
      55:     if(L<=m) query(L,R,lson);
      56:     if(R>m) query(L,R,rson);
      57: }
      58:  
      59:  
      60: int main()
      61: {
      62:     int len,n;
      63:     while(scanf("%d%d%d",&len,&t,&n)>0)
      64:     {
      65:         build(1,len,1);
      66:         for(int i=0; i<n; i++)
      67:         {
      68:             char s[2];
      69:             int L,R;
      70:             scanf("%s%d%d",s,&L,&R);
      71:             if(L>R) swap(L,R);//个人觉得如果题目没有说明L<=R,那么一定要加这一句,以免出错
      72:             if(s[0]=='C'){
      73:                 int c;
      74:                 scanf("%d",&c);
      75:                 update(L,R,c,1,len,1);
      76:             }
      77:             else{
      78:                 memset(have,false,sizeof(have));
      79:                 sum=0;
      80:                 query(L,R,1,len,1);
      81:                 printf("%d
    ",sum);
      82:             }
      83:         }
      84:     }
      85:     return 0;
      86: }
  • 相关阅读:
    洛谷1012 拼数
    洛谷1012 拼数
    洛谷 1155 (NOIp2008)双栈排序——仔细分析不合法的条件
    bzoj 3566 [SHOI2014]概率充电器——树型
    bzoj 1415 [Noi2005]聪聪和可可——其实无环的图上概率
    洛谷 1291 [SHOI2002]百事世界杯之旅
    洛谷 1365 WJMZBMR打osu! / Easy
    洛谷 1297 [国家集训队]单选错位——期望
    洛谷 1099 ( bzoj 1999 ) [Noip2007]Core树网的核
    洛谷 2827 蚯蚓——相邻两个比较的分析
  • 原文地址:https://www.cnblogs.com/zyx1314/p/3628096.html
Copyright © 2011-2022 走看看