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

为什么,我用最原始的思路做错了呢。记录原始的,更新区域的,把它们加起来,错了。

Posted by 987690183 at 2013-04-20 20:46:59 on Problem 3468
#include<stdio.h>
#define HH 1
struct st
{
	long long l;
	long long r;
	long long color;
	long long sum;
	long long num;
}f[100002*4];
long long date[100002],sum1;
void build(long long l,long long r,long long n)
{
	long long mid=(l+r)/2;
	f[n].l=l;
	f[n].r=r;
	f[n].color=0;
	f[n].num=0;
	if(l==r)
	{
		f[n].sum=date[l];
	}
	else 
	{
		build(l,mid,n*2);
		build(mid+1,r,n*2+1);
		f[n].sum=f[n*2].sum+f[n*2+1].sum;
	}
}
void update(long long l,long long r,long long num,long long n)
{
	long long mid=(f[n].l+f[n].r)/2;
	if(f[n].l==l&&f[n].r==r)
	{
		f[n].color=0;
		f[n].num=num;
		return ;
	}
	if(f[n].color==0)
	{
		f[n].color=HH;
		f[n*2].num=f[n].num;
		f[n*2+1].num=f[n].num;
		f[n*2].color=0;
		f[n*2+1].color=0;
	}
	if(mid>=r)
		update(l,r,num,n*2);
	else if(mid<l)
		update(l,r,num,n*2+1);
	else
	{
		update(l,mid,num,n*2);
		update(mid+1,r,num,n*2+1);
	}
}
void getsum(long long l,long long r,long long n)
{
	long long mid=(f[n].l+f[n].r)/2;
	if(f[n].l<=l&&f[n].r>=r&&f[n].color==0)
	{
		//printf("%d %d %d\n",l,r,f[n].num);
		sum1=sum1+(r-l+1)*f[n].num;
	}
	else if(mid>=r)
		getsum(l,r,n*2);
	else if(mid<l)
		getsum(l,r,n*2+1);
	else 
	{
		getsum(l,mid,n*2);
		getsum(mid+1,r,n*2+1);
	}
}
long long getsum1(long long l,long long r,long long n)
{
	long long mid=(f[n].l+f[n].r)/2;
	if(f[n].l==l&&f[n].r==r)
		return f[n].sum;
	else if(mid>=r)
		getsum1(l,r,n*2);
	else if(mid<l)
		getsum1(l,r,n*2+1);
	else {
		return getsum1(l,mid,n*2)+getsum1(mid+1,r,n*2+1);
	}
}

int main()
{
	long long i,n,q,l,r,num;
	char c[4];
	scanf("%lld%lld",&n,&q);
	{
		for(i=1;i<=n;i++)
			scanf("%lld",&date[i]);
		build(1,n,1);
		getchar();
		for(i=1;i<=q;i++)
		{
			scanf("%s",c);
			if(c[0]=='Q')
			{
				sum1=0;
				scanf("%lld%lld",&l,&r);
				getsum(l,r,1);
				sum1=sum1+getsum1(l,r,1);
				printf("%lld\n",sum1);
			}
			else if(c[0]=='C')
			{
				scanf("%lld%lld%lld",&l,&r,&num);
				update(l,r,num,1);
			}
		}
	}
	return 0;
}

求解释啊。

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