Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

借鉴HH大神的代码风格,不断TLE,求解

Posted by yobobobo at 2012-08-24 10:06:30 on Problem 2777 and last updated at 2012-08-24 10:06:41
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define lchild l,m,rt<<1
#define rchild m+1,r,rt<<1|1
const int maxn=111111;
int cnt,col[maxn<<2];
bool hash[maxn];
void build(int l,int r,int rt);
void pusn_down(int rt);
void update(int L,int R,int c,int l,int r,int rt);
void query(int L,int R,int l,int r,int rt);
int main()
{
	char op[10];
	int a,b,c,n,m,T;
	scanf("%d%d%d",&n,&T,&m);
	memset(col,-1,sizeof(col));
	build(1,n,1);
	for(int i=0;i<m;i++)
	{
		scanf("%s%d%d",op,&a,&b);
		if(a>b) swap(a,b);
		if(op[0]=='C')
		{
			scanf("%d",&c);
			update(a,b,c,1,n,1);
		}
		else
		{
			memset(hash,false,sizeof(hash));
			cnt=0;
			query(a,b,1,n,1);
			printf("%d\n",cnt);
		}
	}
	return 0;
}
void push_down(int rt)
{
	if(col[rt]!=-1)
	{
		col[rt<<1]=col[rt<<1|1]=col[rt];
		col[rt]=-1;
	}
}
void build(int l,int r,int rt)
{
	if(l==r)
	{
		col[rt]=1;
		return ;
	}
	int m=(l+r)>>1;
	build(lchild);
	build(rchild);
}
void update(int L,int R,int c,int l,int r,int rt)
{
	if(L<=l&&r<=R)
	{
		col[rt]=c;
		return ;
	}
	push_down(rt);
	int m=(l+r)>>1;
	if(L<=m) update(L,R,c,lchild);
	if(R>m) update(L,R,c,rchild);
}
void query(int L,int R,int l,int r,int rt)
{
	if(col[rt]!=-1)
	{
		if(!hash[col[rt]]) cnt++;
		hash[col[rt]]=true;
		return ;
	}
	int m=(l+r)>>1;
	if(L<=m) query(L,R,lchild);
	if(R>m)  query(L,R,rchild);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator