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. cucumber 文件目录结构和执行顺序

    引用链接:http://www.cnblogs.com/timsheng/archive/2012/12/10/2812164.html Cucumber是Ruby世界的BDD框架,开发人员主要与两类 ...

  2. 搭建高可用mongodb集群(一)——mongodb配置主从模式

    转载自:LANCEYAN.COM 在大数据的时代,传统的关系型数据库要能更高的服务必须要解决高并发读写.海量数据高效存储.高可扩展性和高可用性这些难题.不过就是因为这些问题Nosql诞生了. NOSQ ...

  3. Android 权限的实现

    1.    权限 每个程序在安装时都有建立一个系统ID,如app_15,用以保护数据不被其它应用获取.Android根据不同的用户和组,分配不同权限,比如访问SD卡,访问网络等等.底层映射为Linux ...

  4. spark-wordcount-sample算子测试

    import org.apache.spark.{SparkConf, SparkContext} object radomSampleU { def main(args: Array[String] ...

  5. IOSAutolayout

    21:55:33前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-ip ...

  6. Django之CSRF问题

    1.csrf全称:cross site request forgery(跨站请求伪造),举例来讲,一个安全的网站A,一个恶意网站B,当你在A网站进行了登录后,这时候浏览器会保存你的cookie和ses ...

  7. 洛谷 P2910 [USACO08OPEN]寻宝之路Clear And Present Danger

    题目描述 Farmer John is on a boat seeking fabled treasure on one of the N (1 <= N <= 100) islands ...

  8. UVA - 12264 Risk (二分,网络流)

    题意比较坑,移动完以后的士兵不能再次移动,不然样例都过不了... 最小值最大满足决策单调性所以二分答案,跑网络流验证是否可行. 这种题重点在建图,为了保证只移动一次,拆点,一个入点一个出点,到了出点的 ...

  9. 讲课笔记3——浮动、margin失效的问题、默认样式重置

    EO:搜索引擎优化,一般在网页里面只写一个h1标签,搜索引擎可以通过该h1标签里面的内容搜索你所写的网页(a标签和img标签最好写上title属性)标准写法: .logo { text-decorat ...

  10. 简单css动画 fadeIn fadeOut flash

    考虑兼容性采用 -webkit- -o- -mos- -ms- @keyframes fadeIn{ 0%{ opacity: 0; display: block; } 100%{ opacity: ...