• POJ 2777 Count Color


    题目链接http://poj.org/problem?id=2777

    解题思路:比较巧妙,状态压缩----最多三十种颜色,每一位表示每个颜色状态,那么使用逻辑或运算即可避免颜色重复计算的问题,统计颜色的时候判断1的位数即可。

    延迟标记的传递时候需要更改左右孩子的值以及其标记!

    代码:

     1 const int maxn = 1e5 + 5;
     2 
     3 int tree[maxn * 4], tag[maxn * 4];
     4 int hc[32];
     5 int n, t, o;
     6 
     7 void build(int l, int r, int k){
     8     tree[k] = hc[1]; tag[k] = 1;
     9     if(l == r) return; 
    10     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    11     build(l, mid, lc);
    12     build(mid + 1, r, rc);
    13 }
    14 void update(int ul, int ur, int x, int l, int r, int k){
    15     if(ul <= l && ur >= r){
    16         tree[k] = hc[x];
    17         tag[k] = x;
    18         return;
    19     }
    20     if(ul > r || ur < l) return;
    21     
    22     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    23     if(tag[k] != 0){
    24         tree[k] = hc[tag[k]];
    25         tag[lc] = tag[k]; tree[lc] = hc[tag[k]];
    26         tag[rc] = tag[k]; tree[rc] = hc[tag[k]];
    27         tag[k] = 0;
    28     }
    29     
    30     update(ul, ur, x, l, mid, lc);
    31     update(ul, ur, x, mid + 1, r, rc);
    32     tree[k] = tree[lc] | tree[rc];
    33 }
    34 int query(int ql, int qr, int l, int r, int k){
    35     if(ql <= l && qr >= r) {
    36         if(tag[k]) tree[k] = hc[tag[k]];
    37         return tree[k];
    38     }
    39     if(ql > r || qr < l) return 0;
    40     
    41     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    42     if(tag[k] != 0){
    43         tree[k] = hc[tag[k]];
    44         tag[lc] = tag[k]; tree[lc] = hc[tag[k]];
    45         tag[rc] = tag[k]; tree[rc] = hc[tag[k]];
    46         tag[k] = 0;
    47     }
    48     int q1 = query(ql, qr, l, mid, lc);
    49     int q2 = query(ql, qr, mid + 1, r, rc);
    50     return q1 | q2;
    51 }
    52 int getAmo(int x){
    53     int ans = 0;
    54     while(x > 0){
    55         if(x & 1) ans++;
    56         x >>= 1;
    57     }
    58     return ans;
    59 }
    60 
    61 int main(){
    62     for(int i = 1, x = 1; i <= 30; i++){
    63         hc[i] = x;
    64         x <<= 1;    
    65     }
    66     scanf("%d %d %d", &n, &t, &o);
    67     build(1, n, 1);
    68     for(int i = 0; i < o; i++){
    69         char ch;
    70         scanf(" %c", &ch);
    71         if(ch == 'C'){
    72             int u, v, c;
    73             scanf("%d %d %d", &u, &v, &c);
    74             if(u > v) swap(u, v);
    75             update(u, v, c, 1, n, 1);
    76         }
    77         else{
    78             int u, v;
    79             scanf("%d %d", &u, &v);
    80             if(u > v) swap(u, v);
    81             int tmp = query(u, v, 1, n, 1);
    82             tmp = getAmo(tmp);
    83             printf("%d
    ", tmp);
    84         }
    85     }
    86 }

    题目:

    Count Color
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 46657   Accepted: 14131

    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

     

  • 相关阅读:
    cf Inverse the Problem (最小生成树+DFS)
    cf Make It Nondeterministic (简单贪心)
    cf Learn from Life (简单贪心)
    hdu 5057 Argestes and Sequence (数状数组+离线处理)
    hdu 5056 Boring count (类似单调队列的做法。。)
    hdu 5055 Bob and math problem (很简单贪心)
    三样东西能让女人幸福一生
    第01课 OpenGL窗口(4)
    爱情要不要吃回头草?(林忆)
    第01课 OpenGL窗口(3)
  • 原文地址:https://www.cnblogs.com/bolderic/p/7301568.html
Copyright © 2020-2023  润新知