Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count it once. We define the beautiful value of some interval [x,y] as F(x,y). F(x,y) is calculated as the sum of the beautiful value from the xth ball to the yth ball and the same value is ONLY COUNTED ONCE. For example, if the necklace is 1 1 1 2 3 1, we have F(1,3)=1, F(2,4)=3, F(2,6)=6. 
Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them.
 

Input

The first line is T(T<=10), representing the number of test cases.    For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
 

Output

For each query, output a line contains an integer number, representing the result of the query.
 

Sample Input

2
6
1 2 3 4 3 5
3
1 2
3 5
2 6
6
1 1 1 2 3 5
3
1 1
2 4
3 5
 

Sample Output

3
7
14
1
3
6
 
题意:查询一段连续区间内不同整数的和。
解析:刚开始拿到这题没有想法,后来看了别人的题解才知道可以先对查询区间的右端点从小到大排序然后再处理。从上次更新停止的位置(上一个的右端点)一直更新到这次的右端点。如果某个数之前出现过,则add(pre,-val)(减掉这个数),再在当前位置插入这个数,则可以起到去重的作用。
代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=;
const double eps=0.00000001;
typedef __int64 LL;
int N;
LL A[],elem[],ans[];
int lowbit(int x){ return x&-x; }
LL sum(int id) bit树
{
LL ret=;
while(id>){ ret+=elem[id]; id-=lowbit(id); }
return ret;
}
void add(int id,LL val)
{
while(id<=N){ elem[id]+=val; id+=lowbit(id); }
}
int sign[];
struct node
{
int le,ri,id;
bool operator < (const node& t) const { return ri<t.ri; } //对右端点排序
}qes[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
memset(elem,,sizeof(elem));
for(int i=;i<=N;i++)
{
scanf("%I64d",&A[i]);
sign[A[i]]=;
}
int M;
cin>>M;
for(int i=;i<=M;i++) scanf("%d%d",&qes[i].le,&qes[i].ri),qes[i].id=i; //输入
sort(qes+,qes++M);
int pos=;
for(int i=;i<=M;i++)
{
while(pos<=qes[i].ri) //更新
{
if(sign[A[pos]]) add(sign[A[pos]],-A[pos]); //之前出现过
sign[A[pos]]=pos; //更新到当前位置
add(pos,A[pos]); //插入
pos++;
}
ans[qes[i].id]=sum(qes[i].ri)-sum(qes[i].le-); //得到答案
}
for(int i=;i<=M;i++) printf("%I64d\n",ans[i]);
}
return ;
}
 
 

hdu 3874 Necklace(bit树+事先对查询区间右端点排序)的更多相关文章

  1. HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

    Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  2. HDU - 3874 Necklace (线段树 + 离线处理)

    欢迎參加--每周六晚的BestCoder(有米! ) Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/3 ...

  3. Necklace HDU - 3874 (线段树/树状数组 + 离线处理)

    Necklace HDU - 3874  Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...

  4. HDU 3874 Necklace 区间查询的离线操作

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=3874 对需要查询的区间按右端点排序,然后从左到右依次加入序列中的元素,同时更新,更新的方法是,把上一次出 ...

  5. 洛谷1972 HH的项链 树状数组查询区间内不同的数的数量

    题目链接:https://www.luogu.com.cn/problem/P1972 题意大致是:给定一个序列长度为n,给出m个查询区间,要求响应是区间内不同的数的个数.为此我们考虑到树状数组的区间 ...

  6. HDU - 3874 Necklace (树状数组、离线处理)

    题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...

  7. HDU 3874 Necklace 树状数组

    题意:求区间内不同的数的和 离线处理,按查询右端点从小到大排序,从左往右扫一遍. 记录每个数出现的上一个位置,如果该数之前没有出现过,就加上,否则就在上一个位置减去. #include <cst ...

  8. hdu 3874 Necklace(线段树)

    这道题目和我之前做过的一道3xian大牛出的题目很像,不过总的来说还是要简单一点儿. 计算区间内的值的时候如果两个值相等,只能计算其中一个. 这道题需要将所有的问题输入之后再计算,首先,对所有问题的右 ...

  9. HDU 3874 离线段树

    在所有数字的统计范围,,对于重复统计只有一次 离线段树算法 排序终点坐标.然后再扫,反复交锋.把之前插入树行被删除 #include "stdio.h" #include &quo ...

随机推荐

  1. c++ 14

    一.堆栈(stack) stack -> vector/deque/list push  -> push_back pop   -> pop_back top   -> bac ...

  2. Web UI 网站用户界面设计命名规范

    Web UI 网站用户界面设计命名规范 WEB UI设计命名规范,也就是网站用户界面设计(网页设计)命名规范. 这套规范并非单纯的CSS.html或JavaScript命名规范,它涉及了很多使用Pho ...

  3. <转载>linux gcc编译器中使用gdb单步调试程序,程序不是顺序执行的。

    原文地址http://blog.csdn.net/abc78400123/article/details/6779108 在用gdb调试,使用s 或n单步执行程序时,发现程序不是按顺序运行的,有时莫名 ...

  4. 自定义一个searchBar

    #import "CZSearchBar.h" @implementation CZSearchBar - (instancetype)initWithFrame:(CGRect) ...

  5. 人物角色群体攻击判定(三)Physics.OverlapSphere(群体攻击)

    使用Physics.OverlapSphere来检测不方便调试, 其他都可以.   核心代码: //检测敌人 public void CheckEnemy() { Collider[] cols = ...

  6. python学习之路-13

    SQLAlchemy ORM框架 连表操作 一对多 创建表 指定约束 ForeignKey from sqlalchemy import create_engine from sqlalchemy.e ...

  7. SqlDependency不起作用

    今天使用SqlDependency,结果不起作用,失效,不管数据库怎么修改,这边都没反应,OnChange事件总是不执行,很奇怪.我打开msdn里的例子,代码复制出来,结果没问题,能执行,那剩下来的问 ...

  8. hdu 4427 Math Magic

    一个长了一张数学脸的dp!!dp[ i ][ s ][ t ] 表示第 i 个数,sum为 s ,lcm下标为 t 时的个数.显然,一个数的因子的lcm还是这个数的因子,所以我们的第三维用因子下标代替 ...

  9. Zabbix使用外部邮箱服务器发送邮件报警

    本来是想自己写一篇文章的,但是看到发现网上有写的不错的,于是乎又抄别人的文章,作为记录. 使用外部邮箱来发生邮件明显好处就是防止其他邮箱服务器当垃圾邮件处理,另一方面能降低收邮件延迟. 下面开始进行使 ...

  10. 检测浏览器是否支持AJAX

    <script type="text/javascript"> function ajaxFunction() { var xmlHttp; try { // Fire ...