码迷,mamicode.com
首页 > 编程语言 > 详细

HDU 3333 Turing Tree(离线树状数组)

时间:2017-02-12 15:55:46      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:integer   this   air   bsp   contains   opera   using   first   value   

Turing Tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5014    Accepted Submission(s): 1777

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

 

题目链接:HDU 3333

对于莫队式暴力就没啥好说的了,时间慢不说代码量也比较大,还是离线+BIT快

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 30010;
const int M = 200010;
struct info
{
    int k, l, r, flag, id;
    info() {}
    info(int _k, int _l, int _r, int _flag, int _id): k(_k), l(_l), r(_r), flag(_flag), id(_id) { }
    bool operator<(const info &rhs)const
    {
        return k < rhs.k;
    }
};
info Q[M];
LL T[N], ans[M >> 1];
int arr[N];
map<int, int>last;

void init()
{
    CLR(T, 0);
    CLR(ans, 0);
    last.clear();
}
void add(int k, LL v)
{
    while (k < N)
    {
        T[k] += v;
        k += (k & -k);
    }
}
LL getsum(int k)
{
    LL ret = 0;
    while (k)
    {
        ret += T[k];
        k -= (k & -k);
    }
    return ret;
}
int main(void)
{
    int tcase, n, m, i;
    scanf("%d", &tcase);
    while (tcase--)
    {
        init();
        scanf("%d", &n);
        for (i = 1; i <= n; ++i)
            scanf("%d", &arr[i]);
        scanf("%d", &m);
        int qcnt = 0;
        for (i = 0; i < m; ++i)
        {
            int l, r;
            scanf("%d%d", &l, &r);
            Q[qcnt++] = info(l, l, r, 0, i);
            Q[qcnt++] = info(r, l, r, 1, i);
        }
        sort(Q, Q + qcnt);
        int x = 1;
        for (i = 0; i < qcnt; ++i)
        {
            while (x <= Q[i].k)
            {
                if (last[arr[x]])
                    add(last[arr[x]], -arr[x]);
                add(x, arr[x]);
                last[arr[x]] = x;
                ++x;
            }
            if (Q[i].flag)
                ans[Q[i].id] += getsum(Q[i].r) - getsum(Q[i].l - 1);
            else
            {
                add(x - 1, -arr[x - 1]);
                ans[Q[i].id] -= getsum(Q[i].r) - getsum(Q[i].l - 1);
                add(x - 1, arr[x - 1]);
            }
        }
        for (i = 0; i < m; ++i)
            printf("%I64d\n", ans[i]);
    }
    return 0;
}

HDU 3333 Turing Tree(离线树状数组)

标签:integer   this   air   bsp   contains   opera   using   first   value   

原文地址:http://www.cnblogs.com/Blackops/p/6390957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!