您的位置:首页 > 其它

HDU 3333 Turing Tree(线段树)

2017-09-24 18:27 387 查看


Turing Tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5748    Accepted Submission(s): 2046


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

 

Source

HDOJ Monthly Contest – 2010.03.06

【思路】

题目意在求一个给定序列的区间不同数之和,所以我们要对区间的数字进行去重。朴素的做法是O(n³)的复杂度,显然是不能接受的。在这里用线段树和排序处理的巧妙做法,就可以把复杂度降到O(nlogn)。建一棵节点sum都为0的线段树,先将所求区间根据右端点从小到大排序,然后每次以右端点为终点,从上次停下的位置扫一下,在某一个数的节点处加上它,在它上次出现的地方,减掉它,然后求区间和即可。正确性证明:这么做可以把重复的数尽可能往后推,每一次都是扫到要求的区间的右端点,所以能保证不会多减,且不论左端点如何,都可以保证区间里必定计上了所有要计的数,所以只需要对右端点进行排序即可。

【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;

const int MAXN = 30010, MAXM = 100005;

struct segment {
int l, r, mid;
long long sum;
};

struct question {
int l, r, id;

bool operator<(const question &another) const
{
return r < another.r;
}
};

int t, n, q;
int a[MAXN];
long long ans[MAXM];
segment tree[MAXN * 4];
question qu[MAXM];
map<int, int> visited;

void build(int left, int right, int root)
{
tree[root].l = left;
tree[root].r = right;
tree[root].mid = (left + right) >> 1;
tree[root].sum = 0;
if (left == right) return;
build(left, tree[root].mid, root << 1);
build(tree[root].mid + 1, right, root << 1 | 1);
tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;
}

void modify(int index, int num, int root)
{
if (tree[root].l == tree[root].r) {
tree[root].sum += (long long)num;
return;
}
if (index <= tree[root].mid) modify(index, num, root << 1);
if (index >= tree[root].mid + 1) modify(index, num, root << 1 | 1);
tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;
}

long long query(int left, int right, int root)
{
if (left <= tree[root].l && tree[root].r <= right) return tree[root].sum;
long long ans = 0;
if (left <= tree[root].mid) ans += query(left, right, root << 1);
if (right >= tree[root].mid + 1) ans += query(left, right, root << 1 | 1);
return ans;
}

int main()
{
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d %d", &qu[i].l, &qu[i].r);
qu[i].id = i;
}
sort(qu + 1, qu + 1 + q);
build(1, n, 1);
visited.clear();
memset(ans, 0, sizeof(ans));
int j = 1;
for (int i = 1; i <= q; i++) {
while (j <= n && j <= qu[i].r) {
if (visited.count(a[j]))
modify(visited[a[j]], -a[j], 1);
modify(j, a[j], 1);
visited[a[j]] = j;
j++;
}
ans[qu[i].id] = query(qu[i].l, qu[i].r, 1);
}
for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: