zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    服务器消息机制实现记录
    转载SQL经典代码按某一字段分组取最大(小)值所在行的数据
    记录js获取当前URL
    (原创)xilinx IP建立向导创建的目录和文件都是做什么的?由错误ERROR:HDLCompiler:Instantiating <xx> from unknown module <xx>引发的思考
    [转]NTFS3G的安装和配置
    (原创)Notepad++怎么实现双视图/双窗口?
    (原创)Quartus硬件工程路径改变,nios工程该怎么办?
    (原)verilog中的reg类型变量,一定会综合出触发器吗?
    (Windows)使用纯净版本的系统碟安装系统后没有网卡驱动怎么办?
    [转]NIOS_II的Boot过程分析
  • 原文地址:https://www.cnblogs.com/herumw/p/9464830.html
Copyright © 2011-2022 走看看