Turing Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5415 Accepted Submission(s): 1921
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
题意:给你一段区间,询问区间内不同数的和是多少。
分析:
可持久化线段树来做。(我又换了一个板)
先把各个数离散化,如果这个数没有前驱,那么直接把这个数加进去,如果这个数有前驱,我们在新构造这个数的时候,就先把它的前驱位置-val,然后再这个点在修改的数的基础上加进去。然后查询的时候就查询以r为根的树上,l这个点到r的和。
第一次认真注意了博客的格式23333
在MLE和RE之间来回挣扎
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define ll long long
using namespace std;
const int N = 30010;
int T;
int n,m;
int a[N],b[N],Pre[N];
map<int,int> q;
void lisanhua(){
sort(b+1,b+n+1);
int cnt=unique(b+1,b+n+1)-b-1;
for(register int i=1;i<=n;i++){
int x=a[i];
a[i]=lower_bound(b+1,b+cnt+1,a[i])-b;
q[a[i]]=x;
}
}
struct node{
ll sum;
int l,r;
int ls,rs;
}t[N*50];
int tail=0;
int root[N];
void build(int &root,int l,int r){
root=++tail;
t[root].l=l,t[root].r=r,t[root].sum=0;
if(l==r) return ;
int mid=l+r>>1;
build(t[root].ls,l,mid);
build(t[root].rs,mid+1,r);
}
void update(int root){
t[root].sum=t[t[root].ls].sum+t[t[root].rs].sum;
}
void modify(int &root,int pos,int val,int k){
root=++tail;
t[root]=t[pos];
int l=t[root].l,r=t[root].r;
if(l==r){
t[root].sum+=k;
return ;
}
int mid=l+r>>1;
if(val<=mid) modify(t[root].ls,t[pos].ls,val,k);
else modify(t[root].rs,t[pos].rs,val,k);
update(root);
return ;
}
/*
ll query(int root,int pos){
int l=t[root].l,r=t[root].r;
if(l>=pos){
return t[root].sum;}
int mid=l+r>>1;
if(pos<=mid) return query(t[root].ls,pos)+t[t[root].rs].sum;
else if(pos>mid) return query(t[root].rs,pos);
}*/
ll query(int root,int pos,int val){
int l=t[root].l,r=t[root].r;
if(pos<=l&&val>=r){
return t[root].sum;}
int mid=l+r>>1;
ll ans=0;
if(pos<=mid) ans+=query(t[root].ls,pos,val);
if(val>mid) ans+=query(t[root].rs,pos,val);
return ans;
}
#define ms(x,y) memset(x,y,sizeof(x))
void zero(){
ms(Pre,0);tail=0;
}
int main(){
scanf("%d",&T);
while(T--){
zero();
scanf("%d",&n);
for(register int i=1;i<=n;i++){
scanf("%d",&a[i]);b[i]=a[i];}
lisanhua();
build(root[0],0,n);
for(register int i=1;i<=n;i++){
if(!Pre[a[i]]) {
modify(root[i],root[i-1],i,q[a[i]]);
Pre[a[i]]=i;
continue;
}
int temp;
modify(temp,root[i-1],Pre[a[i]],-q[a[i]]);
modify(root[i],temp,i,q[a[i]]);
Pre[a[i]]=i;
}
scanf("%d",&m);
while(m--){
int l,r;
scanf("%d%d",&l,&r);
//printf("%lld\n",query(root[r],l));
printf("%lld\n",query(root[r],l,r));
}
}
return 0;
}
//其实两种都可以 划掉的应该还快一点。