SPOJ DQUERY D-query (在线主席树/ 离线树状数组)
版权声明:本文为博主原创文章,未经博主允许不得转载。
题意:
给出一串数,询问[L,R]区间中有多少个不同的数 。
解法:
关键是查询到某个右端点时,使其左边出现过的数都记录在它们出现的最右位置置1,其他位置置0,然后直接统计[L,R]的区间和就行了。
在线和离线都可以做 。
话不多说,上代码 。
在线主席树
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#define ll long long using namespace std; const int N=+;
const int M=+; int Ls[N*],Rs[N*],sum[N*],root[N];
int tot=; int pos[M];
int a[N];
int n,q; inline void copy(int x,int y){
Ls[x]=Ls[y];
Rs[x]=Rs[y];
sum[x]=sum[y];
} inline int bulidtree(int L,int R){
if (L>R) return ;
int k=tot++;
sum[k]=;
if (L==R) return k;
int mid=(L+R)>>;
Ls[k]=bulidtree(L,mid);
Rs[k]=bulidtree(mid+,R);
return k;
} inline int update(int o,int p,int v,int L,int R){
int k=tot++;
copy(k,o);
sum[k]+=v; if (L==R) return k; int mid=(L+R)>>;
if (p<=mid) Ls[k]=update(Ls[k],p,v,L,mid);
else Rs[k]=update(Rs[k],p,v,mid+,R); return k;
} inline int query(int o,int x,int y,int L,int R){
if (L==x && R==y) return sum[o];
int mid=(L+R)>>;
if (y<=mid) return query(Ls[o],x,y,L,mid);
else if (x>mid) return query(Rs[o],x,y,mid+,R);
else return query(Ls[o],x,mid,L,mid)+query(Rs[o],mid+,y,mid+,R);
} int main(){
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",a+i); root[]=bulidtree(,n); memset(pos,-,sizeof(pos));
for (int i=;i<=n;i++){
root[i]=root[i-];
if (~pos[a[i]])
root[i]=update(root[i],pos[a[i]],-,,n);
root[i]=update(root[i],i,,,n);
pos[a[i]]=i;
} scanf("%d",&q);
int x,y;
while (q--){
scanf("%d %d",&x, &y);
printf("%d\n",query(root[y],x,y,,n));
} return ;
}
离线树状数组
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#define ll long long using namespace std; const int N=;
const int Q=;
const int M=; struct query{
int L,R;
int id;
bool operator < (const query & t) const {
return R<t.R;
}
}q[Q]; int a[N];
int pos[M]={};
int ans[Q]; int c[N]; // 树状数组
int n; inline int lowbit(int x){
return x&(-x);
} inline void add(int x,int d){
while (x<=n) {
c[x]+=d;
x+=lowbit(x);
}
} inline int sum(int x){
int ret=;
while (x){
ret+=c[x];
x-=lowbit(x);
}
return ret;
} int main(){
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",a+i); int m;
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); int j=;
for (int i=;i<=n;i++){
if (pos[a[i]]) add(pos[a[i]],-);
add(i,);
pos[a[i]]=i; while (j<=m && q[j].R==i){
ans[q[j].id]=sum(q[j].R)-sum(q[j].L-);
j++;
}
} for (int i=;i<=m;i++) printf("%d\n",ans[i]); return ;
}
SPOJ DQUERY D-query (在线主席树/ 离线树状数组)的更多相关文章
- SPOJ 3267 D-query(离散化+在线主席树 | 离线树状数组)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)
DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...
- hdu 4605 Magic Ball Game (在线主席树/离线树状数组)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4605 题意: 有一颗树,根节点为1,每一个节点要么有两个子节点,要么没有,每个节点都有一个权值wi .然后,有一个球,附带值x . 球 ...
- HDU 5869 Different GCD Subarray Query rmq+离线+数状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5869 Different GCD Subarray Query Time Limit: 6000/3 ...
- bzoj 2434: 阿狸的打字机 fail树+离线树状数组
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=2434 题解: 首先我们可以发现这个打字的过程本身就是在Trie上滚来滚去的过程 所以我们 ...
- 【SPOJ】375. Query on a tree(树链剖分)
http://www.spoj.com/problems/QTREE/ 这是按边分类的. 调试调到吐,对拍都查不出来,后来改了下造数据的,拍出来了.囧啊啊啊啊啊啊 时间都花在调试上了,打hld只用了半 ...
- Codeforces Round #345 (Div. 1) D - Zip-line 带单点修改的LIS 主席树 | 离线树状数组
D - Zip-line #include<bits/stdc++.h> #define LL long long #define fi first #define se second # ...
- BZOJ2120:数颜色(数状数组套主席树)(带修改的莫对)
墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...
- 5.15 牛客挑战赛40 E 小V和gcd树 树链剖分 主席树 树状数组 根号分治
LINK:小V和gcd树 时限是8s 所以当时好多nq的暴力都能跑过. 考虑每次询问暴力 跳父亲 这样是nq的 4e8左右 随便过. 不过每次跳到某个点的时候需要得到边权 如果直接暴力gcd的话 nq ...
随机推荐
- 【JavaScript&jQuery】购物车自动结算
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- BZOJ3745 COCI2015Norma(分治)
完全想不到地,考虑分治. 对区间[l,r],将左端点x由mid不断左移,右边记录最右的p满足max[mid+1,p]<=max[x,mid],q满足min[mid+1,q]>=min[x, ...
- C++解析(21):四个操作符
0.目录 1.逻辑操作符的陷阱 2.逗号操作符的分析 3.前置操作符和后置操作符 4.小结 1.逻辑操作符的陷阱 逻辑运算符的原生语义: 操作数只有两种值(true和false) 逻辑表达式不用完全计 ...
- 【CF813E】Army Creation(主席树)
[CF813E]Army Creation(主席树) 题面 CF 洛谷 翻译 by ppl 见洛谷 题解 考虑最多只会有\(K\)个相同的数 那么,也就是说,如果一个数会被选 那么,和它相等的数中,在 ...
- Zookeeper(一) zookeeper基础使用
一.Zookeeper是什么 (安装的是3.4.7) ZooKeeper 是一个分布式的,开放源码的分布式应用程序协调服务,是 Google 的 Chubby 一个开源的实现.它提供了简单原始的功能, ...
- 框架----Django之Form提交验证(二)
一.Form提交验证之(学生表.老师表.班级表)的添加和编辑实现案例 1. 浏览器访问 http://127.0.0.1:8000/student_list/ http://127.0.0.1:800 ...
- iframe在ios上不能滚动问题解决
同这篇文章一样,用到了-webkit-overflow-scrolling: touch;属性.主要解决方案是在iframe外层添加一个div,然后设置-webkit-overflow-scrolli ...
- 【目标检测】Faster RCNN算法详解
Ren, Shaoqing, et al. “Faster R-CNN: Towards real-time object detection with region proposal network ...
- 在Eclipse中开发使用Spring IOC的JUnit/TestNG测试用例之详解
转载自:http://blog.csdn.net/radic_feng/article/details/6740438 我们期望能像在产品代码中一样,在测试用例中使用的bean也由Spring Con ...
- sqlserver 2008连接
初次安装使用SQL server 2008时,可能会遇到无法连接到(local)的情况.那么,如何解决此问题? 工具/原料 SQL server 2008 方法/步骤 1 打开SQL se ...