SPOJ 3267 D-query (可持久化线段树,区间重复元素个数)
D-query
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj.
Input
- Line 1: n (1 ≤ n ≤ 30000).
- Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 106).
- Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
- In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).
Output
- For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, ..., aj in a single line.
Example
Input
5
1 1 2 1 3
3
1 5
2 4
3 5 Output
3
2
3 题意:求区间内不重复的数的个数。 n,m<=100000
题解:建立可持久化线段树,以右端点为最后建立现在版本线段树,
然后就是维护每一棵线段树,就是前面的点什么时候失效,询问大区间就一定会包含小区间中的
相同权值的点,然后只需要记录和即可,离散化还是需要的+二分。
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdio> #define N 60007
#define M 20000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if (ch=='-') f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,top,sz,q;
int a[N],b[N],num[N],pos[N],root[N];
int ls[M],rs[M],sum[M]; int bs(int num)
{
int l=,r=top;
while(l<=r)
{
int mid=(l+r)>>;
if (b[mid]==num) return mid;
if (b[mid]<num) l=mid+;
else r=mid-;
}
}
void change(int l,int r,int x,int &y,int wei,int z)
{
y=++sz;
if (l==r)
{
sum[y]=sum[x]+z;
return;
}
ls[y]=ls[x],rs[y]=rs[x],sum[y]=sum[x]+z;
int mid=(l+r)>>;
if (wei<=mid) change(l,mid,ls[x],ls[y],wei,z);
else change(mid+,r,rs[x],rs[y],wei,z);
} int query(int p,int l,int r,int x,int y)
{
if (l==x&&y==r) return sum[p];
int mid=(l+r)>>;
if (y<=mid) return query(ls[p],l,mid,x,y);
else if (x>mid) return query(rs[p],mid+,r,x,y);
else return query(ls[p],l,mid,x,mid)+query(rs[p],mid+,r,mid+,y);
}
int main()
{
int n=read();
for (int i=;i<=n;i++)
a[i]=read(),b[i]=a[i];
sort(b+,b+n+);
top=;
for (int i=;i<=n;i++)
if (b[i]!=b[i-]) b[++top]=b[i];
for (int i=;i<=n;i++)
{
int num=bs(a[i]);
change(,n,root[i-],root[i],i,);
if (pos[num]) change(,n,root[i],root[i],pos[num],-);
pos[num]=i;
}
q=read();
while(q--)
{
int l=read(),r=read();
printf("%d\n",query(root[r],,n,l,r));
}
}
SPOJ 3267 D-query (可持久化线段树,区间重复元素个数)的更多相关文章
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- 【bzoj2653】middle 可持久化线段树区间合并
题目描述 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个长度为n的序列s.回答Q个这样的询问:s的左端点在[a,b]之间,右端点在[ ...
- hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
法一:暴力! 让干什么就干什么,那么久需要可持久化线段树了. 但是空间好紧.怎么破? 不down标记好了! 每个点维护sum和add两个信息,sum是这段真实的和,add是这段整体加了多少,如果这段区 ...
- 可持久化线段树——区间更新hdu4348
和线段树类似,每个结点也要打lazy标记 但是lazy标记和线段树不一样 具体区别在于可持久化后lazy-tag不用往下传递,而是固定在这个区间并不断累加,变成了这个区间固有的性质(有点像分块的标记了 ...
- SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)
题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...
- HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- BZOJ5371[Pkusc2018]星际穿越——可持久化线段树+DP
题目描述 有n个星球,它们的编号是1到n,它们坐落在同一个星系内,这个星系可以抽象为一条数轴,每个星球都是数轴上的一个点, 特别地,编号为i的星球的坐标是i. 一开始,由于科技上的原因,这n个星球的居 ...
- bzoj 3524 可持久化线段树
我们可以先离散化,然后建立权值的可持久化线段树,记录每个数出现的次数,对于区间询问直接判断左右儿子的cnt是不是大于(r-k+1)/2,然后递归到最后一层要是还是大于就有,否则不存在. 反思:挺简单一 ...
- SPOJ Meteors - 可持久化线段树 - 二分法
Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The plan ...
随机推荐
- vue-router之 beforeRouteEnter
beforeRouteEnter在每次路由切换都执行 ,而项目优化后,切换路由mounted只在最开始执行一次 beforeRouteEnter的具体用法可参考官方文档 https://cn.vuej ...
- 实现通知栏Notification
课程Demo public class MainActivity extends Activity implements OnClickListener{ NotificationManager ma ...
- Objective-C Properties
Objective-C Properties Apple introduced properties, a combination of new compiler directivesand a ne ...
- 解决jquery与其他库的冲突
1.jquery在其他库之后导入 第一种: jQuery.noConflict();//将变量$的控制权限交给其他类库,即将$的控制权让渡给其他类库 jQuery(function(){ jQuery ...
- 【PostgreSQL-9.6.3】表空间
在PostgreSQL中,表空间实际上是为表指定一个存储目录,这样方便我们把不同的表放在不同的存储介质或者文件系统中.在创建数据库.表.索引时都可以指定表空间. 1. 创建表空间 --表空间目录必须是 ...
- HTML5——动画延迟的另外一种方式
https://www.cnblogs.com/hhhhhh/p/5758167.html
- vba,excel,网址提取名字与链接url
'宏操作 Sub 复制超级链接() '这里控制读取A列的第1到10行,你根据自已的要求修改一下起始和结束行数 ).Hyperlinks.Count > ).Value = Cells(a, ). ...
- C++ 继承/派生、访问属性、构造函数
1.子类继承父类的继承方式:public,private,protected,不写则默认为private: 2.子类会继承父类的全部成员(除了构造函数.析构函数,虽然析构函数有virtual,但是不是 ...
- MySQL(MMM架构使用)
本案例要求基于普通版的MySQL服务器改造MMM架构,完成以下任务操作:启动MMM集群架构设置集群中服务器为online状态MySQL-MMM架构部署完成后需要启动,数据库端启动mmm-agent进程 ...
- jQuery 点击查看 收起
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...