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

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;
}
时间: 2024-12-13 08:03:06

HDU 3333 Turing Tree(离线树状数组)的相关文章

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

HDU 3333 Turing Tree 题目链接 题意:给定一个数组,每次询问一个区间,求出这个区间不同数字的和 思路:树状数组离线处理,把询问按右端点判序,然后用一个map记录下每个数字最右出现的位置,因为一个数字在最右边出现,左边那些数字等于没用了,利用树状数组进行单点修改区间查询即可 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <map> using n

HDU 3333 Turing Tree(树状数组 || 线段树)

题意:给定一个区间,q个查询,对于每次查询回答这个区间内所有不重复的数的和. 思路:可以考虑使用树状数组来做. 先读入所有查询,离线来做,将所有查询按右端点升序排序. 那么我们从给定区间的第一个元素开始遍历这个区间,在此过程中更新每一个元素上一次出现的位置,每次将现在位置加上a[i]并将lastpos位置减去a[i], 也就是说,我们每一步都是保留与当前位置距离最近的重复元素值,其余置零,那么这样肯定能保证答案正确, 如果遍历到的这个位置恰好是一个询问的右端点,那么我们输出修改后的区间值之和.

hdu 3333 Turing Tree(线段树)

题目链接:hdu 3333 Turing Tree 题目大意:给定一个长度为N的序列,有M次查询,每次查询l,r之间元素的总和,相同元素只算一次. 解题思路:涨姿势了,线段树的一种题型,离线操作,将查询按照右区间排序,每次考虑一个询问,将mv ~ r的点全部标记为存在,并且对于每个位置i,如果A[i]在前面已经出现过了,那么将前面的那个位置减掉A[i],当前位置添加A[i],这样做维护了每个数尽量做,那么碰到查询,用sum[r] - sum[l-1]即可. #include <cstdio>

hdu 4417 Super Mario(离线树状数组|划分树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2584    Accepted Submission(s): 1252 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

HDU 3333 Turing Tree (离线询问+线段树)

题目地址:HDU 3333 将询问离线保存下来,然后将数组的点离散化,记录每个值上一次出现的位置.然后枚举数组的数,若当前枚举的数前面出现过,那么就删掉前面出现过的那个位置上的数,更新当前这个位置上的数,然后那些所有询问的右端点为当前位置的就可以通过查询来得到结果了. 更新与查询用线段树来优化. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #

hdu 4777 Rabbit Kingdom(离线树状数组&amp;思维)

Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 964    Accepted Submission(s): 315 Problem Description Long long ago, there was an ancient rabbit kingdom in the forest. Every rab

HDU 3333 Turing Tree (主席树)

题意:给定上一个序列,然后有一些询问,求区间 l - r 中有多少个不同的数的和. 析:和求区间不同数的方法是一样,只要用主席树维护就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iost

HDU 3333 Turing Tree 树状数组 离线查询

题意: 给你一个数列,然后有n个查询,问你给定区间中不同数字的和是多少. 思路还是比较难想的,起码对于蒟蒻我来说. 将区间按照先右端点,后左端点从小到大排序之后,对于每个查询,我只要维护每个数字出现的最后一次就可以了(这个结论稍微想一下就可以证明是正确的). 然后就是简单的点更新,区间求和问题了- #include <cstdio> #include <cstring> #include <iostream> #include <map> #include

HDU 3333 Turing Tree (离散化+离线处理+树状数组)

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 t

HDU 5156 - Harry and Christmas tree (dfs序+离线树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=5156 BC#25的C题. 题意是:给出一颗大小为n的树,以1为根,然后给出m次染色,每次将节点u加上一种颜色(一个节点可以有多个颜色). 最后查询树上每个节点对应子树上包含的不同颜色数量. 当时这场比赛没有做,回来看一下题目,没看标解就试着敲了一遍,于是解题思路从一开始就走上了不归路. 标解是O(n+m)的方法,主要思路是将问题转为:一次染色表示将u到根节点的路径都染上这种颜色. 但这样做需要去重,因为如果u