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. AsyncTask 与 对话框显示 view.WindowManager$BadTokenException: Unable to add window…is not valid; is your a

    最近遇到一个奇葩的问题,好郁闷 之前也没有仔细看.问题偶尔出现一次.再去查看日志时,出现 view.WindowManager$BadTokenException: Unable to add win ...

  2. 【转】基于Qt, TUIO和TSLIB的嵌入式Linux下的多点触摸设计

    这个教程描述了在嵌入式linux下使用Qt如何设置一个支持多点触摸和单点触摸的输入系统.这里假定你已经有了对应的驱动程序,驱动可以从触摸屏的厂商那里获得或者使用一个linux 内核源码中已经存在的驱动 ...

  3. Linux 通过HTTP进行域名更新

    一.3322动态域名更新接口 接口地址 API URL http://members.3322.net/dyndns/update HTTP请求 GET /dyndns/update?hostname ...

  4. nonatomic,assign,copy,retain的区别

    nonatomic:非原子性访问,不加同步,多线程并发访问会提高性能.如果不加此属性,则默认是两个访问方法都为原子型事务访问.                    (atomic是Objc使用的一种 ...

  5. XSS第二节,XSS左邻右舍

    昨天的文章中引用了OWASP2013年的江湖排名,今天来看一下TOP中XSS的左邻右舍都是谁,先看一下他们的大名,再进一步介绍 [以下主要翻译自https://www.owasp.org/index. ...

  6. 概率dp ZOJ 3640

    Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...

  7. Rss 的作用 及使用方法

    也可以参考http://jingyan.baidu.com/article/e73e26c0c73e1f24adb6a70f.html 什么是RSS RSS是站点用来和其他站点之间共享内容的一种简易方 ...

  8. My way to Python - Day03

    列表和字典的赋值 dict1 = {} dict1['k1'] = 'v1' list1 = [] list1.append('v1') 集合系列 1,计数器 Python 2.7.6 (defaul ...

  9. linq和lamda表达式中添加时间判断时解决方案

    在工作中遇到个问题,在使用lamda查询数据的时候,需要添加一个时间判断, DateTime.AddDays(3) > e.ExpirationDate 例如:list = Context.Vo ...

  10. SQL Server中带事务的存储过程简单举例

    先来看一个概念: 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完整地执行,要么完全地不执行.那么在存储过程里添加事务,则可以保证该事务里的所 ...