hdu 3333 Turing Tree

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

Author

3xian@GDUT

  • 离线化
  • 从左到右处理原数列
  • 对询问排序

不再是难题。

/* 
 * File:   main.cpp
 * Author: semprathlon
 *
 * Created on March 25, 2016, 4:02 PM
 */
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef long long ll;

const int maxn = 100010;

struct SegTree {
    int l, r, m;
    ll val;
    SegTree *L, *R;

    SegTree() {}

    SegTree(int x, int y) {
        this->build(x, y);
    }
    
    ~SegTree(){
        if (this->L!=NULL) free(this->L);
        if (this->R!=NULL) free(this->R);
    }

    void build(int x, int y) {
        int mi = (x + y) >> 1;
        l = x;r = y;m = mi;val = 0;
        if (x < y) {
            L = new SegTree(x, m);
            R = new SegTree(m + 1, y);
        }
    }
    
    inline void up(){
        val=L->val+R->val;
    }
    
    void add(int x, int v) {
        if (l == r) {
            val += v;
            return;
        }
        if (x <= m)
            L->add(x, v);
        else if (x > m)
            R->add(x, v);
        up();
    }

    ll query(int x, int y) {
        if (x <= l && r <= y) {
            return val;
        }
        ll res = 0;
        if (x <= m)
            res += L->query(x, y);
        if (y > m)
            res += R->query(x, y);
        return res;
    }
} *ST;

struct Inv {
    int l, r, id;

    Inv() {};

    Inv(int _l, int _r, int _id) : l(_l), r(_r), id(_id) {};
};

inline bool operator<(const Inv& a, const Inv& b) {
        return a.r < b.r;
}


int a[maxn];
ll ans[maxn];

vector v;

vector::iterator it;

map mp;

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        int n, q;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        v.clear();
        scanf("%d", &q);
        for (int i = 0; i < q; i++) {
            int x, y;
            scanf("%d%d", &x, &y);
            v.push_back(Inv(x, y, i));
        }
        sort(v.begin(), v.end());
        it = v.begin();
        ST = new SegTree(1, n);
        fill(ans, ans + q, 0);
        mp.clear();
        for (int i = 1; i <= n; i++) {
            if (mp.find(a[i])!=mp.end()){
                ST->add(mp[a[i]], -a[i]);
            }
            mp[a[i]]=i;
            ST->add(i, a[i]);
            for (; it != v.end() && it->r == i; it++)
                ans[it->id] = ST->query(it->l, it->r);
        }
        for (int i = 0; i < q; i++)
            printf("%I64d\n", ans[i]);
        free(ST);
    }
    return 0;
}
Author

Semprathlon / Simfae Dean

Posted on

03/25/2016

Updated on

07/19/2023

Licensed under

Comments