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. Tomcat查看用户名密码

    在非安装版的tomcat中,可以在{解压路径}/conf/tomcat_users.xml 配置文件中找到,也可以自己添加新的用户

  2. WebFrom模拟MVC

    如:  aspx前台     这样写生成页面时不会产生新的html标签,用控件则会产生新的html标签 <h1><%= title %></h1> <p> ...

  3. sprig——jar包

    Struts. Hibernate.Spring这类的框架给我们开发带来非常大的好处,让我们更加快速.有效的开发.所以我们在开发中通常都会用到各种框架,每个框架 都有很多jar包,每个jar都有各自不 ...

  4. mysql 自己定义存储过程和触发器

    mysql 自己定义存储过程和触发器 --存储过程示范 DROP PROCEDURE IF EXISTS PRO_TEST; CREATE PROCEDURE PRO_TEST(IN NUM_IN I ...

  5. kaggle之泰坦尼克的沉没

    Titanic 沉没 参见:https://github.com/lijingpeng/kaggle 这是一个分类任务,特征包含离散特征和连续特征,数据如下:Kaggle地址.目标是根据数据特征预测一 ...

  6. 不同浏览器对URL最大长度的限制(转)

    1.今天碰到一个bug,window.open后面的页面,接收参数不全,导致后台报错.实验了一下.发现是使用get方法请求服务器时,URL过长所致 微软官方的说明: http://support.mi ...

  7. WinForm的TreeView实现Win7 Areo效果

    新建一个继承自TreeView的控件类,代码如下: using System; using System.Windows.Forms; using System.Drawing; using Syst ...

  8. .net 地址栏传中文乱码 的解决方法

    1.设置web.config文件. <system.web> ...... <globalization requestEncoding="gb2312" res ...

  9. js-String

    1.一个字符串可以使用单引号或双引号 2.查找 字符串使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置 如果没找到对应的字符函数返回-1 lastIndexOf() 方法在字符串 ...

  10. dispatch_group_t

    最近在写的模块有这样一个问题,要保证所有block里面的东西全都回来之后再执行某一个 例如我要做完所有的数据库操作再刷新界面,数据库的内容很多,所有用到了group  dispatch_group_t ...