第五周周四总结

这几天还是偶尔看了看知识点。。。今天晚上一直想用java写这个线段树的,结果写了一个多小时一直错误,还是对java不熟练。。。我还是再去查查静态变量的知识吧。。。

Count Color

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 62   Accepted Submission(s) : 22
Problem 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

就是给你区间涂色,查询区间颜色数目,一遍过。。。在java弄了一个多小时都是不知道哪里错误一直没法运行,c一遍就过了。。

#include<stdio.h>
#include<string.h>
struct f
{
int l,r;
int num;
}tree[400005];
int bj[35];
void build(int l,int r,int k)
{
    tree[k].l=l;
    tree[k].r=r;
    tree[k].num=1;
    int mid=(l+r)/2;
    if (l==r)
        return ;
    build(l,mid,k<<1);
    build(mid+1,r,k<<1|1);
}
void update(int l,int r,int c,int k)
{
    if(tree[k].l==l&&tree[k].r==r)
        {
        tree[k].num=c;
        return;
        }
    if(tree[k].num==c)
        return;
    if (tree[k].num!=-1)
        {
        tree[k<<1].num=tree[k].num;
        tree[k<<1|1].num=tree[k].num;
        tree[k].num=-1;
    }
    int mid=(tree[k].l+tree[k].r)/2;
    if (l>mid)
        update(l,r,c,k<<1|1);
    else if (r<=mid)
        update(l,r,c,k<<1);
    else
        {
        update(l,mid,c,k<<1);
        update(mid+1,r,c,k<<1|1);
        }
}
void find(int l,int r,int k)
{
    if (tree[k].num!=-1)
    {
        bj[tree[k].num]=1;
        return;
        }
    int mid=(tree[k].l+tree[k].r)/2;
    if(r<=mid)
    find(l,r,2*k);
    else if (l>mid)
        find(l,r,k<<1|1);
    else
    {
        find(l,mid,k<<1);
        find(mid+1,r,k<<1|1);
    }
}
int main()
{
    int l,t,o,s;
    int ll,rr,k;
    char zf;
    int i;
    while (~scanf("%d%d%d",&l,&t,&o))
    {
    build(1,l,1);
    while (o--)
        {
        getchar();
        scanf("%c",&zf);
        if (zf=='C')
            {
            scanf("%d%d%d",&ll,&rr,&k);
            if (ll>rr)
                update(rr,ll,k,1);
            else
                update(ll,rr,k,1);
            }
        else
            {
            scanf("%d%d",&ll,&rr);
            memset(bj,0,sizeof(bj));
            if(ll>rr)
                find(rr,ll,1);
            else
                find(ll,rr,1);
            s=0;
            for(i=1;i<=t;i++)
                if(bj[i]==1)
                    s++;
            printf ("%d\n",s);
            }
        }
    }
return 0;
}

贴上java的吧,等有空再来改

package zy;
import java.util.Scanner;
public class poj2777_Count_Color 
	{
	public class f
		{
		int l,r,num;
		public f()
			{
			}
		}
	
	public static void build(int l,int r,int k)
		{
		tree[k].l=l;
		tree[k].r=r;
		tree[k].num=1;
		int mid=(l+r)/2;
		if(l==r)
			return;
		build(l,mid,k<<1);
		build(mid+1,r,k<<1|1);
		}
	
	public static void update(int l,int r,int n,int k)
		{
		System.out.println("haha");
		if(tree[k].l==r&&tree[k].r==r)
			{
			tree[k].num=n;
			return;
			}
		System.out.println("haha");
		if(tree[k].num==n)
			return;
		if(tree[k].num!=-1)
			{
			tree[k<<1].num=tree[k].num;
			tree[k<<1|1].num=tree[k].num;
			tree[k].num=-1;
			}
		int mid=(tree[k].l+tree[k].r)/2;
		if(l>mid)
			update(l,r,n,k<<1|1);
		else if(r<=mid)
				update(l,r,n,k<<1);
		else
			{
			update(l,mid,n,k<<1);
			update(mid|1,r,n,k<<1|1);
			}
		}
	
	public static void find(int l,int r,int k)
		{
		if(tree[k].num==-1)
			{
			bj[tree[k].num]=1;
			return;
			}
		int mid=(tree[k].l+tree[k].r)/2;
		if(r<=mid)
			find(l,r,k<<1);
		else if(l>mid)
			 	find(l,r,k<<1|1);
		else
			{
			find(l,mid,k<<1);
			find(mid|1,r,k<<1|1);
			}
		}
	
	public static f[] tree=new f[400005];
	public static int[] bj=new int[31];
	
	public static void main(String[] args)
		{
		Scanner cin=new Scanner(System.in);
		int l,t,o;
		int ll,rr,k;
		int i;
		int s;
		String zf;
		char ch;
		while(cin.hasNext())
			{
			l=cin.nextInt();
			t=cin.nextInt();
			o=cin.nextInt();
			build(1,l,1);
			while(o!=0)
				{
				o--;
				zf=cin.next();
				ch=zf.charAt(0);
				if(ch=='C')
					{
					ll=cin.nextInt();
					rr=cin.nextInt();
					k=cin.nextInt();
					update(ll,rr,k,1);
					}
				else
					{
					ll=cin.nextInt();
					rr=cin.nextInt();
					for(i=0;i<31;i++)
						bj[i]=0;
					find(ll,rr,1);
					s=0;
					for(i=0;i<t;i++)
						if(bj[i]==1)
							s++;
					System.out.println(s);
					}
				}
			}
		}	
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值