Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3116 Accepted Submission(s): 1066
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.
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.
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
解题思路:求给定区间内不重复的数字的数字和是多少,采用离线的做法,把询问区间按右端点从小到大排好,开一个map记录每个值的最右位置,每次更新,区间求和
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <bitset>
#include <cmath>
#include <utility>
#define Maxn 100005
#define Maxm 1000005
#define lowbit(x) x&(-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI acos(-1.0)
#define make_pair MP
#define LL long long
#define Inf (1LL<<62)
#define inf 0x3f3f3f3f
#define re freopen("in.txt","r",stdin)
#define wr freopen("out.txt","w",stdout)
using namespace std;
long long tree[50005],ans[200005];
map<int,int> Hash;
struct Q
{
int l;
int r;
int id;
bool operator <(const Q &a)const
{
if(r!=a.r)
return r<a.r;
return l<a.l;
}
}query[200005];
void add(int pos,int n,int w)
{
int i;
for(i=pos;i<=n;i+=lowbit(i))
tree[i]+=w;
}
long long sum(int pos)
{
long long i,sum=0;
for(i=pos;i>0;i-=lowbit(i))
sum+=tree[i];
return sum;
}
int main()
{
int i,t,n,m,a[50005];
//re;wr;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
memset(tree,0,sizeof(tree));
for(i=1;i<=m;i++)
{
scanf("%d%d",&query[i].l,&query[i].r);
if(query[i].l>query[i].r)
swap(query[i].l,query[i].r);
query[i].id=i;
}
sort(query+1,query+1+m);
Hash.clear();
int r=1;
for(i=1;i<=m;i++)
{
while(r<=query[i].r)
{
if(Hash.find(a[r])!=Hash.end())
add(Hash[a[r]],n,-a[r]);
Hash[a[r]]=r;
add(r,n,a[r]);
r++;
}
ans[query[i].id]=sum(query[i].r)-sum(query[i].l-1);
}
for(i=1;i<=m;i++)
printf("%I64d\n",ans[i]);
}
return 0;
}