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 ...
随机推荐
- CF337D-Book of Evil
题目 一棵树上有一个古籍,这个古籍可以影响到与它距离为 \(d\) 以内的点.现在给出被影响到的点,问古籍可能在多少个点上. \(0\le m,d\le n\le 10^5\). 分析 原问题不好做, ...
- 【bzoj1495】[NOI2006]网络收费 暴力+树形背包dp
题目描述 给出一个有 $2^n$ 个叶子节点的完全二叉树.每个叶子节点可以选择黑白两种颜色. 对于每个非叶子节点左子树中的叶子节点 $i$ 和右子树中的叶子节点 $j$ :如果 $i$ 和 $j$ 的 ...
- BZOJ3598 SCOI2014方伯伯的商场之旅(数位dp)
看到数据范围就可以猜到数位dp了.显然对于一个数最后移到的位置应该是其中位数.于是考虑枚举移到的位置,那么设其左边和为l,左右边和为r,该位置数为p,则需要满足l+p>=r且r+p>=l. ...
- castle activerecord 学习过程出现的问题
优点: 1.CRUD:代码简洁 2.不用配置map 3.自带事务方便 4.自带IOC 5.自带 数据有效性验证 缺点: 1.自增长(Oracle 一直提示序号不存在,有空继续尝试) 2.多条件,直接用 ...
- Spring Boot系列教程三:使用devtools实现热部署
一.前言 Eclipse下使用spring-tool-suite插件创建一个spring boot 工程,通过右键“Run As”--->"Spring Boot App"来 ...
- jsp中文乱码终极解决方法
转载http://blog.csdn.net/csh624366188/article/details/6657350 一 找出问题的根源 乱码可能出现的地方:1 jsp页面中 ...
- ORACLE 中如何截取到时间的年月日中的年、月、日
在Oracle中,要获得日期中的年份,例如把sysdate中的年份取出来,并不是一件难事.常用的方法是:Select to_number(to_char(sysdate,'yyyy')) from d ...
- ICPC 2018 南京网络赛 J Magical Girl Haze(多层图最短路)
传送门:https://nanti.jisuanke.com/t/A1958 题意:n个点m条边的路,你有k次机会将某条路上的边权变为0,问你最短路径长度 题解:最短路变形,我们需要在常规的最短路上多 ...
- UVA-10375 数学
UVA-10375 题意 : 输入p,q,r,s,求C(p,q)/C(r,s). p,q,r,s<=10000:结果不超过1e8 代码: //显然不能直接计算,考虑每个数都可以由若干个素数乘积得 ...
- 题解 UVA1184 【Air Raid】
有向无环图(DAG)的最小路径覆盖的模板题. 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 由题意可得这是一个有向图,而路径不能相交,于是我们知道这是无向图的不相交最小路径覆盖问 ...