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 (可持久化线段树,区间重复元素个数)的更多相关文章

  1. 计蒜客 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 ...

  2. 【bzoj2653】middle 可持久化线段树区间合并

    题目描述 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个长度为n的序列s.回答Q个这样的询问:s的左端点在[a,b]之间,右端点在[ ...

  3. hdu4348 - To the moon 可持久化线段树 区间修改 离线处理

    法一:暴力! 让干什么就干什么,那么久需要可持久化线段树了. 但是空间好紧.怎么破? 不down标记好了! 每个点维护sum和add两个信息,sum是这段真实的和,add是这段整体加了多少,如果这段区 ...

  4. 可持久化线段树——区间更新hdu4348

    和线段树类似,每个结点也要打lazy标记 但是lazy标记和线段树不一样 具体区别在于可持久化后lazy-tag不用往下传递,而是固定在这个区间并不断累加,变成了这个区间固有的性质(有点像分块的标记了 ...

  5. SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)

    题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...

  6. 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 ...

  7. BZOJ5371[Pkusc2018]星际穿越——可持久化线段树+DP

    题目描述 有n个星球,它们的编号是1到n,它们坐落在同一个星系内,这个星系可以抽象为一条数轴,每个星球都是数轴上的一个点, 特别地,编号为i的星球的坐标是i. 一开始,由于科技上的原因,这n个星球的居 ...

  8. bzoj 3524 可持久化线段树

    我们可以先离散化,然后建立权值的可持久化线段树,记录每个数出现的次数,对于区间询问直接判断左右儿子的cnt是不是大于(r-k+1)/2,然后递归到最后一层要是还是大于就有,否则不存在. 反思:挺简单一 ...

  9. SPOJ Meteors - 可持久化线段树 - 二分法

    Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The plan ...

随机推荐

  1. 如何移除EditText自动焦点

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_cont ...

  2. Java中的流(4)InputStream,InputStreamReader,BufferedReader关系

    InputStream是字节流,InputStreamReader将字节流转成字符流,BufferedReader将字符流转成字符缓冲,开始读字符. 1.InputStream.OutputStrea ...

  3. java中什么包不需要导入

    java中Math.random()*10;在math包中不需要导入: 即import java.lang.Math; 即lang下的所有包都不需要导入.

  4. 关于线程间操作无效: 从不是创建控件“xx”的线程访问它,错误解决方法(自定义委托和系统委托Action或Func解决)

    这是一个线程不安全的问题.跨线程操作问题. 比如我们需要在线程中改变textbox的文本,textbox的name是txtShowMsg 第一种方法(不推荐使用) 在窗体构造函数中写Control.C ...

  5. 微信小程序组件解读和分析:十二、picker滚动选择器

    picker滚动选择器组件说明: picker: 滚动选择器,现支持三种选择器,通过mode属性来区分, 分别是普通选择器(mode = selector),时间选择器(mode = time),日期 ...

  6. iOS----时间日期处理

    时间日期处理 1.NSDateFormatter 日期格式化 ①可以把NSString 类型转为 NSDate类型 举例 把 "2015-08-23 19:46:14" 转为NSD ...

  7. 泛型术语:占位类型placeholder

    Here’s a generic version of the same code: struct Stack<Element> { var items = [Element]() mut ...

  8. CREATE USER - 创建一个新的数据库用户帐户

    SYNOPSIS CREATE USER name [ [ WITH ] option [ ... ] ] where option can be: SYSID uid | [ ENCRYPTED | ...

  9. js 或jquery定义方法时,参数不固定是怎么实现的

    //①不定义接受参数的方式来接受参数(arguments) function getparams(){ //利用arguments来接受参数,arguments表示参数集合, //里面存放的调用这个方 ...

  10. PHP 中 include() 与 require() 的区别说明

    引用文件的方法有两种:require 及 include.两种方式提供不同的使用弹性. require 的使用方法如 require("MyRequireFile.php"); . ...