标签:des style blog http io color ar os sp
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
题意:求给定区间内不同值的和,重复的计算一次。。询问Q<=100000,考虑离线
做法。将所有的询问保存下来,按照右端点由小到大排序。跟新小于p[i].r的值,对
值进行hash,保存值的位置,相同时,原位置上值更新为0,此值的位置更新的当前位置。。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<map>
- typedef long long LL;
- using namespace std;
- #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
- #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
- #define CLEAR( a , x ) memset ( a , x , sizeof a )
- const int maxn=30005;
- const int maxm=100005;
- LL sum[maxn<<2],a[maxn],ans[maxm];
- struct node{
- int l,r,index;
- bool operator<(const node a)const
- {
- return r<a.r;
- }
- }p[maxm];
- map<LL,int>hash;
- int t,n,m;
- void pushup(int rs)
- {
- sum[rs]=sum[rs<<1]+sum[rs<<1|1];
- }
- void build(int rs,int l,int r)
- {
- sum[rs]=0;
- if(l==r) return ;
- int mid=(l+r)>>1;
- build(rs<<1,l,mid);
- build(rs<<1|1,mid+1,r);
- }
- void update(int rs,LL c,int x,int l,int r)
- {
- if(l==r)
- {
- sum[rs]=c;
- return ;
- }
- int mid=(l+r)>>1;
- if(x<=mid) update(rs<<1,c,x,l,mid);
- else update(rs<<1|1,c,x,mid+1,r);
- pushup(rs);
- }
- LL query(int rs,int x,int y,int l,int r)
- {
- if(l>=x&&r<=y)
- return sum[rs];
- int mid=(l+r)>>1;
- LL res=0;
- if(x<=mid) res+=query(rs<<1,x,y,l,mid);
- if(y>mid) res+=query(rs<<1|1,x,y,mid+1,r);
- return res;
- }
- void solve()
- {
- int pos=1;
- REPF(i,1,m)
- {
- while(p[i].r>=pos)
- {
- if(hash[a[pos]])
- update(1,0,hash[a[pos]],1,n);
- hash[a[pos]]=pos;
- update(1,a[pos],pos,1,n);pos++;
- }
- ans[p[i].index]=query(1,p[i].l,p[i].r,1,n);
- }
- }
- int main()
- {
- scanf("%d",&t);
- int x,y;
- while(t--)
- {
- hash.clear();
- scanf("%d",&n);
- build(1,1,n);
- REPF(i,1,n) scanf("%I64d",&a[i]);
- scanf("%d",&m);
- REPF(i,1,m)
- {
- scanf("%d%d",&p[i].l,&p[i].r);
- p[i].index=i;
- }
- sort(p+1,p+1+m);
- solve();
- REPF(i,1,m)
- printf("%I64d\n",ans[i]);
- }
- return 0;
- }
HDU 3333 Turing Tree(离线线段树)
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/yuyanbian/p/4096328.html