D. Necklace

Time Limit: 5000ms
Memory Limit: 32768KB

64-bit integer IO format: %I64d      Java class name: Main

 
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

解题:离线树状数组,为什么要把y坐标从小到大进行排序呢?因为树状数组的特性所致,右边的节点可能包含左边的节点的值,所以从左往右,不断去掉重复的数值更简便。

离线处理即先一次性把所有询问存储起来。最后一次性回复。用一个数组pre[i]记录元素d[i]距离i最近的一次出现的下标。pre[i] == -1即表示该元素d[i]目前是唯一的,不存在重复元素的。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
struct query {
int x,y,id;
} q[maxn];
LL tree[];
int pre[],loc[],n,m,d[];
LL ans[maxn];
bool cmp(const query &a,const query &b) {
return a.y < b.y;
}
int lowbit(int x) {
return x&(-x);
}
void update(int x,int val) {
for(; x <= n; x += lowbit(x)) {
tree[x] += val;
}
}
LL sum(int x) {
LL ans = ;
for(; x; x -= lowbit(x))
ans += tree[x];
return ans;
}
int main() {
int t,i,j;
scanf("%d",&t);
while(t--) {
memset(loc,-,sizeof(loc));
memset(tree,,sizeof(tree));
scanf("%d",&n);
for(i = ; i <= n; i++) {
scanf("%d",d+i);
pre[i] = loc[d[i]];
loc[d[i]] = i;
update(i,d[i]);
}
scanf("%d",&m);
for(i = ; i <= m; i++) {
scanf("%d %d",&q[i].x,&q[i].y);
q[i].id = i;
}
sort(q+,q+m+,cmp);
int y = ;
for(i = ; i <= m; i++) {
for(j = y+; j <= q[i].y; j++) {
if(pre[j] != -) update(pre[j],-d[j]);
}
y = q[i].y;
ans[q[i].id] = sum(q[i].y) - sum(q[i].x-);
}
for(i = ; i <= m; i++) {
printf("%I64d\n",ans[i]);
}
}
return ;
}

xtu数据结构 D. Necklace的更多相关文章

  1. xtu数据结构 H. City Horizon

    H. City Horizon Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java cl ...

  2. xtu数据结构 I. A Simple Tree Problem

    I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld     ...

  3. xtu数据结构 G. Count the Colors

    G. Count the Colors Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Jav ...

  4. xtu数据结构 B. Get Many Persimmon Trees

    B. Get Many Persimmon Trees Time Limit: 1000ms Memory Limit: 30000KB 64-bit integer IO format: %lld  ...

  5. xtu数据结构 C. Ultra-QuickSort

    C. Ultra-QuickSort Time Limit: 7000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java ...

  6. 数据结构(主席树,Bit):XTU 1247/COGS 2344. pair-pair

    pair-pair 输入文件:pair-pair.in   输出文件:pair-pair.out   简单对比 时间限制:7 s   内存限制:64 MB Time Limit : 7000 MS M ...

  7. CH1807 Necklace【Hash】【字符串】【最小表示法】

    1807 Necklace 0x18「基本数据结构」练习 背景 有一天,袁☆同学绵了一条价值连城宝石项链,但是,一个严重的问题是,他竟然忘记了项链的主人是谁!在得知此事后,很多人向☆同学发来了很多邮件 ...

  8. 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)

    前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...

  9. 一起学 Java(三) 集合框架、数据结构、泛型

    一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...

随机推荐

  1. 操作messageBox类

    我们经常操作messagebox类,有时候我们又分不清一些参数,下面是一些操作messageBox的常用方法: public static class ClsMsg { public static v ...

  2. java数据结构和算法06(红黑树)

    这一篇我们来看看红黑树,首先说一下我啃红黑树的一点想法,刚开始的时候比较蒙,what?这到底是什么鬼啊?还有这种操作?有好久的时间我都缓不过来,直到我玩了两把王者之后回头一看,好像有点儿意思,所以有的 ...

  3. 1102 采药 2005年NOIP全国联赛普及组

    1102 采药 2005年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 辰辰是个天资聪颖的孩子 ...

  4. JSP界面设置提示浮动框

    1.公共js <script type="text/javascript"> var tip={ $:function(ele){ if(typeof(ele)==&q ...

  5. Android WiFi使用记录

    最近在做Android的WiFi部分的开发,连接的工具类参照了这个文章的工具类. http://www.cnblogs.com/zhuqiang/p/3566686.html 开发中碰上的一些问题,在 ...

  6. EJB配置jboss数据源

    1.数据源的模板在\jboss-4.2.3.GA-jdk6\jboss-4.2.3.GA\docs\examples\jca\下2.编辑数据源文件,比如mysql-ds.xml,命名规则是名称-ds. ...

  7. ajax传给springMVC数据编码集问题

    前台 ajax: $.ajax("${pageContext.request.contextPath}/hello",// 发送请求的URL字符串. { dataType : &q ...

  8. 简单的 创建AJax的方法

    // 简单的ajax对象 var myAjax = { // XMLHttpRequest IE7+, Firefox, Chrome, Opera, Safari : ActiveXObject I ...

  9. oracle调用子存储过程+游标循环实例

    一,有子节点的部门的子节点的排序,调用子存储过程 CREATE OR REPLACE PROCEDURE "PRO_INIT_SORT" AS CURSOR cur_departm ...

  10. 什么是二维数组?二维遍历?Java二维数组制作图片迷宫 使用如鹏游戏引擎制作窗口界面 附带压缩包下载,解压后双击start.bat启动

    什么是二维数组? 数组当中放的还是数组 int [][] arr=new int[3][2]; 有3个小箱子,每个箱子2个格子. 看结果? int [][] arr=new int[3][2]; Sy ...