hdu3333.Turing Tree

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.
 

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


题意:

给一个长度为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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值