DQUERY - D-query

English Vietnamese

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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=5e4+;
const int M=N*N+;
struct seg {
int lson,rson;
int cnt;
};
seg T[N*];
int root[N],tot;
vector<int>pos;
int arr[N];
int last_pos[N]; void init() {
pos.clear();
met(root,);met(last_pos,);
tot=;
T[].cnt=T[].lson=T[].rson=;
}
void update(int &cur,int ori,int l,int r,int pos,int flag) {
cur=++tot;
T[cur]=T[ori];
T[cur].cnt+=flag;
if(l==r)
return ;
int mid=(l+r)/;
if(pos<=mid)
update(T[cur].lson,T[ori].lson,l,mid,pos,flag);
else
update(T[cur].rson,T[ori].rson,mid+,r,pos,flag);
}
int query(int S,int E,int l,int r,int x,int y) {
if(x<=l&&r<=y)
return T[E].cnt-T[S].cnt;
else {
int mid=(l+r)/;
if(y<=mid)
return query(T[S].lson,T[E].lson,l,mid,x,y);
else if(x>mid)
return query(T[S].rson,T[E].rson,mid+,r,x,y);
else
return query(T[S].lson,T[E].lson,l,mid,x,mid)+query(T[S].rson,T[E].rson,mid+,r,mid+,y);
}
}
int main(void) {
int n,m,i,l,r;
while (~scanf("%d",&n)) {
init();
for (i=; i<=n; ++i) {
scanf("%d",&arr[i]);
pos.push_back(arr[i]);
}
scanf("%d",&m);
sort(pos.begin(),pos.end());
pos.erase(unique(pos.begin(),pos.end()),pos.end());
int temp_rt=;
for (i=; i<=n; ++i) {
arr[i]=lower_bound(pos.begin(),pos.end(),arr[i])-pos.begin()+;
if(!last_pos[arr[i]]) {
update(root[i],root[i-],,n,i,);
last_pos[arr[i]]=i;
} else {
update(temp_rt,root[i-],,n,last_pos[arr[i]],-);
update(root[i],temp_rt,,n,i,);
last_pos[arr[i]]=i;
}
}
for (i=; i<m; ++i) {
scanf("%d%d",&l,&r);
printf("%d\n",query(root[l-],root[r],,n,l,r));
}
}
return ;
}


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: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) ...

  3. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  4. zoj2112 树状数组+主席树 区间动第k大

    Dynamic Rankings Time Limit: 10000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Subm ...

  5. HDU 4348 To the moon(主席树区间修改)

    题意 给你一个区间,支持如下操作: 在一段区间内加上一个值,并生成一个历史版本 查询某个版本下一段区间内的和 回到一个历史版本上并舍弃之后的版本 做法 这就是主席树区间修改裸题啦QwQ 上一篇博客我讲 ...

  6. HDU 4348 主席树区间更新

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...

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

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

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

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

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

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

随机推荐

  1. PICT:基于正交法的软件测试用例生成工具

    成对组合覆盖这一概念是Mandl于1985年在测试Aad编译程序时提出来的.Cohen等人应用成对组合覆盖测试技术对Unix中的“Sort”命令进行了测试.测试结果表明覆盖率高达90%以上.可见成对组 ...

  2. C++ Primer 第3章 字符串、向量和数组

    C++ Primer 第3章 字符串.向量和数组 C Primer 第3章 字符串向量和数组 1 命名空间的using声明 2 标准库类型string 3 标准库类型vector 4 迭代器介绍 5 ...

  3. linux下如何修改进程优先级?

    linux下的进程调度优先级是从-20到19,一共40个级别,数字越大,表示进程的优先级越低.默认时候,进程的优先级是0.查看进程优先级有两个办法:ps和top. 改变进程的优先级的方法有两种: 1, ...

  4. java链接数据库--Mysql

    /************************************************************************* > File Name: Mysql.jav ...

  5. BZOJ3668 [Noi2014]起床困难综合症 【贪心】

    题目 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过研究相关文献,他找到了该病的发病原因 ...

  6. 太空飞船(spaceship)

    太空飞船(spaceship) 题目描述 21XX年,秋. 小诚是THU(Tomorrow Happy University)航天学院船舶设计系本科四年级的学生.为了顺利毕业,小诚仔细阅读了这几年被引 ...

  7. 3.2 Lucene实战:一个简单的小程序

    在讲解Lucene索引和检索的原理之前,我们先来实战Lucene:一个简单的小程序! 一.索引小程序 首先,new一个java project,名字叫做LuceneIndex. 然后,在project ...

  8. DP———6.两个状态之间的 处理

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. Audio Unit 介绍

    关于 Audio Unit iOS 提供了音频处理插件,支持混音,声音均衡,格式转化,以及用于录音,回放,离线渲染,实时对话的输入输出.可以动态载入和使用这些强大而灵活的插件,在 iOS 应用中这些插 ...

  10. hihocoder 后缀自动机二·重复旋律5

    求不同子串个数 裸的后缀自动机 #include<cstring> #include<cmath> #include<iostream> #include<a ...