HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2083 Accepted Submission(s): 747
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.
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.
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
7
14
1
3
6
- /*
- 题意为查找区间去重后的和
- 用树状数组离线处理
- 将所有查询以右端点从小到大排序
- 按此顺序边去重边查询
- 前面的去重就不会影响到后面的结果了
- */
#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 (树状数组 | 线段树 的离线处理)的更多相关文章
- 树状数组 && 线段树应用 -- 求逆序数
参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树
正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...
- hdu 1166 敌兵布阵——(区间和)树状数组/线段树
pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...
- HDU 1166 敌兵布阵 树状数组||线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...
- HDU 3303 Harmony Forever 前缀和+树状数组||线段树
Problem Description We believe that every inhabitant of this universe eventually will find a way to ...
- hdu 1166 树状数组(线段树)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- SQL Server高级性能调优策略
论坛里经常有人问“我的数据库很慢,有什么办法提高速度呢?”.这是个古老的话题,又是常见的问题,也是DBA们最想解决的问题之一.我想就SQLServer调优大家一起论一论,如果可以的话尽量发表自己观点, ...
- 从nib文件里加载collectionViewCell
如何取出在xib文件里绘制的collectionViewCell ? 1.获得nib文件 UINib *nib = [[UINib NibWithName:@"xib文件的名字"] ...
- 字符串和json之间的互相转化
在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法. JSON.stringify(obj)将JSO ...
- I Hate It(hdu1754)(线段树区间最大值)
I Hate It hdu1754 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- [原] XAF ListView显示隐藏Footer菜单
using System; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Win.Editors; using DevExpress ...
- AS3从入门到放弃
工作久了,在技术上肯定有自己的一些见解.一直以来都懒得写下来,总觉得尤其写博客的时间,还不如自己学一点新东西.但不能总找这样的接口啊,于是乎开始了这篇博客. 工作了三年,有一年半的时间是在做AS3,在 ...
- SQLSERVER2014 2014年4月1日发布
SQLSERVER2014 2014年4月1日发布 原文地址: http://blogs.technet.com/b/microsoft_blog/archive/2014/03/18/sql-ser ...
- SQL语句汇总(终篇)—— 表联接与联接查询
上一篇博文说到相关子查询效率低下,那我们怎么能将不同表的信息一起查询出来呢?这就需要用到表联接. 和之前的UNION组合查询不同,UNION是将不同的表组合起来,也就是纵向联接,说白了就是竖着拼起来. ...
- 【WPF】提高InkAnalyer手写汉字识别的准确率
最近项目中需要用到一个手写键盘,我们使用了WPF的InkCanvas+InkAnalyer来开发. 按照文档,一般的代码写法如下: var analyzer = new InkAnalyzer(); ...
- 一起来花5分钟写一个PHP入门Demo
最近公司招了几个应届毕业生,他们对前端的了解还挺多,但是对后端的技术一无所知,我觉得,作为一个前端攻城狮,如果你有远大的抱负,就应该雨露均沾... 今天我就跟大家讲一讲PHP最基本的入门,至少别人问起 ...