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

Total Submission(s): 4776 Accepted Submission(s): 1690

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

题解



求一段区间内的不同元素的和。

需要离线处理询问。

先讲询问按照右端点升序排序;

之后顺序扫描每一个数字,如果这个数字之前出现过(用map判断),那么就更新那个那个数字最后的位置为当前位置。并把之前那个数字去掉。

这样可以保证我们维护了一段数字全都不相同的序列a[1..当前的位置],这个序列中用0代表重复的数字,然后处理右区间为当前位置的询问即可。

然后用线段树维护区间和,单点更新,单点上传。很简单。

之前一道类似的题用的是树状数组,所以这次用的线段树。很久没打线段树了。有点生疏。

#include <cstdio>
#include <algorithm>
#include <map>
#include <iostream>
#define lson begin,m,rt<<1
#define rson m+1,end,rt<<1|1
#define LL long long using namespace std; const int MAXN = 30000 + 100;
const int MAXQ = 1e5 + 100; struct abc
{
int l, r, id;
}; int a[MAXN], m, n;
LL sum[MAXN << 2],ans[MAXQ];
map <int, int> frequent;
abc Q[MAXQ]; void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
} void build(int begin, int end,int rt)
{
sum[rt] = 0;
if (begin == end)
return;
int m = (begin + end) >> 1;
build(lson);
build(rson);
} bool cmp(abc a, abc b)
{
return a.r < b.r;
} void add(int begin, int end,int rt, int pos, int key)
{
if (begin == end)
{
sum[rt] += key;
return;
}
int m = (begin + end) >> 1;
if (pos <= m)
add(lson, pos, key);
else
add(rson, pos, key);
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
} LL query(int l, int r, int begin, int end, int rt)
{
if (l <= begin && end <= r)
return sum[rt];
LL temp1 = 0,temp2 = 0;
int m = (begin + end) >> 1;
if (l <= m)
temp1 += query(l, r, lson);
if (m < r)
temp2 += query(l, r, rson);
return temp1 + temp2;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
int t;
input(t);
while (t--)
{
frequent.clear();
input(n);
build(1, n, 1);
for (int i = 1; i <= n; i++)
input(a[i]);
input(m);
for (int i = 1; i <= m; i++)
input(Q[i].l), input(Q[i].r), Q[i].id = i;
sort(Q + 1, Q + 1 + m, cmp);
int temp = 1;
for (int i = 1; i <= n; i++)
{
if (frequent[a[i]])
add( 1, n,1, frequent[a[i]],-a[i]);
frequent[a[i]] = i;
add(1, n, 1, i, a[i]);
while (temp <= m && Q[temp].r == i)
{
ans[Q[temp].id] = query(Q[temp].l, Q[temp].r, 1, n, 1);
temp++;
}
}
for (int i = 1; i <= m; i++)
printf("%I64d\n", ans[i]);
}
return 0;
}

【35.39%】【hdu 3333】Turing Tree的更多相关文章

  1. 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】

    利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...

  2. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  4. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...

  5. 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...

  6. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  7. 【归并排序】【逆序数】HDU 5775 Bubble Sort

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...

  8. 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai  的数 ...

  9. 【规律】【贪心】【数学】HDU 5573 Binary Tree

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 题目大意: 从1走到第k层,下一层的数是上一层的数*2或者*2+1,可以选择加上或者减去走的数 ...

随机推荐

  1. 1.1 Python基础知识 - 变量

    1.什么是变量? 变量是可以通过变量名访问的内存地址,变量通常是可变的. 2.怎样去定义? 变量格式: 变量名 = "变量值" 例如: name = "Zhanghk&q ...

  2. Android 蓝牙扫描代码

    /** * Created by rbq on 2016/11/1. */ import android.bluetooth.BluetoothAdapter; import android.blue ...

  3. Android5.0(Lollipop) BLE蓝牙4.0+浅析demo连接(三)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23363591来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Android5.0(L ...

  4. 【z08】乌龟棋

    描述 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起点出发走到终点 ...

  5. MyCAT分表初体验

    1.mycat二进制包安装 下载地址:http://dl.mycat.io/ tar -zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar ...

  6. MySQL误删数据救命指南

    预防误操作导致文件/数据丢失的建议: 1.欲删除文件时,将rm命令改成mv,可在系统层面将rm命令做个alias(或参考Windows / Mac OSX做法,删除文件时先进回收站).2.删除数据库. ...

  7. oracle 让人抓狂的错误之 null值 与 无值(无结果)-开发系列(一)

    近期.在做开发.写存过的时候碰到一些问题,找了好长时间才发现原因.并且是曾经不知道的. 所以在这给记下来 给自己备忘和大家參考. 一 .null值 以下举个最简单的样例.寻常工作其中肯定比这个sql复 ...

  8. Java设计模式之从[暗黑破坏神存档点]分析备忘录(Memento)模式

    在大部分游戏中,都有一个"存档点"的概念.比如,在挑战boss前,游戏会在某个地方存档,假设玩家挑战boss失败,则会从这个存档点開始又一次游戏.因此,我们能够将这个"存 ...

  9. git入门基础

    git基础 参考: 官网git基础 git 文件的生命周期 文件的生命周期图: git中的文件可以分为4个阶段. Untracked : 这是目录中没有被跟踪的文件,即不在git项目中,使用 git ...

  10. poi读取excell表格

    原文链接:http://blog.csdn.net/qq_37936542/article/details/79024847 最近项目需要实现一个将excell中的数据导入数据库,在网上找到这篇文章, ...