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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章