• Count Color


    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
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 
      5 using namespace std;
      6 
      7 const int N=100010;
      8 
      9 #define L(rt) (rt<<1)
     10 #define R(rt) (rt<<1|1)
     11 
     12 struct Tree{
     13     int l,r;
     14     int col;    //  用一个32位的int型,每一位对应一种颜色,用位运算代替bool col[32]
     15     bool cover; //  表示这个区间都被涂上同一种颜色,线段树效率的体现,否则插入就是0(n)了。
     16 }tree[N<<2];
     17 
     18 void PushUp(int rt){    // 最后递归回来再更改父节点的颜色
     19     tree[rt].col=tree[L(rt)].col | tree[R(rt)].col;
     20 }
     21 
     22 void build(int L,int R,int rt){
     23     tree[rt].l=L;
     24     tree[rt].r=R;
     25     tree[rt].col=1; //  开始时都为涂有颜色1,看题要仔细,要注意状态。
     26     tree[rt].cover=1;
     27     if(tree[rt].l==tree[rt].r)
     28         return ;
     29     int mid=(L+R)>>1;
     30     build(L,mid,L(rt));
     31     build(mid+1,R,R(rt));
     32 }
     33 
     34 void PushDown(int rt){  //  延迟覆盖的操作
     35     tree[L(rt)].col=tree[rt].col;
     36     tree[L(rt)].cover=1;
     37     tree[R(rt)].col=tree[rt].col;
     38     tree[R(rt)].cover=1;
     39     tree[rt].cover=0;
     40 }
     41 
     42 void update(int val,int L,int R,int rt){
     43     if(L<=tree[rt].l && R>=tree[rt].r){
     44         tree[rt].col=val;
     45         tree[rt].cover=1;
     46         return ;
     47     }
     48     if(tree[rt].col==val)  //剪枝
     49         return ;
     50     if(tree[rt].cover)
     51         PushDown(rt);
     52     int mid=(tree[rt].l+tree[rt].r)>>1;
     53     if(R<=mid)
     54         update(val,L,R,L(rt));
     55     else if(L>=mid+1)
     56         update(val,L,R,R(rt));
     57     else{
     58         update(val,L,mid,L(rt));
     59         update(val,mid+1,R,R(rt));
     60     }
     61     PushUp(rt);      // 最后递归回来再更改父节点的颜色
     62 }
     63 
     64 int sum;
     65 
     66 void query(int L,int R,int rt){
     67     if(L<=tree[rt].l && R>=tree[rt].r){
     68         sum |= tree[rt].col;
     69         return ;
     70     }
     71     if(tree[rt].cover){     //  这个区间全部为1种颜色,就没有继续分割区间的必要了
     72         sum |= tree[rt].col;     //  颜色种类相加的位运算代码
     73         return;
     74     }
     75     int mid=(tree[rt].l+tree[rt].r)>>1;
     76     if(R<=mid)
     77         query(L,R,L(rt));
     78     else if(L>=mid+1)
     79         query(L,R,R(rt));
     80     else{
     81         query(L,mid,L(rt));
     82         query(mid+1,R,R(rt));
     83     }
     84 }
     85 
     86 int solve(){
     87     int ans=0;
     88     while(sum){
     89         if(sum&1)
     90             ans++;
     91         sum>>=1;
     92     }
     93     return ans;
     94 }
     95 
     96 void swap(int &a,int &b){
     97     int tmp=a;a=b;b=tmp;
     98 }
     99 
    100 int main(){
    101 
    102     //freopen("input.txt","r",stdin);
    103 
    104     int n,t,m;
    105     while(~scanf("%d%d%d",&n,&t,&m)){
    106         build(1,n,1);
    107         char op[3];
    108         int a,b,c;
    109         while(m--){
    110             scanf("%s",op);
    111             if(op[0]=='C'){
    112                 scanf("%d%d%d",&a,&b,&c);
    113                 if(a>b)
    114                     swap(a,b);
    115                 update(1<<(c-1),a,b,1); // int型的右起第c位变为1,即2的c-1次方。
    116             }else{
    117                 scanf("%d%d",&a,&b);
    118                 if(a>b)
    119                     swap(a,b);
    120                 sum=0;
    121                 query(a,b,1);
    122                 printf("%d
    ",solve());
    123             }
    124         }
    125     }
    126     return 0;
    127 }
    View Code

    A Corrupt Mayor's Performance Art

    Problem Description
    Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money.

    Because a lot of people praised mayor X's painting(of course, X was a mayor), mayor X believed more and more that he was a very talented painter. Soon mayor X was not satisfied with only making money. He wanted to be a famous painter. So he joined the local painting associates. Other painters had to elect him as the chairman of the associates. Then his painting sold at better price.

    The local middle school from which mayor X graduated, wanted to beat mayor X's horse fart(In Chinese English, beating one's horse fart means flattering one hard). They built a wall, and invited mayor X to paint on it. Mayor X was very happy. But he really had no idea about what to paint because he could only paint very abstract paintings which nobody really understand. Mayor X's secretary suggested that he could make this thing not only a painting, but also a performance art work.

    This was the secretary's idea:

    The wall was divided into N segments and the width of each segment was one cun(cun is a Chinese length unit). All segments were numbered from 1 to N, from left to right. There were 30 kinds of colors mayor X could use to paint the wall. They named those colors as color 1, color 2 .... color 30. The wall's original color was color 2. Every time mayor X would paint some consecutive segments with a certain kind of color, and he did this for many times. Trying to make his performance art fancy, mayor X declared that at any moment, if someone asked how many kind of colors were there on any consecutive segments, he could give the number immediately without counting.

    But mayor X didn't know how to give the right answer. Your friend, Mr. W was an secret officer of anti-corruption bureau, he helped mayor X on this problem and gained his trust. Do you know how Mr. Q did this?
     
    Input
    There are several test cases.

    For each test case:

    The first line contains two integers, N and M ,meaning that the wall is divided into N segments and there are M operations(0 < N <= 1,000,000; 0<M<=100,000)

    Then M lines follow, each representing an operation. There are two kinds of operations, as described below:

    1) P a b c
    a, b and c are integers. This operation means that mayor X painted all segments from segment a to segment b with color c ( 0 < a<=b <= N, 0 < c <= 30).

    2) Q a b
    a and b are integers. This is a query operation. It means that someone asked that how many kinds of colors were there from segment a to segment b ( 0 < a<=b <= N).

    Please note that the operations are given in time sequence.

    The input ends with M = 0 and N = 0.
     
    Output
    For each query operation, print all kinds of color on the queried segments. For color 1, print 1, for color 2, print 2 ... etc. And this color sequence must be in ascending order.
     
    Sample Input
    5 10
    P 1 2 3
    P 2 3 4
    Q 2 3
    Q 1 3
    P 3 5 4
    P 1 2 7
    Q 1 3
    Q 3 4
    P 5 5 8
    Q 1 5
    0 0
     
    Sample Output
    4
    3 4
    4 7
    4
    4 7 8
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include"algorithm"
      5 
      6 using namespace std;
      7 
      8 const int N=1000010;
      9 
     10 #define L(rt) (rt<<1)
     11 #define R(rt) (rt<<1|1)
     12 int order[31];
     13 
     14 struct Tree{
     15     int l,r;
     16     int col;    //  用一个32位的int型,每一位对应一种颜色,用位运算代替bool col[32]
     17     bool cover; //  表示这个区间都被涂上同一种颜色,线段树效率的体现,否则插入就是0(n)了。
     18 }tree[3*N];
     19 
     20 void PushUp(int rt){    // 最后递归回来再更改父节点的颜色
     21     tree[rt].col=tree[L(rt)].col | tree[R(rt)].col;
     22 }
     23 
     24 void build(int L,int R,int rt){
     25     tree[rt].l=L;
     26     tree[rt].r=R;
     27     tree[rt].col=2; //  开始时都为涂有颜色1,看题要仔细,要注意状态。
     28     tree[rt].cover=1;
     29     if(tree[rt].l==tree[rt].r)
     30         return ;
     31     int mid=(L+R)>>1;
     32     build(L,mid,L(rt));
     33     build(mid+1,R,R(rt));
     34 }
     35 
     36 void PushDown(int rt){  //  延迟覆盖的操作
     37     tree[L(rt)].col=tree[rt].col;
     38     tree[L(rt)].cover=1;
     39     tree[R(rt)].col=tree[rt].col;
     40     tree[R(rt)].cover=1;
     41     tree[rt].cover=0;
     42 }
     43 
     44 void update(int val,int L,int R,int rt){
     45     if(L<=tree[rt].l && R>=tree[rt].r){
     46         tree[rt].col=val;
     47         tree[rt].cover=1;
     48         return ;
     49     }
     50     if(tree[rt].col==val)  //剪枝
     51         return ;
     52     if(tree[rt].cover)
     53         PushDown(rt);
     54     int mid=(tree[rt].l+tree[rt].r)>>1;
     55     if(R<=mid)
     56         update(val,L,R,L(rt));
     57     else if(L>=mid+1)
     58         update(val,L,R,R(rt));
     59     else{
     60         update(val,L,mid,L(rt));
     61         update(val,mid+1,R,R(rt));
     62     }
     63     PushUp(rt);      // 最后递归回来再更改父节点的颜色
     64 }
     65 
     66 int sum;
     67 
     68 void query(int L,int R,int rt){
     69     if(L<=tree[rt].l && R>=tree[rt].r){
     70         sum |= tree[rt].col;
     71         return ;
     72     }
     73     if(tree[rt].cover){     //  这个区间全部为1种颜色,就没有继续分割区间的必要了
     74         sum |= tree[rt].col;     //  颜色种类相加的位运算代码
     75         return;
     76     }
     77     int mid=(tree[rt].l+tree[rt].r)>>1;
     78     if(R<=mid)
     79         query(L,R,L(rt));
     80     else if(L>=mid+1)
     81         query(L,R,R(rt));
     82     else{
     83         query(L,mid,L(rt));
     84         query(mid+1,R,R(rt));
     85     }
     86 }
     87 
     88 void solve(){
     89     int ans=0,i,kk=0;
     90   //  while(sum){
     91   //      if(sum&1)
     92    //         ans++;
     93    //     sum>>=1;
     94    // }
     95    memset(order,0,sizeof(order));
     96    for(i=1;i<=30;i++)
     97    {
     98         if(sum&(1<<(i-1)))
     99         order[kk++]=i;
    100    }
    101    sort(order,order+kk);
    102    for(i=0;i<kk;i++)
    103    {
    104         if(order[i])
    105         {
    106         printf("%d ",order[i]);
    107         }
    108    }
    109    printf("
    ");
    110     //return ans;
    111 }
    112 
    113 //void swap(int &a,int &b){
    114 //    int tmp=a;a=b;b=tmp;
    115 //}
    116 
    117 int main(){
    118 
    119     //freopen("input.txt","r",stdin);
    120 
    121     int n,t,m;
    122     while(~scanf("%d%d",&n,&m)){
    123         if(n+m==0)
    124          break;
    125         build(1,n,1);
    126         char op[3];
    127         int a,b,c;
    128         while(m--){
    129             scanf("%s",op);
    130             if(op[0]=='P'){
    131                 scanf("%d%d%d",&a,&b,&c);
    132                // if(a>b)
    133                  //   swap(a,b);
    134                 update(1<<(c-1),a,b,1); // int型的右起第c位变为1,即2的c-1次方。
    135             }else{
    136                 scanf("%d%d",&a,&b);
    137                 sum=0;
    138                 query(a,b,1);
    139                 //printf("%d
    ",solve());
    140                 solve();
    141             }
    142         }
    143     }
    144     return 0;
    145 }
    View Code
  • 相关阅读:
    java中值传递和引用传递
    java中的XML
    java I/O流
    RandomAccessFile类
    java中File类
    Java 理论与实践: 正确使用 Volatile 变量
    eclipse 总弹出 secure storage的解决办法
    安卓表格布局android:collapseColumns,android:shrinkColumns和stretchColumn
    Android关联源码support-v4的问题解决
    关于spring framework最新发布压缩包的下载问题 【非常非常新手帖】
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/3983336.html
Copyright © 2020-2023  润新知