poj_2777Count Color线段树_区间更新

来源:互联网 发布:淘宝充流量发验证码 编辑:程序博客网 时间:2024/06/07 00:33
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 36037 Accepted: 10882

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 4C 1 1 2P 1 2C 2 2 2P 1 2

Sample Output

2

1

做完本题推荐做POJ2528题,是这题的升级版

#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define mem0(a) memset(a,0,sizeof(a))#define maxn  100000+10int vis[35];//记录颜色struct node{    int l,r,cor;//保存线段树每个结点代表区间的l,r值}a[maxn<<2]; //cur作为根结点时起标记作用,作为子叶结点时表示颜色void build(int l,int r,int cur){    a[cur].l = l;    a[cur].r = r;    a[cur].cor = 1;//初始化颜色都为1    if( l < r ){        int mid = ( l + r )>>1;        build(l,mid,cur<<1);        build(mid+1,r,cur<<1|1);    }}void update(int l,int r,int v,int cur){//插入函数,着色    if( l  <= a[cur].l&&  r>=a[cur].r){//这里的l,r使要查询的范围        a[cur].cor = v;//a[cur].l和a[cur].r是线段树区间范围        return ;    }                               //自上向下传递颜色    if(a[cur]. l < a[cur].r ){        int mid =(a[cur].l + a[cur].r )>>1;        if(a[cur].cor > 0 ){//向左右子树覆着色            a[cur<<1|1].cor=a[cur<<1].cor =a[cur].cor;        }        a[cur].cor = -1;//清除本结点标记        if(r <= mid )//插入左子树            update(l,r,v,cur<<1);        else if(l > mid )            update(l,r,v,cur<<1|1);        else {            update(l,mid,v,cur<<1);            update(mid+1,r,v,cur<<1|1);        }    }}void query(int l,int r,int cur){//统计多少种颜色    if(a[cur].cor > 0 ){//从根节点算起,        vis[a[cur].cor]=1;        return ;    }    if(a[cur].l < a[cur].r){        int mid = (a[cur].l + a[cur].r )>>1;        if(r <= mid)            query(l,r,cur<<1);        else if(l > mid )            query(l,r,cur<<1|1);        else {            query(l,mid,cur<<1);            query(mid+1,r,cur<<1|1);        }    }}int main(){    int n,t,o;    while(scanf("%d%d%d",&n,&t,&o)!=EOF){        build(1,n,1);        int x,y,c;        char s[10];        while(o--){            scanf("%s",s);            if(s[0]=='C'){                scanf("%d%d%d",&x,&y,&c);                if(x > y ){swap(x,y);}                update(x,y,c,1);            }            else if(s[0]=='P'){                scanf("%d%d",&x,&y);                mem0(vis);                if(x>y) swap(x,y);                query(x,y,1);                int ans = 0 ;                for(int i = 1 ; i<= 30 ; i++){                    if(vis[i])                        ans++;                }                printf("%d\n",ans);            }        }    }   return 0;}



0 0
原创粉丝点击