题目描述

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. checkbox设置复选框的只读效果不让用户勾选

    在Web开发中,有时候需要显示一些复选框(checkbox),表明这个地方是可以进行勾选操作的,但是有时候是只想告知用户"这个地方是可以进行勾选操作的"而不想让用户在此处勾选(比如 ...

  2. Mysql无法启动服务解决办法

    只需要输入:mysqld  --initialize 进行初始化,即可启动

  3. (转)用@Resource注解完成属性装配

    http://blog.csdn.net/yerenyuan_pku/article/details/52858878 前面我们讲过spring的依赖注入有两种方式: 使用构造器注入. 使用属性set ...

  4. 封装addClass 、 removeClass

    <script> window.onload = function() { var oDiv = document.getElementById('div1'); var oDiv2 = ...

  5. dig - 发送域名查询信息包到域名服务器

    SYNOPSIS(总览) dig [@ server ] domain [Aq query-type ] [Aq query-class ] [+ Aq query-option ] [-Aq dig ...

  6. vue 高度 动态更新计算 calcHeight watch $route

    vue 高度 动态更新计算 calcHeight () { // this.tableHeight = window.innerHeight - 210 } }, mounted () { // co ...

  7. vue 模块 props

    inbody.vue <template> <div> <Breadcrumb :style="{margin: '24px 0'}"> < ...

  8. 浅谈nodejs与npm

    (1)npm介绍 在正式介绍Node.js学习之前,我们先认识一下npm. npm是什么东西?npm其实是Node.js的包管理工具(package manager). 为啥需要一个包管理工具呢?因为 ...

  9. win7下自动更新svn目录

    !!注意,文件编码必须是ANSI.否则中文路径会乱码 ::这里是svn安装目录 set svnExe="C:\Program Files\TortoiseSVN\bin\TortoisePr ...

  10. NETCORE使用DB First

    1)引用 (1)Install-Package Microsoft.EntityFrameworkCore (2)Install-Package Microsoft.EntityFrameworkCo ...