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;
    }
    
  • 相关阅读:
    在模板生成页面的时候,页面里的标签可能会生成多个id,这时候使用id选择器,往往只能取到第一个id的元素。
    后台返回model里的时间格式,用@JsonFormat是没用的,它只有在返回JSON数据的时候生效,我脑抽了
    thymeleaf关于 Error resolving template “index”, template might not exist or might not be accessible by any of the configured Template Resolvers
    thymeleaf 配合 Spring Security 权限判断时,sec:authentication无法取到值(null)
    MySQL 常用30种SQL查询语句优化方法
    Linux常用命令
    APP微信登录---第三方登录
    关于文件的工具类例子
    Java时间戳与日期格式字符串的互转
    Java字符串与文件的互转操作
  • 原文地址:https://www.cnblogs.com/herumw/p/9464830.html
Copyright © 2011-2022 走看看