zoukankan      html  css  js  c++  java
  • Count Color 线段树

    Count Color
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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 <string.h>
     3 #include <stdio.h>
     4 #include <algorithm>
     5 using namespace std;
     6 #define ll long long
     7 typedef struct abcd
     8 {
     9     ll d,nu;
    10 } abcd;
    11 abcd a[888888];
    12 void build(int b,int c,int x)
    13 {
    14     if(b==c)
    15     {
    16         a[x].nu=1<<1;
    17         return ;
    18     }
    19     int m=(b+c)>>1;
    20     build(b,m,x<<1);
    21     build(m+1,c,x<<1|1);
    22     a[x].nu=1<<1;
    23 }
    24 void fun(int x)
    25 {
    26     a[x<<1].d=a[x<<1|1].d=a[x].d;
    27     a[x<<1].nu=1<<a[x].d;
    28     a[x<<1|1].nu=1<<a[x].d;
    29     a[x].d=0;
    30 }
    31 void update(int x,int y,int b,int c,int t,int z)
    32 {
    33     if(x<=b&&y>=c)
    34     {
    35         a[t].d=z;
    36         a[t].nu=1<<z;
    37         return ;
    38     }
    39     if(a[t].d)
    40         fun(t);
    41     int m=(b+c)>>1;
    42     if(x<=m)update(x,y,b,m,t<<1,z);
    43     if(y>m)update(x,y,m+1,c,t<<1|1,z);
    44     a[t].nu=a[t<<1].nu|a[t<<1|1].nu;
    45 }
    46 ll query(int x,int y,int b,int c,int t)
    47 {
    48     if(x<=b&&y>=c)
    49         return a[t].nu;
    50     if(a[t].d)
    51         fun(t);
    52     int m=(b+c)>>1;
    53     ll r=0;
    54     if(x<=m)r=query(x,y,b,m,t<<1);
    55     if(y>m)r|=query(x,y,m+1,c,t<<1|1);
    56     return r;
    57 }
    58 int unn(ll x)
    59 {
    60     ll ans=0;
    61     while(x)
    62     {
    63         ans+=x&1;
    64         x>>=1;
    65     }
    66     return ans;
    67 }
    68 int main()
    69 {
    70     //freopen("in.txt","r",stdin);
    71     int l,t,o,i,aa,ab,ac;
    72     char x;
    73     while(~scanf("%d%d%d",&l,&t,&o))
    74     {
    75         memset(a,0,sizeof(a));
    76         build(1,l,1);
    77         for(i=0; i<o; i++)
    78         {
    79             getchar();
    80             x=getchar();
    81             if(x=='C')
    82             {
    83                 scanf("%d%d%d",&aa,&ab,&ac);
    84                 if(aa>ab)swap(aa,ab);
    85                 update(aa,ab,1,l,1,ac);
    86             }
    87             else
    88             {
    89                 scanf("%d%d",&aa,&ab);
    90                 if(aa>ab)swap(aa,ab);
    91                 ll ans=query(aa,ab,1,l,1);
    92                 printf("%d
    ",unn(ans));
    93             }
    94         }
    95     }
    96 }
    View Code
  • 相关阅读:
    关于SQL的一些小知识
    关于VO中的Attribute的问题
    关于JDEV的连接问题
    object xml
    自己写的一个用于往文件中插入字符串及空格的bat
    修改 SQL SERVER 2008 編輯前200筆 資料表問題? 转载自:http://www.dotblogs.com.tw/easy1201/archive/2008/12/04/6179.aspx
    Create Advanced Web Applications With Object-Oriented Techniques
    需求第一
    fwrite() and UTF8 转载
    mysql 表字段与关键字相同的话
  • 原文地址:https://www.cnblogs.com/ERKE/p/3843334.html
Copyright © 2011-2022 走看看