HDU 3333 Turing Tree 主席树/线段树离线处理

After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick thing happens again... 

Now given a sequence of N numbers A1, A2, ..., AN and a number of Queries(i, j) (1≤i≤j≤N). For each Query(i, j), you are to caculate the sum of distinct values in the subsequence Ai, Ai+1, ..., Aj.

Input

The first line is an integer T (1 ≤ T ≤ 10), indecating the number of testcases below. 
For each case, the input format will be like this: 
* Line 1: N (1 ≤ N ≤ 30,000). 
* Line 2: N integers A1, A2, ..., AN (0 ≤ Ai ≤ 1,000,000,000). 
* Line 3: Q (1 ≤ Q ≤ 100,000), the number of Queries. 
* Next Q lines: each line contains 2 integers i, j representing a Query (1 ≤ i ≤ j ≤ N).

Output

For each Query, print the sum of distinct values of the specified subsequence in one line.

Sample Input

2
3
1 1 4
2
1 2
2 3
5
1 1 2 1 3
3
1 5
2 4
3 5

Sample Output

1
5
6
3
6

题意:给出N长的序列,Q个询问(x,y),输出[x,y]所有不同的数之和。

思路:和spoj dquery基本一样,只是这里是求和的.

SPOJ DUQERY:https://blog.csdn.net/why932346109/article/details/98955619

主席树:

#include<map>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=3e4+110;
map<int,int> mp;
ll a[maxn];
int root[maxn],cnt;
struct node{
	int l;
	int r;
	ll sum;
}tree[maxn*40];
void pushup(int cur)
{
	tree[cur].sum=tree[tree[cur].l].sum+tree[tree[cur].r].sum;
}
void build(int &cur,int l,int r)
{
	cur=++cnt;
	tree[cur].sum=0;
	if(l==r) return ;
	int m=(l+r)>>1;
	build(tree[cur].l,l,m);
	build(tree[cur].r,m+1,r);
}
void update(int &now,int last,int l,int r,int tar,ll val)
{
	now=++cnt;
	tree[now]=tree[last];
	if(l==r)
	{
		tree[now].sum=val;
		return ;
	}
	int m=(l+r)>>1;
	if(tar<=m) update(tree[now].l,tree[last].l,l,m,tar,val);
	else update(tree[now].r,tree[last].r,m+1,r,tar,val);
	pushup(now);
}
ll query(int now,int L,int R,int l,int r)
{
	if(L<=l&&r<=R) return tree[now].sum;
	ll res=0;
	int m=(l+r)>>1;
	if(L<=m) res+=query(tree[now].l,L,R,l,m);
	if(R>m)  res+=query(tree[now].r,L,R,m+1,r);
	return res;
}
int main()
{
	int t,n,q,l,r,tmp;
	scanf("%d",&t);
	while(t--)
	{
		cnt=0;
		mp.clear();
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		build(root[0],1,n);	
		for(int i=1;i<=n;i++)
		{
			if(!mp[a[i]])
			{
				update(root[i],root[i-1],1,n,i,a[i]);
				mp[a[i]]=i;
			}
			else
			{
				update(tmp,root[i-1],1,n,mp[a[i]],0);
				update(root[i],tmp,1,n,i,a[i]);
				mp[a[i]]=i;
			}
		}
		scanf("%d",&q);
		while(q--)
		{
			scanf("%d%d",&l,&r);
			ll ans=query(root[r],l,r,1,n);
			printf("%lld\n",ans);
		}
	}
	return 0;
}

线段树离线处理:

#include<map>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=6e4+100;
ll a[maxn<<2],res[maxn<<2];
map<ll,int> mp;
int n;
struct node{
	int l;
	int r;
	int id;
}qu[maxn<<2];
struct Node{
	int l;
	int r;
	ll sum; 
}tree[maxn<<2];
void pushup(int cur)
{
	tree[cur].sum=tree[cur<<1].sum+tree[cur<<1|1].sum;
}
void build(int l,int r,int cur)
{
	tree[cur].l=l;
	tree[cur].r=r;
	tree[cur].sum=0;
	if(l==r) return ;
	int m=(l+r)>>1;
	build(l,m,cur<<1);
	build(m+1,r,cur<<1|1);
}
void update(int tar,ll val,int cur)
{
	if(tree[cur].l==tree[cur].r)
	{
		tree[cur].sum+=val;
		return ;
	}
	if(tar<=tree[cur<<1].r) update(tar,val,cur<<1);
	else update(tar,val,cur<<1|1);
	pushup(cur);
}
ll query(int L,int R,int cur)
{
	if(L<=tree[cur].l&&tree[cur].r<=R) return tree[cur].sum;
	ll res=0;
	if(L<=tree[cur<<1].r) res+=query(L,R,cur<<1);
	if(R>tree[cur<<1].r) res+=query(L,R,cur<<1|1);
	return res;
}
bool cmp(node x,node y)
{
	return x.r<y.r;
}
int main()
{
	int t,q;
	scanf("%d",&t);
	while(t--)
	{
		mp.clear();
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		build(1,n,1);
		scanf("%d",&q);
		for(int i=1;i<=q;i++)
		{
			scanf("%d%d",&qu[i].l,&qu[i].r);
			qu[i].id=i;
		}
		sort(qu+1,qu+1+q,cmp);
		int j=1;
		for(int i=1;i<=q;i++)
		{
			for(;j<=qu[i].r;j++)
			{
				if(mp[a[j]]) update(mp[a[j]],-a[j],1);
				update(j,a[j],1);
				mp[a[j]]=j;
			}
			res[qu[i].id]=query(qu[i].l,qu[i].r,1);
		}
		for(int i=1;i<=q;i++) printf("%lld\n",res[i]);
	}
	return 0; 
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值