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
 
Source
 
Recommend
lcy
 

题目大意:
查询区间[L, R]内所有不同元素的和。
 
解法:
在线做法我还不清楚,有一个利用树状数组的巧妙的离线做法。
将所有查询[L, R]按右端点R从小到大排序后,依次处理。
要点是记录一个数上一次出现的位置,这样就可以将当前的数在上个位置上的计数消除。
这样做不会影响当前的查询,因为当这个数的当前位置离所查询的区间的右端点更近。
 
Implementation:
总体是个two-pointer
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N(3e4+), M(1e5+); LL bit[N], ans[M];
int pos[N], a[N], b[N], n, q; void add(int x, int v)
{
for(; x<=n; bit[x]+=v, x+=x&-x);
} LL sum(int x)
{
LL res=;
for(; x; res+=bit[x], x-=x&-x);
return res;
} struct P
{
int l, r, id;
P(int l, int r, int id):l(l),r(r),id(id){}
P(){}
bool operator<(const P &b)const{return r<b.r;}
}p[M]; int main()
{
int T;
for(cin>>T; T--; )
{
cin>>n;
for(int i=; i<=n; i++) cin>>a[i], b[i-]=a[i];
sort(b, b+n); //error-prone
int *e=unique(b, b+n);
memset(bit, , sizeof(bit));
memset(pos, , sizeof(pos));
cin>>q;
for(int l, r, i=; i<q; i++)
{
cin>>l>>r;
p[i]={l, r, i};
}
sort(p, p+q);
for(int i=, j=, k; i<q && j<=n; )
{
for(; j<=p[i].r; j++)
{
int id=lower_bound(b, e, a[j])-b;
if(pos[id]) add(pos[id], -a[j]);
pos[id]=j, add(j, a[j]);
}
for(k=i; p[k].r==p[i].r; k++)
{
ans[p[k].id]=sum(p[k].r)-sum(p[k].l-);
}
i=k;
}
for(int i=; i<q; i++) cout<<ans[i]<<endl;
}
return ;
}
 
 

HDU #3333的更多相关文章

  1. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  2. 数值标记问题 离线+树状数组 HDU 3938 + HDU 3333

    HDU 3938 题目大意:给你一个长度为n的数组a,定义区间[l,r]的val为区间内所有不同的数值之和.现在有m个询问,每次询问一个区间,问区间的val是多少. 思路:将所有的询问按照右端点排序. ...

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

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  5. hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)

    http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others)    ...

  6. hdu 3333 树状数组+离线处理

    http://acm.hdu.edu.cn/showproblem.php?pid=3333 不错的题,想了非常久不知道怎么处理,并且答案没看懂,然后找个样例模拟下别人的代码立即懂了---以后看不懂的 ...

  7. HDU 3333 Turing Tree 线段树+离线处理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...

  8. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  9. HDU 3333 & 3874 (线段树+离线询问)

    两个题目都是求区间之内,不重复的数字之和,3333需要离散化处理................. 调试了一下午........说多了都是泪........... #include <iostr ...

  10. SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)

    题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...

随机推荐

  1. 15Mybatis_输出类型

    输出类型分为两种:1.resultType          和         2.resultMap 接下来先讲解resultType: 使用resultType进行输出映射,只有查询出来的列名和 ...

  2. 继续Wcf记录点滴

    之前说wcf以tcp协议作为通信方式的话会出现很多奇怪的bug,今天我把自己遇到的比较特殊的一个exception和解决方案列出来.主要是自己记录一下,顺便方便遇到这个问题的有缘人吧!废话不多说直接上 ...

  3. VisualStudio2013+EF6+MySql5.5环境下配置

    看院子里对EF框架和MySql的配置文章不少,但是几乎出自一篇文章的转载,而且这篇转载的文章的也比较坑爹,下面我将介绍一下我的配置过程: 第一步:安装mysql-connector-net-6.9.9 ...

  4. Download the WDK, WinDbg, and associated tools

    Download the WDK, WinDbg, and associated tools This is where you get your Windows Driver Kit (WDK) a ...

  5. [win]AD域组策略wifi自动配置

    http://wenku.baidu.com/link?url=MC950wliAZNeVUJ2M6Y1VTi5faqo7kG374fyBjW57r0qyLJkBZLg5ypiql4RFywQ8q7y ...

  6. 怎么用JS截取字符串中第一个和第二个字母间的部分?

    一.JS中用正则判断字符串是否有匹配正则的字符串部分,格式如下: /[a-zA-Z](.*?)[a-zA-Z]/.test('1a123d45678901a2') “.test”前面的部分是正则表达式 ...

  7. [转]World Wind学习总结一

    WW的纹理,DEM数据,及LOD模型 以earth为例 1. 地形数据: 默认浏览器纹理数据存放在/Cache/Earth/Images/NASA Landsat Imagery/NLT Landsa ...

  8. 《WCF服务编程第三版》知识点摘录

  9. 修改TrustedInstaller权限文件(无法删除文件)

    1.    右击需要修改的文件-属性 2.    切换到"安全"选项卡,点击"高级"按钮. 3.    切换到"所有者"选项卡 一般情况下默 ...

  10. 树莓派之web服务器搭建

    树莓派之web服务器搭建 (一)使用ufw创建防火墙 设置目的:可以完全阻止对树莓派的访问也可以用来配置通过防火墙对特点程序的访问.使用防火墙更好的保护树莓派. 准备工作 1.带有5V电源的树莓派 2 ...