Turing Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5972 Accepted Submission(s): 2138
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思路:这是线段树离线操作的一个应用,首先我们先将查询的区间排序,按照区间右端小的放在前面,然后从[1,1],[1,2],[1,3],这样更新树,然后前面出现过的数字,就变为0,这样不断更新。代码:#include <iostream> #include <stdio.h> #include <string.h> #include <map> #include <algorithm> using namespace std; const int M=30005; struct node { int l,r; long long int sum; }a[M*4]; struct no { int l,r; int i; }b[100005]; void build(int i,int l,int r) { a[i].l=l; a[i].r=r; a[i].sum=0; if(a[i].l==a[i].r)return ; int mid=(a[i].l+a[i].r)/2; build(i*2,l,mid); build(i*2+1,mid+1,r); } void change(int l,int r,int x,int y) { if(l<=a[x].l&&r>=a[x].r) { a[x].sum=y; return ; } int mid=(a[x].l+a[x].r)/2; if(l<=mid) { change(l,r,x*2,y); } if(r>mid)change(l,r,x*2+1,y); a[x].sum=a[x*2].sum+a[x*2+1].sum; } long long int findx(int i,int l,int r) { if(l<=a[i].l&&r>=a[i].r) { return a[i].sum; } int mid=(a[i].l+a[i].r)/2; long long int ans=0; if(l<=mid)ans+=findx(i*2,l,r); if(r>mid)ans+=findx(i*2+1,l,r); return ans; } bool cmp(no a,no b) { return a.r<b.r; } long long int d[100005]; int c[M]; int main() { int T; cin>>T; while(T--) { int n; cin>>n; int i; build(1,1,n); map<int,int>mm; for(i=1;i<=n;i++) { scanf("%d",&c[i]); } int m; scanf("%d",&m); for(i=0;i<m;i++) { scanf("%d%d",&b[i].l,&b[i].r); b[i].i=i; } sort(b,b+m,cmp); int z=0; int t; for(i=1;i<=n;i++) { t=mm[c[i]]; if(t==0) { change(i,i,1,c[i]); mm[c[i]]=i; } else { change(t,t,1,0); change(i,i,1,c[i]); mm[c[i]]=i; } for(;z<m&&b[z].r==i;z++) { d[b[z].i]=findx(1,b[z].l,b[z].r); } } for(i=0;i<m;i++) { cout<<d[i]<<endl; } } return 0; }