Count Color

http://acm.hust.edu.cn:8080/judge/contest/viewProblem.action?pid=45730

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
第一份代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
#define MAXN 100010
bool flag[MAXN];
struct node 
{
	int l,r,color;
}t[MAXN*4];

void make(int st,int ed ,int num)
{
	t[num].l=st;
	t[num].r=ed;
	t[num].color=1;
	if(st==ed)return ;
	int mid=(st+ed)/2;
	make(st,mid,num*2);
	make(mid+1,ed,num*2+1);
}

void updata(int st,int ed,int num,int color)
{
	if(t[num].l==st&&t[num].r==ed)
	{
		t[num].color=color;
		return ;
	}
	if(t[num].color>0)
	{
		t[num*2].color=t[num].color;
		t[num*2+1].color=t[num].color;
		t[num].color=0;
	}
	int mid=(t[num].l+t[num].r)/2;
	if(st>mid)
		updata(st,ed,num*2+1,color);
	else if(ed<=mid)
		updata(st,ed,num*2,color);
	else 
	{
		updata(st,mid,num*2,color);
		updata(mid+1,ed,num*2+1,color);
	}
}

void   query(int st,int ed,int num)
{
	if(t[num].color>0)
	{
		flag[t[num].color]=1;
		return ;
	}
	int mid=(t[num].l+t[num].r)/2;
	if(st>mid)query(st,ed,num*2+1);
	else if(ed<=mid)query(st,ed,num*2);
	else 
	{
		query(st,mid,num*2);
		query(mid+1,ed,num*2+1);
	}
}

int main()
{
	int T,L,O;
	char str[3];
	int ans;
	scanf("%d%d%d",&L,&T,&O);
	make(1,L,1);
	while(O--)
	{
		scanf("%s",str);
		if(str[0]=='C')
		{
			int x,y,clo;
			scanf("%d%d%d",&x,&y,&clo);
			updata(x,y,1,clo);
		}
		if(str[0]=='P')
		{
			int x;
			int y;
			memset(flag,0,sizeof(flag));
			scanf("%d%d",&x,&y);
			ans=0;
			query(x,y,1);
			for(int i=1;i<=30;i++)
			{
				if(flag[i]==1)
					ans++;
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}
第二份代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
struct  
{
	int l,r,color;
}g[100005*4];
int a[35];
void creat(int l,int r,int k)
{
	g[k].l=l;
	g[k].r=r;
	g[k].color=1;
	int mid;
	if(l<r)
	{
		mid=(l+r)/2;
		creat(l,mid,k*2);
		creat(mid+1,r,k*2+1);
	}

}

void update(int l,int r,int add,int k)
{
	if(g[k].l>=l&&g[k].r<=r)
	{
		g[k].color=add;
		return ;
	}
	int mid;
	if(g[k].l<g[k].r)
	{
		if(g[k].color>0)
  

    

      

        
//延时标记
{g[k*2].color=g[k*2+1].color=g[k].color;}g[k].color=-1;mid=(g[k].l+g[k].r)/2;if(r<=mid)update(l,r,add,k*2);else if(l>mid)update(l,r,add,k*2+1);else{update(l,mid,add,k*2);update(mid+1,r,add,k*2+1);}}}void quire(int l,int r,int k){if(g[k].color>0)//不用将整个叶子节点全部求出;{a[g[k].color]=1;return;}int mid;if(g[k].l<g[k].r){ mid=(g[k].l+g[k].r)/2; if(r<=mid) quire(l,r,k*2); else if(l>mid) quire(l,r,k*2+1); else{ quire(l,mid,k*2); quire(mid+1,r,k*2+1);} }}int main(){int m,n,i,j,k,l,r,add;char c;while(scanf("%d%d%d",&m,&n,&k)!=EOF){creat(1,m,1);while(k--){scanf(" %c",&c);if(c=='C'){ scanf("%d%d%d",&l,&r,&add); if(l>r){l^=r^=l^=r;}update(l,r,add,1);}else{scanf("%d%d",&l,&r);memset(a,0,sizeof(a));if(l>r){l^=r^=l^=r;}quire(l,r,1);int d=0;for(i=1;i<=n;i++){if(a[i]>0)d++;}printf("%d\n",d);}}}return 0;}
第三份代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 100005

struct node
{
	int l,r,color;
	bool flag;
}tree[3*maxn];
int n,m,tt;
 
void BuildTree(int p,int l,int r)
{
	tree[p].l=l; tree[p].r=r;
	tree[p].color=1; tree[p].flag=false;
	if(l==r) return;
	if(l<r)
	{
		int mid=(l+r)/2;
		BuildTree(2*p,l,mid);
		BuildTree(2*p+1,mid+1,r);
	}
//	tree[p].color=tree[2*p].color|tree[2*p+1].color;
} 

void change(int p,int l,int r,int x)
{
	if(tree[p].l>=l&&tree[p].r<=r)
	{
		tree[p].color=x;
		tree[p].flag=true;
		return ;
	}
	
	if(tree[p].l<tree[p].r)
	{	
		if(tree[p].flag)
		{
			tree[2*p].color=tree[p].color;
			tree[2*p].flag=true;
			tree[2*p+1].color=tree[p].color;
			tree[2*p+1].flag=true;
			tree[p].flag=false;
		}
		int mid=(tree[p].l+tree[p].r)/2;
		if(r<=mid)
			change(2*p,l,r,x);
		else if(l>mid)
			change(2*p+1,l,r,x);
		else
		{
			change(2*p,l,mid,x);
			change(2*p+1,mid+1,r,x);
		}
		tree[p].color=tree[2*p].color|tree[2*p+1].color;
	}
}

int que(int p,int l,int r)
{
	if(tree[p].flag)
		return	tree[p].color;
	if(tree[p].l==l&&tree[p].r==r)
		return tree[p].color;
	
	if(tree[p].l<tree[p].r)
	{
		if(tree[p].flag)
		{
			tree[2*p].flag=true;
			tree[2*p].color=tree[p].color;
			tree[2*p+1].flag=true;
			tree[2*p+1].color=tree[p].color;
			tree[p].flag=false;
		}
		int mid=(tree[p].l+tree[p].r)/2;
		if(r<=mid)
			return que(2*p,l,r);
		else if(l>mid)
			return que(2*p+1,l,r);
		else
			return que(2*p,l,mid)|que(2*p+1,mid+1,r);
	}
}
 
int make_ans(int x)
{
	int ans=0;
	while(x)
	{
		if(x%2)	ans++;
		x/=2;
	}
	return ans;
}
 
int main()
{
	int i,j,x,y,d;
	char c;
	scanf("%d%d%d",&n,&m,&tt);
	BuildTree(1,1,n);
	getchar();
	while(tt--)
	{
		scanf("%c",&c);
		if(c=='C')
		{
			scanf("%d%d%d",&x,&y,&d);
			if(x>y)
				swap(x,y);
			change(1,x,y,1<<(d-1));
		}
		else if(c=='P')
		{
			scanf("%d%d",&x,&y);
			if(x>y)
				swap(x,y);
			int ans=que(1,x,y);
			printf("%d\n",make_ans(ans));
		}
		getchar();
	}	
	return 0;
}








以下是修改后的代码,可以在PPT中运行,实现相同功能的宏代码: ``` Sub ApplyRandomFont() Dim colorList As Variant colorList = Array(RGB(255, 0, 0), RGB(255, 165, 0), RGB(255, 255, 0), RGB(0, 255, 0), RGB(139, 69, 19), RGB(0, 255, 255), RGB(0, 0, 255), RGB(128, 0, 128), RGB(255, 192, 203), RGB(0, 0, 0)) Dim fontList As Variant fontList = Array("星座文字A5", "星座文字A12", "几何标准体A3", "花型文字A1", "花型文字A2", "花型文字A3", "花型文字A4", "欧拉文字A4", "几何标准体B3", "华为文字A1", "星座文字A1", "星座文字B3", "几何方滑体A32") Dim slide As Slide For Each slide In ActivePresentation.Slides Dim shape As Shape For Each shape In slide.Shapes If shape.HasTextFrame Then Dim paragraph As TextRange For Each paragraph In shape.TextFrame.TextRange.Paragraphs Dim run As TextRange For Each run In paragraph.Runs Dim color As Long color = run.Font.Color.RGB Dim i As Integer For i = 0 To UBound(colorList) If color = colorList(i) Then Dim fontIndex As Integer fontIndex = Int(Rnd * UBound(fontList) + 1) run.Font.Name = fontList(fontIndex) fontList(fontIndex) = "" Exit For End If Next i Next run Next paragraph End If Next shape Next slide End Sub ``` 这段代码与之前的代码基本相同,只是将操作对象从Word文档改为PPT文档,并稍作调整以适应PPT文档结构。具体来说,它会遍历PPT的所有文本框,找到指定颜色的字符并随机应用字体。具体实现方法与之前的代码相同,不再赘述。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值