题目描述

English VietnameseGiven a sequence of n numbers a _{1}1​ , a _{2}2​ , ..., a _{n}n​ 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 a _{i}i​ , a _{i+1}i+1​ , ..., a _{j}j​.

输入输出格式

输入格式:

  • Line 1: n (1 ≤ n ≤ 30000).
  • Line 2: n numbers a _{1}1​ , a _{2}2​ , ..., a _{n}n​ (1 ≤ a _{i}i​ ≤ 10 ^{6}6 ).
  • 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).

输出格式:

  • For each d-query (i, j), print the number of distinct elements in the subsequence a _{i}i​ , a _{i+1}i+1​ , ..., a _{j}j​ in a single line.

输入输出样例

输入样例#1:

5
1 1 2 1 3
3
1 5
2 4
3 5
输出样例#1:

3
2
3 离线扫描经典题,但是我复习一下主席树233
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=200005;
struct node{
int tag;
node *lc,*rc;
}nil[maxn*53],*rot[30005],*cnt;
int n,Q,a[30005],le,ri,pre[maxn*5]; node *update(node *u,int l,int r){
node *ret=++cnt;
*ret=*u;
if(l>=le&&r<=ri){
ret->tag++;
return ret;
} int mid=l+r>>1;
if(le<=mid) ret->lc=update(ret->lc,l,mid);
if(ri>mid) ret->rc=update(ret->rc,mid+1,r); return ret;
} int query(node *u,int l,int r){
if(l==r) return u->tag;
int mid=l+r>>1;
if(le<=mid) return u->tag+query(u->lc,l,mid);
else return u->tag+query(u->rc,mid+1,r);
} inline void init(){
rot[0]=nil->lc=nil->rc=cnt=nil;
nil->tag=0;
for(int i=1;i<=n;i++){
le=pre[a[i]]+1,ri=i,rot[i]=update(rot[i-1],1,1000000);
pre[a[i]]=i;
}
} inline void solve(){
scanf("%d",&Q);
while(Q--){
scanf("%d%d",&le,&ri);
printf("%d\n",query(rot[ri],1,1000000));
}
} int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",a+i);
init();
solve();
return 0;
}

  


Spoj 3267 DQUERY - D-query的更多相关文章

  1. SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  2. SPOJ 3267 D-query(离散化+在线主席树 | 离线树状数组)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  3. SPOJ 3267. D-query (主席树,查询区间有多少个不相同的数)

    3267. D-query Problem code: DQUERY English Vietnamese Given a sequence of n numbers a1, a2, ..., an  ...

  4. SPOJ - 3267. D-query 主席树求区间个数

    SPOJ - 3267 主席树的又一种写法. 从后端点开始添加主席树, 然后如果遇到出现过的元素先把那个点删除, 再更新树, 最后查询区间就好了. #include<bits/stdc++.h& ...

  5. SPOJ 3267 DQUERY - D-query (主席树)(区间数的种数)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

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

  7. SPOJ 3267: DQUERY 树状数组,离线算法

    给出q个询问,询问一段区间里面的不同元素的个数有多少个. 离线做,用树状数组. 设树状数组的意义是:1--pos这个段区间的不用元素的种类数.怎么做?就是add(pos,1);在这个位置中+1,就是说 ...

  8. spoj 3267 D-query

    题目链接:http://vjudge.net/problem/SPOJ-DQUERY --------------------------------------------------------- ...

  9. SPOJ 3267 DQUERY(离线+树状数组)

    传送门 话说这好像HH的项链啊…… 然后就说一说上次看到的一位大佬很厉害的办法吧 对于所有$r$相等的询问,需要统计有多少个不同的数,那么对于同一个数字,我们只需要关心它最右边的那一个 比如$1,2, ...

随机推荐

  1. CSData

    NSString 转换成NSData 对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncodin ...

  2. 使用JavaScript给对象修改注册监听器

    我们在开发一些大型前端项目时,会遇到这样一种情况,某个变量上有个字段.我们想知道是哪一段程序修改了这个变量上的字段.比如全局变量window上我们自定义了一个新字段_name,我们想知道到底有哪些程序 ...

  3. xls表格 拼接字段 拼json =CONCAT("{ code:'",A2,"',","codeName: '",B2,"',","flag: '",C2,"'},")

    xls表格 拼接字段 拼json =CONCAT("{ code:'",A2,"',","codeName: '",B2,"',& ...

  4. 爬虫学习之csv读取和存储

    一.读取 该读取主要使用到csv里面的Reader().DictReader()方法,和引用io里面的StringIO进行对字符串进行封装 在处理网上的csv文件方式主要是有一下几方面: • 手动把C ...

  5. CentOS7.6 静态IP配置

    1:选中安装好的虚拟机,点击“编辑”,然后选择“虚拟网络编辑器(N…)”,如下图所示: 2:选择桥接模式,在桥接到指定的本地网卡即可."确定"保存 3:选中虚拟机,右击虚拟机,选择 ...

  6. 线段树、KMP、HASH模板

    线段树 #include<cstdio> using namespace std; int n,p,a,b,m,x,y,ans; struct node { int l,r,w,f; }t ...

  7. ZOJ - 1655 Transport Goods(单源最长路+迪杰斯特拉算法)

    题目: 有N-1个城市给首都(第N个城市)支援物资,有M条路,走每条路要耗费一定百分比(相对于这条路的起点的物资)的物资.问给定N-1个城市将要提供的物资,和每条路的消耗百分比.求能送到首都的最多的物 ...

  8. SQL的几种连接

    1. 内连接 结果: 从左表中取出每一条记录,去右表中与所有的记录进行匹配: 匹配必须是某个条件是左表中与右表中相同,才会保留结果,否则不保留: 1.等值连接:在连接条件中使用等于号(=)运算符比较被 ...

  9. 【eclipse】使用说明

    一. 设置jdk版本 1. Window->Preferences->Java->Installed JREs,选择jdk安装目录: 2. Window->Preference ...

  10. Linux I2C驱动

    Linux I2C 驱动结构 i2c体系结构由三部分组成 i2C core i2c core提供了i2c 总线驱动 和 设备驱动的注册,注销方法 i2C and SMBus protocol 实现 i ...