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. hydra爆破用法

    -R 根据上一次进度继续破解 -S 使用SSL协议连接 -s 指定端口 -l 指定用户名 -L 指定用户名字典(文件) -p 指定密码破解 -P 指定密码字典(文件) -e 空密码探测和指定用户密码探 ...

  2. JsonHelper

    .net下的json序列化在以前没有Newtonsoft.Json崭露头角之前采用System.Web.Script.Serialization命名空间下的JavaScriptSerializer对象 ...

  3. transition的局限

    transition的优点在于简单易用,但是它有几个很大的局限. (1)transition需要事件触发,所以没法在网页加载时自动发生. (2)transition是一次性的,不能重复发生,除非一再触 ...

  4. 如何将list转为json?

  5. 取消StringGrid的自动滚动

    StringGrid的Options的goRowSelect为false时,在点击右侧未显示完全的Cell,StringGrid会自动向左滚动,怎样设定,取消StringGrid的自动滚动啊?Delp ...

  6. Go cron定时任务的用法

    cron是什么 cron的意思就是:计划任务,说白了就是定时任务.我和系统约个时间,你在几点几分几秒或者每隔几分钟跑一个任务(job),就那么简单. cron表达式 cron表达式是一个好东西,这个东 ...

  7. [转]php返回json数据中文显示的问题

    转自 : http://blog.csdn.net/superbirds/article/details/8091910 解决方法:   <?php    function Notice(){  ...

  8. [py] 导入模块 reload(sys)

        #!/usr/bin/env python # coding: utf-8 import sys   reload(sys) #<------这个是什么意思 sys.setdefault ...

  9. word 2010自定义快捷键提高工作效率

    经常使用word处理文档, 做笔记的时候会把word文档框缩小,以便同时看pdf同时记录笔记,但是缩小的word框不能把所有的菜单项显示出来,我比较常用那个插入边框下面的那个横线来做分割符,但是缩小了 ...

  10. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...