zoukankan      html  css  js  c++  java
  • 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
    ", ans[i]);
        }
        return 0;
    }
  • 相关阅读:
    hadoop的文件系统FileSystem
    关于hadoop的日志
    top命令的使用
    对于多个集合求两两交集(共同关注的用户、共同转载的微薄等)
    hadoop配置含义(继续更新中)
    thrift
    【VS2015】Win7 X64上面安装VS2015
    【经验记录】开发中的实际问题记录
    【VS2012】F5不能自动编译新修改
    斯巴达三百程序员
  • 原文地址:https://www.cnblogs.com/Blackops/p/6390957.html
Copyright © 2011-2022 走看看