http://acm.hdu.edu.cn/showproblem.php?pid=3333
Turing Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
231 1 421 22 351 1 2 1 331 52 43 5
Sample Output
15636
题意:
给一个长度为N的数列,M次询问,每次询问一段区间内不同数的和。
大致思路:
离线操作。询问按区间尾部从小到大排序。枚举每个数Ai,算出S = A1--Ai的不同数的和,然后对于询问(j,i),答案就是S减去最后一次出现在j之前的数的和。。这个用一个树状数组记录即可。
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 31000
#define MAXM 110000
#define eps (1e-8)
#define INF 1000000000
#define abs(x) ( (x) > 0? (x): -(x) )
#define sqr(x) ((x) * (x))
#define MAX(a, b) ((a) > (b)? (a): (b))
#define MIN(a, b) ((a) < (b)? (a): (b))
typedef long long LL;
struct Query
{
int first, id, next;
} List[MAXM];
int n, m, cq;
int a[MAXN], last[MAXN];
LL at[MAXN], sum, ans[MAXM];
map< int, int > loc;
template <typename T> void myswap( T &x, T &y ) { T temp = x; x = y; y = temp; }
void init()
{
scanf( "%d", &n );
for ( int i = 1; i <= n; ++i ) scanf( "%d", &a[i] );
scanf( "%d", &m );
cq = 0;
memset( last, 0, sizeof(last) );
for ( int i = 1; i <= m; ++i )
{
int a, b;
scanf( "%d%d", &a, &b );
++cq;
List[cq].first = a;
List[cq].id = i;
List[cq].next = last[b];
last[b] = cq;
}
loc.clear();
memset( at, 0, sizeof(at) );
sum = 0;
}
int next( int x ) { return x & -x; }
void add( int x, LL d )
{
while ( x < MAXN )
{
at[x] += d;
x += next(x);
}
}
LL counts( int x )
{
LL ret = 0;
while ( x )
{
ret += at[x];
x -= next(x);
}
return ret;
}
int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
init();
for ( int i = 1; i <= n; ++i )
{
if ( loc.find(a[i]) != loc.end() ) add( loc[a[i]], -a[i] );
else sum += (LL)a[i];
add( i, a[i] );
loc[a[i]] = i;
int pnt = last[i];
while ( pnt )
{
ans[List[pnt].id] = sum - counts( List[pnt].first - 1 );
pnt = List[pnt].next;
}
}
char format[20];
strcpy( format, "%lld\n" );
for ( int i = 1; i <= m; ++i ) printf( format, ans[i] );
}
return 0;
}