HDU3874:Necklace

点击打开题目链接

Necklace

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1993    Accepted Submission(s): 711


Problem Description
Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count it once. We define the beautiful value of some interval [x,y] as F(x,y). F(x,y) is calculated as the sum of the beautiful value from the xth ball to the yth ball and the same value is ONLY COUNTED ONCE. For example, if the necklace is 1 1 1 2 3 1, we have F(1,3)=1, F(2,4)=3, F(2,6)=6.

Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them.
 

Input
The first line is T(T<=10), representing the number of test cases.
  For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
 

Output
For each query, output a line contains an integer number, representing the result of the query.
 

Sample Input
      
      
2 6 1 2 3 4 3 5 3 1 2 3 5 2 6 6 1 1 1 2 3 5 3 1 1 2 4 3 5
 

Sample Output
      
      
3 7 14 1 3 6
 

Source
 

Recommend
lcy
 


=====================================题目大意=====================================


Mery有一串由N个魔法珠做成的漂亮项链,每个魔法珠都有一个魅力值,现在Mery想让你帮她计算第L颗到第R颗(L<=R)不同珠子的

力值之和,

其中魅力值相同的珠子由于看起来一样所以只能被计算一次。


=====================================算法分析=====================================


线段树(树状数组)加离线算法(第一次接触唉)(点击打开资料链接)。

本题最大的困难在于相同珠子的处理,如果有办法让查询区间内不存在相同珠子那就太好办了。

但是查询区间是随机的,这让问题看起来无法解决。

而离线算法正是这样的一个算法:读入所有输入数据,视情况有序得依次处理每组输入数据,也就是化随机输入为有序输入

(纯属个人见解)。

根据离线算法的启示,本题可以如此处理:

将读入的所有查询区间根据右端点升序排序,依次处理每个查询区间。

而在处理每个查询区间时只需保证区间[1,查询区间的右端点]内相同珠子只有最右端的一个有效即可

当处理下一个区间的时候,则逐个插入珠子至下一个区间的右端点。

如果在插入珠子时发现之前插入过相同珠子,则删除之前的珠子后再插入(这很好做到,只需用一个数组记录即可)。

这样在处理下一个查询区间的时候,区间[1,查询区间的右端点]内相同珠子仍然只有最右端的一个有效。


=======================================代码=======================================


一、线段树。



#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define LSON(N)      ((N)<<1  )               
#define RSON(N)      ((N)<<1|1)                    
#define MID(L,R)     (((L)+(R))>>1)  

const int MAXN=50005;
const int MAXM=200005;
const int MAXV=1000005;

int T,N,M,Ball[MAXN],Record[MAXV];

long long SegmTree[MAXN<<2],Ans[MAXM];

struct INTERVAL { int L,R,ID; } Inter[MAXM];

bool cmp(INTERVAL& I1,INTERVAL& I2)
{
	return (I1.R<I2.R);
}

void Insert(int InsID,int InsVal,int L,int R,int N)
{
	SegmTree[N]+=InsVal;
	if(L!=R)
	{
		int M=MID(L,R);
		if(InsID<=M) { Insert(InsID,InsVal,L,M,LSON(N)); }
		else { Insert(InsID,InsVal,M+1,R,RSON(N)); }
	}
}

long long Query(INTERVAL I,int L,int R,int N)  
{  
    if(I.R<L||R<I.L) { return 0; }  
    if(I.L<=L&&R<=I.R) { return SegmTree[N]; }  
    int M=MID(L,R);  
    return Query(I,L,M,LSON(N))+Query(I,M+1,R,RSON(N));  
}  

void ReadAnsDealData()
{
	memset(Record,0,sizeof(Record));
	memset(SegmTree,0,sizeof(SegmTree));
	scanf("%d",&N);
	for(int i=1;i<=N;++i)
	{
		scanf("%d",&Ball[i]);
	}
	scanf("%d",&M);
	for(i=0;i<M;++i)
	{
		scanf("%d%d",&Inter[i].L,&Inter[i].R);
		Inter[i].ID=i;
	}
	sort(Inter,Inter+M,cmp);
}

int main()
{
	while(scanf("%d",&T)==1) while(T--)
	{
		ReadAnsDealData();
		int curball=1;
		for(int i=0;i<M;++i)
		{
			while(curball<=Inter[i].R)
			{
				int val=Ball[curball];
				if(Record[val]) { Insert(Record[val],-val,1,N,1); }
				Record[val]=curball;
				Insert(curball,val,1,N,1);
				++curball;
			}
			Ans[Inter[i].ID]=Query(Inter[i],1,N,1);
		}
		for(i=0;i<M;++i)
		{
			printf("%I64d\n",Ans[i]);
		}
	}
	return 0;
}


二、树状数组。




#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define LOWBIT(X) ((X)&((~X)+1))

const int MAXN=50005;
const int MAXM=200005;
const int MAXV=1000005;

int T,N,M,Ball[MAXN],Record[MAXV];

long long BinTree[MAXN<<2],Ans[MAXM];

struct INTERVAL { int L,R,ID; } Inter[MAXM];

bool cmp(INTERVAL& I1,INTERVAL& I2)
{
	return (I1.R<I2.R);
}

void Insert(int InsID,int InsVal)
{
	while(InsID<=N)
	{
		BinTree[InsID]+=InsVal;
		InsID+=LOWBIT(InsID);
	}
}

long long Query(int QueID)  
{  
	long long cnt=0;
	while(QueID)
	{
		cnt+=BinTree[QueID];
		QueID-=LOWBIT(QueID);
	}
	return cnt;
}  

void ReadAnsDealData()
{
	memset(Record,0,sizeof(Record));
	memset(BinTree,0,sizeof(BinTree));
	scanf("%d",&N);
	for(int i=1;i<=N;++i)
	{
		scanf("%d",&Ball[i]);
	}
	scanf("%d",&M);
	for(i=0;i<M;++i)
	{
		scanf("%d%d",&Inter[i].L,&Inter[i].R);
		Inter[i].ID=i;
	}
	sort(Inter,Inter+M,cmp);
}

int main()
{
	while(scanf("%d",&T)==1) while(T--)
	{
		ReadAnsDealData();
		int curball=1;
		for(int i=0;i<M;++i)
		{
			while(curball<=Inter[i].R)
			{
				int val=Ball[curball];
				if(Record[val]) { Insert(Record[val],-val); }
				Record[val]=curball;
				Insert(curball,val);
				++curball;
			}
			Ans[Inter[i].ID]=Query(Inter[i].R)-Query(Inter[i].L-1);
		}
		for(i=0;i<M;++i)
		{
			printf("%I64d\n",Ans[i]);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值