Necklace

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2083    Accepted Submission(s): 747

Problem Description
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
 
Source
 
Recommend
lcy
 
  1. /*
  2. 题意为查找区间去重后的和
  3. 用树状数组离线处理
  4. 将所有查询以右端点从小到大排序
  5. 按此顺序边去重边查询
  6. 前面的去重就不会影响到后面的结果了
  7. */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int N=;
const int M=; struct node{
int l,r;
int id;
}q[M]; int n,m,val[N],pre[N],loc[];
long long arr[N],res[M]; int lowbit(int x){
return x&(-x);
} void update(int i,int x){
while(i<=n){
arr[i]+=x;
i+=lowbit(i);
}
} long long Sum(int i){
long long ans=;
while(i>){
ans+=arr[i];
i-=lowbit(i);
}
return ans;
} bool cmp(node a,node b){
return a.r<b.r;
} int main(){ //freopen("input.txt","r",stdin); int t;
scanf("%d",&t);
while(t--){
memset(arr,,sizeof(arr));
memset(loc,-,sizeof(loc));
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&val[i]);
pre[i]=loc[val[i]];
loc[val[i]]=i;
update(i,val[i]);
}
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q+,q++m,cmp);
int r=;
for(int i=;i<=m;i++){
for(int j=r+;j<=q[i].r;j++)
if(pre[j]!=-)
update(pre[j],-val[j]);
r=q[i].r;
res[q[i].id]=Sum(q[i].r)-Sum(q[i].l-);
}
for(int i=;i<=m;i++)
printf("%I64d\n",res[i]);
}
return ;
}

线段树:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map> using namespace std; const int N=; //#define L(rt) (rt<<1)
//#define R(rt) (rt<<1|1) #define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1 struct Tree{
int l,r;
int id;
}q[N<<]; map<int,int> mp;
int n,m,a[N];
long long sum[N<<],res[N<<]; void PushUp(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
} void update(int id,int val,int l,int r,int rt){
if(l==r){
sum[rt]+=val;
return ;
}
int mid=(l+r)>>;
if(id<=mid)
update(id,val,lson);
else
update(id,val,rson);
PushUp(rt);
} long long query(int L,int R,int l,int r,int rt){
if(L<=l && R>=r)
return sum[rt];
int mid=(l+r)>>;
long long ans=;
if(L<=mid)
ans+=query(L,R,lson);
if(R>mid)
ans+=query(L,R,rson);
return ans;
} int cmp(Tree a,Tree b){
return a.r<b.r;
} int main(){ //freopen("input.txt","r",stdin); int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q+,q++m,cmp);
mp.clear();
memset(sum,,sizeof(sum));
int r=;
for(int i=;i<=m;i++){
for(int j=r+;j<=q[i].r;j++){
if(mp[a[j]])
update(mp[a[j]],-a[j],,n,);
update(j,a[j],,n,);
mp[a[j]]=j;
r=q[i].r;
}
res[q[i].id]=query(q[i].l,q[i].r,,n,);
}
for(int i=;i<=m;i++)
printf("%I64d\n",res[i]);
}
return ;
}

HDU 3874 Necklace (树状数组 | 线段树 的离线处理)的更多相关文章

  1. 树状数组 && 线段树应用 -- 求逆序数

    参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...

  2. hdu1394(枚举/树状数组/线段树单点更新&区间求和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...

  3. 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树

    正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...

  4. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  6. hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...

  7. hdu 1166 敌兵布阵——(区间和)树状数组/线段树

    pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...

  8. HDU 1166 敌兵布阵 树状数组||线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...

  9. HDU 3303 Harmony Forever 前缀和+树状数组||线段树

    Problem Description We believe that every inhabitant of this universe eventually will find a way to ...

  10. hdu 1166 树状数组(线段树)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. ubuntu 14.04 unity 管理工具 unity-tweak-tool

    安装方式: sudo apt-get update sudo apt-get install unity-tweak-tool 用于更改字体,修改状态,disable 亚马逊的搜索等 功能.很好用

  2. 嵌入式系统coredump设计

    阴沟翻船,马失前蹄,说明凡事皆有可能.自然,程序设计的再好,也会有crash的时候.开发期还还说,正式交付的系统crash自然更是难以承受的.无论何时,死一次就够了,得有方法查个水落石出. 几年前哥去 ...

  3. 【转】require.js学习笔记(二)

    require.js遵循AMD规范,通过define定义模块,require异步加载模块,一个js文件即一个模块. 一.模块加载require1.加载符合AMD规范模块 HTML: <scrip ...

  4. 【10_169】Majority Element

    今天遇到的题都挺难的,不容易有会做的. 下面是代码,等明天看看Discuss里面有没有简单的方法~ Majority Element My Submissions Question Total Acc ...

  5. UVA 12657 Boxes in a Line 双向链表

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066 利用链表换位置时间复杂度为1的优越性,同时也考虑到使用实际 ...

  6. Memcache的使用基本介绍

    Memcache学习总结2-Memcache的使用基本介绍 上一次总结中我们已经安装部署好了Memcached,并且把PHP扩展Memcache也安装好了,这一节我们详细学习一下PHP扩展Memcac ...

  7. innodb_ft_max_token_size取值范围

    根据问档中所说,innodb_ft_max_token_size取值范围为10-252,而实际上但我们在配置文件设置innodb_ft_max_token_size=252时,error log会出现 ...

  8. C#设计模式(8)——桥接模式(Bridge Pattern)

    一.引言 这里以电视遥控器的一个例子来引出桥接模式解决的问题,首先,我们每个牌子的电视机都有一个遥控器,此时我们能想到的一个设计是——把遥控器做为一个抽象类,抽象类中提供遥控器的所有实现,其他具体电视 ...

  9. jQuery插件之验证控件jquery.validate.js

    今天学习一下jQuery.Validate插件,为便于日后翻阅查看和广大博客园园友共享,特记于此. 本博客转载自:jQuery Validate jQuery Validate 插件为表单提供了强大的 ...

  10. 如何设置iframe高度自适应,在跨域的情况下能做到吗?

    在页面上使用iframe来动态加载页面内容是网页开发中比较常见的方法.在父页面中给定一个不带滚动条的iframe,然后对属性src指定一个可加载的页面,这样当父页面被访问的时候,子页面可以被自动加载. ...