Turing Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5745 Accepted Submission(s): 2044
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.
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
Code
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
const int Max=100005;
struct query{
int lq;
int rq;
int inum;
}q[Max];//查询区间
ll seg[Max*4];
bool cmp(query a,query b){
if(a.rq<b.rq)
return true;
return false;
}
ll sum(ll a,ll b){
return a+b;
}
ll query(int root,int q_c_l,int q_c_r,int q_l,int q_r){
ll res=0;
if(q_l<=q_c_l && q_c_r<=q_r){
return seg[root];
}
int mid=(q_c_l+q_c_r)>>1;
if(q_l<=mid) res+=query(root*2,q_c_l,mid,q_l,q_r);
if(q_r>mid) res+=query(root*2+1,mid+1,q_c_r,q_l,q_r);
return res;
}
void update(int root,int q_c_l,int q_c_r,int pos,int y){
if(q_c_l==q_c_r){
seg[root]+=y;
return;
}
int mid=(q_c_l+q_c_r)>>1;
if(pos<=mid) update(root*2,q_c_l,mid,pos,y);
else update(root*2+1,mid+1,q_c_r,pos,y);
seg[root]=seg[root*2]+seg[root*2+1];
}
int main(){
// freopen("E://1001.in","r",stdin);
// freopen("E://1001.out","w",stdout);
int T;
cin>>T;
while(T--)
{
map<int,int>mapp;
mapp.clear();
memset(seg,0,sizeof(seg));
int arr[Max];
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
int Q;
cin>>Q;
for(int i=0;i<Q;i++)
{
cin>>q[i].lq>>q[i].rq;
q[i].inum=i;
}
sort(q,q+Q,cmp);
int N=0;
// N=cm(2,n)-1;
for(N=1;N<n;N<<=1);
N<<1;//这个N可以适当放大一些,在内存允许的范围内。
int p=0;
ll ans[Max];
for(int i=0;i<Q;i++)
{
while(p<q[i].rq){//如果arr[]之前已经出现过了这个树
p++;
if(mapp.count(arr[p])){
int update_c=mapp[arr[p]];
int y=-arr[p]; //删除树上节点的值
update(1,1,N,update_c,y);
}
int update_c_c=p;
int y_c=arr[p];
update(1,1,N,update_c_c,y_c);
mapp[arr[p]]=p;//更新map
}
int query_c_l=q[i].lq;
int query_c_r=q[i].rq;
ans[q[i].inum]=query(1,1,N,query_c_l,query_c_r);
}
for(int i=0;i<Q;i++)
{
cout<<ans[i]<<endl;
}
}
return 0;
}
这道题我写了一个下午。。由于自己是个小白,是在我同学的帮助下理解的,然后自己写了又出现了问题,又在网上查资料。。这题主要是一个数一个数往树上增加数据,map的key是唯一的所以他保存的是最后一出现这个key的位置,map中有count这个内置函数,他可以把知道这个值有没有出现过,出现过几次!arr[]中在这个key之前出现过的都清除了,然后再更新线段树,好在查询和更新树的代码很好写。