• poj2777 Count Color


    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 37779   Accepted: 11355

    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
    这道题和just a hook差不多,在那道题的基础上加深了一些,每一次询问都要把数组a[]清空,然后往数组力添加代表不同颜色的数字,如果数组里已经有了该颜色就不用添加了。还学到了"<<"运算符,当它和"+"一起使用的时候,要加括号,因为其优先级小于"+",用位运算的速度确实比直接乘和除快啊。1<<i代表2的i次,i<<1代表i*2.
    #include<stdio.h>
    #include<string.h>
    char s[10];
    int a[100],t;
    struct node
    {
    	int l,r,num;
    }b[4*200006];
    
    
    void build(int l,int r,int i)
    {
    	int mid;
    	b[i].l=l;b[i].r=r;b[i].num=1;
    	if(l==r)return;
    	mid=(l+r)/2;
    	build(l,mid,i<<1);
    	build(mid+1,r,(i<<1)+1);
    }
    
    
    void update(int l,int r,int num,int i)
    {
    	int mid;
    	if(b[i].num==num)return;
    	if(b[i].l==l && b[i].r==r){
    		b[i].num=num;return;
    	}
    	if(b[i].num!=-1){
    		b[i<<1].num=b[(i<<1)+1].num=b[i].num;b[i].num=-1;
    	}
    	mid=(b[i].l+b[i].r)/2;
    	if(r<=mid)update(l,r,num,i<<1);
    	else if(l>mid) update(l,r,num,(i<<1)+1);
    	else if(r>mid && l<=mid) {
    		update(l,mid,num,i<<1);update(mid+1,r,num,(i<<1)+1);
    	}
    }
    
    
    void question(int l,int r,int i)
    {
    	int mid,j,flag;
    	if(b[i].num!=-1){
    		flag=1;
    		for(j=1;j<=t;j++){
    			if(a[j]==b[i].num){
    				flag=0;break;
    			}
    		}
    		if(flag==1){
    			a[++t]=b[i].num;
    		}
    		return;
    	}
    	mid=(b[i].l+b[i].r)/2;
    	if(r<=mid) question(l,r,i<<1);
    	else if(l>mid) question(l,r,(i<<1)+1);
    	else {
    		question(l,mid,i<<1);
    		question(mid+1,r,(i<<1)+1);
    	}
    }
    
    
    int main()
    {
    	int n,m,i,j,c,d,e,T,temp,num,h;
    	while(scanf("%d%d%d",&n,&m,&T)!=EOF)
    	{
    		build(1,n,1);
    		for(i=1;i<=T;i++){
    			scanf("%s",s);
    			if(s[0]=='C'){
    				scanf("%d%d%d",&c,&d,&e);
    				if(c>d){
    					temp=c;c=d;d=temp;
    				}
    				update(c,d,e,1);
    			}
    			else if(s[0]=='P'){
    				scanf("%d%d",&c,&d);
    				if(c>d){
    					temp=c;c=d;d=temp;
    				}
    				memset(a,0,sizeof(a));
    				t=0;
    				question(c,d,1);
    				printf("%d
    ",t);
    			}
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    系统重启
    Linux驱动程序开发
    Linux 下实现控制屏幕显示信息和光标的状态
    Linux C 语言 获取系统时间信息
    linux 获取系统屏幕分辨率
    Linux下得到显示屏参数的方法
    Linux如何关闭防火墙和查看防火墙的具体情况
    查看Linux下网卡状态或 是否连接(转)
    ArcGIS Engine中正确释放打开资源
    在ArcEngine下实现图层属性过滤的两种方法
  • 原文地址:https://www.cnblogs.com/herumw/p/9464830.html
Copyright © 2020-2023  润新知