Turing Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6361 Accepted Submission(s): 2275
Problem Description
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.
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).
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
题意:求一个区间内不同数字的和
思路:这题需要离线处理,先把要询问的区间按照右端点进行排序,让小的排在前面,然后用线段树操作,对于出现多次的数字那就让它在前面位置变为0,然后再进行查询。
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=30000+10;
struct node
{
int l,r;
ll sum;
}T[N<<2];
struct qu
{
int l,r,i;
}q[111111];
ll a[N];
ll ans[111111];
map<ll,int>mp;
bool cmp(qu a,qu b)
{
if(a.r!=b.r) return a.r<b.r;
else return a.l<b.l;
}
void build(int l,int r,int x)
{
T[x].l=l;
T[x].r=r;
T[x].sum=0;
if(l==r)
{
return;
}
int mid=(l+r)/2;
build(l,mid,x*2);
build(mid+1,r,x*2+1);
}
void update(int x,int k,ll num)
{
if(T[x].l==T[x].r&&T[x].l==k)
{
T[x].sum=num;
return;
}
int mid=(T[x].l+T[x].r)/2;
if(k<=mid)update(x*2,k,num);
else update(x*2+1,k,num);
T[x].sum=T[x*2].sum+T[x*2+1].sum;
}
ll query(int l,int r,int x)
{
if(l<=T[x].l&&r>=T[x].r)
{
return T[x].sum;
}
int mid=(T[x].l+T[x].r)/2;
ll sum=0;
if(l<=mid)sum+=query(l,r,x*2);
if(r>mid)sum+=query(l,r,x*2+1);
return sum;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
mp.clear();
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
build(1,n,1);
int Q;
scanf("%d",&Q);
for(int i=1;i<=Q;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
q[i].i=i;
}
sort(q+1,q+Q+1,cmp);
for(int i=1,j=1;i<=n;i++)
{
if(mp[a[i]])
{
update(1,mp[a[i]],0);
update(1,i,a[i]);
mp[a[i]]=i;
//cout<<2<<endl;
}
else
{
//cout<<1<<endl;
update(1,i,a[i]);
//cout<<1<<endl;
mp[a[i]]=i;
}
while(j<=Q&&q[j].r==i)
{
ans[q[j].i]=query(q[j].l,q[j].r,1);
j++;
}
}
for(int i=1;i<=Q;i++)
{
printf("%lld\n",ans[i]);
}
}
return 0;
}