题目描述

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. 伪类的格式重点:父标签层级 & 当前标签类型

    伪类的格式重点:父标签层级 & 当前标签类型 通过例子说明: css1: span:nth-of-type(2){color: red;} css2: span :nth-of-type(2) ...

  2. 如何使用SAP CRM Marketing Survey创建一个市场问卷调查

    使用事务码CRM_SURVEY_SUITE进行编辑.选中Activities这个应用类型,点击新建按钮: 双击Survey的根节点,点击编辑按钮维护Suvey的标题: Survey的正文布局类型(La ...

  3. Web开发者不容错过的10个HTML5工具

    HTML5已经成为当今世界的一个必然组成部分.由于World Wide Web万维网是使用超文本标记语言来架构和呈现的,于是HTML5成为了最流行的编程语言之一.随着网络的不断扩张,Web开发人员非常 ...

  4. docker的网络配置

    Docker的4种网络模式 我们在使用docker run创建Docker容器时,可以用–net选项指定容器的网络模式,Docker有以下4种网络模式: host模式:使用–net=host指定. c ...

  5. ElasticSearch使用spring-data-elasticSearch的用法

    spring-data-Elasticsearch 使用之前,必须先确定版本,elasticsearch 对版本的要求比较高. spring和elasticsearch有两种链接方式,一种是用TCP协 ...

  6. 汇编4OPCODE

    opcode原理 前缀域 切换操作数大小前缀 : 066h 可以将32位的操作数切换成16位的操作数 B8 00010000 | MOV EAX,0x100     66:B8 0001 | MOV ...

  7. Python3基础教程(二十)—— flask介绍

    基本概念 什么是Flask? Flask 是一个 web 框架.也就是说 Flask 为你提供工具,库和技术来允许你构建一个 web 应用程序.这个 web 应用程序可以是一些 web 页面.博客.w ...

  8. END - 提交当前的事务

    SYNOPSIS END [ WORK | TRANSACTION ] DESCRIPTION 描述 END END 提交当前事务. 所有当前事务做的修改都可被其它事务看到并且保证在发生崩溃的情况下的 ...

  9. Open Cascade:如何从AIS_Shape导出TopoDS_Shape?

    Open Cascade:如何从AIS_Shape导出TopoDS_Shape? 实现代码如下: if( !myAISContext->HasOpenedContext()) { wxMessa ...

  10. 错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1099; nested exception is: java.net.BindException: Address already in use: JVM_Bind

    在使用SpringMVC测试的时候, 遇到了这样一个问题, 说的是端口已经被使用了. 代理抛出异常错误: java.rmi.server.ExportException: Port already i ...