hdu 3874 Necklace 求数组任意区间和(相同元素只算一次) 树状数组+离线算法
2011-07-28 17:04
465 查看
[align=left]Problem Description[/align]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.
[align=left]Input[/align]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.
[align=left]Output[/align]For each query, output a line contains an integer number, representing the result of the query.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
//
先对y排序,记录每一个值前一个出现的位置,然后每次一个值v时,若前面出现过v则将前面一个v灭掉,然后将当前位置加上v。。。其实就是保留最后一个出现v的位置的v,前面的地方都归0.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=50010;
//离线算法
int n;
__int64 c[maxn];
int lowbit(int x){return x&(-x);}
void update(int x,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
c[i]+=val;
}
}
__int64 getsum(int x)
{
__int64 cnt=0;
for(int i=x;i>=1;i-=lowbit(i))
{
cnt+=c[i];
}
return cnt;
}
int a[maxn];
map<int,int> hash;
struct Node
{
int l,r;
int idx;
};
Node qu[maxn*4];
__int64 ans[maxn*4];
bool cmp(Node h,Node k)
{
return h.r<k.r;
}
int main()
{
int ci;scanf("%d",&ci);
while(ci--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int q;scanf("%d",&q);
for(int i=0;i<q;i++)
{
int l,r;scanf("%d%d",&l,&r);
if(l>r) swap(l,r);
qu[i].l=l,qu[i].r=r;
qu[i].idx=i;
}
sort(qu,qu+q,cmp);
for(int i=1;i<=n;i++) c[i]=0;
hash.clear();
int r=1;
for(int i=0;i<q;i++)
{
while(r<=qu[i].r)
{
if(hash[a[r]]!=0)
{
update(hash[a[r]],-a[r]);
}
hash[a[r]]=r;
update(r,a[r]);
r++;
}
ans[qu[i].idx]=getsum(qu[i].r)-getsum(qu[i].l-1);
}
for(int i=0;i<q;i++) printf("%I64d\n",ans[i]);
}
return 0;
}
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.
[align=left]Input[/align]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.
[align=left]Output[/align]For each query, output a line contains an integer number, representing the result of the query.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
3 7 14 1 3 6
//
先对y排序,记录每一个值前一个出现的位置,然后每次一个值v时,若前面出现过v则将前面一个v灭掉,然后将当前位置加上v。。。其实就是保留最后一个出现v的位置的v,前面的地方都归0.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=50010;
//离线算法
int n;
__int64 c[maxn];
int lowbit(int x){return x&(-x);}
void update(int x,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
c[i]+=val;
}
}
__int64 getsum(int x)
{
__int64 cnt=0;
for(int i=x;i>=1;i-=lowbit(i))
{
cnt+=c[i];
}
return cnt;
}
int a[maxn];
map<int,int> hash;
struct Node
{
int l,r;
int idx;
};
Node qu[maxn*4];
__int64 ans[maxn*4];
bool cmp(Node h,Node k)
{
return h.r<k.r;
}
int main()
{
int ci;scanf("%d",&ci);
while(ci--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int q;scanf("%d",&q);
for(int i=0;i<q;i++)
{
int l,r;scanf("%d%d",&l,&r);
if(l>r) swap(l,r);
qu[i].l=l,qu[i].r=r;
qu[i].idx=i;
}
sort(qu,qu+q,cmp);
for(int i=1;i<=n;i++) c[i]=0;
hash.clear();
int r=1;
for(int i=0;i<q;i++)
{
while(r<=qu[i].r)
{
if(hash[a[r]]!=0)
{
update(hash[a[r]],-a[r]);
}
hash[a[r]]=r;
update(r,a[r]);
r++;
}
ans[qu[i].idx]=getsum(qu[i].r)-getsum(qu[i].l-1);
}
for(int i=0;i<q;i++) printf("%I64d\n",ans[i]);
}
return 0;
}
相关文章推荐
- HDU 3874 Necklace(树状数组离线处理)
- hdu 1806 Frequent values(给定一个非降序数组,求任意区间内出现次数最多的数的次数)
- HDU 3874 Necklace(树状数组的离线操作)
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
- HDU 1556 Color the ball【线段树区间更新,一次查询+数组模拟】
- HDU 3874 Necklace(树状数组的离线操作)
- hdu 3874 Necklace (树状数组)
- 从长度为n的数组中(元素互不相同)任意选择m个数的所有组合
- HDU 3874 Necklace (树状数组)
- HDU 3874 Necklace 区间查询的离线操作
- vector数组中删除相同元素(输入的元素输出时只能出现一次)
- hdu 3874 Necklace 线段树单点更新区间求和
- HDU 3874 Necklace (数状数组)
- HDU 3874 Necklace (线段树单点更新+区间查询+离线操作)
- HDU 3874 Necklace(树状数组离线处理)
- HDU 3333&&HDU-3874 Necklace 离线树状数组
- hdu 3874 Necklace 树状数组 离线操作
- HDU 3874 Necklace 树状数组 + 离线处理
- HDU 3874 Necklace(线段树啊 单点更新 区间求和)
- hdu 3874 Necklace(树状数组的灵活应用)