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 ;
}


最新文章

  1. Caffe源码解析4: Data_layer
  2. JAVA 线程中的异常捕获
  3. JavaScript学习(2):对象、集合以及错误处理
  4. [VBS]脚本中的字典、动态数组、队列和堆栈
  5. jquery事件切换hover/toggle
  6. Asp.Net多线程用法1
  7. Java [leetcode 7] Reverse Integer
  8. IOS Note - View Controller(视图控制器)
  9. ERROR 2003 (HY000): Can&#39;t connect to MySQL server on &#39;localhost&#39; (10061)
  10. mybatis06 增删改差 源码
  11. openstack theme topic
  12. JBoss7官方下载最新版本
  13. 聊聊 JUC 并发包
  14. 在Node中使用ES6语法
  15. sysbench压力测试工具安装及使用
  16. liblas 1.8.1编译安装
  17. Python 扩展知识:编程习惯
  18. Hdu1542 Atlantis
  19. sublime text3 如何在多行前面快速插入序号
  20. iScroll4插件的使用实例

热门文章

  1. apt-get阿里源
  2. Jmeter获取Cookie并传递到下一个线程---跨线程后cookie找不到了
  3. linux ubuntu开启sshd
  4. 最干净的pyinstaller打包成exe应用程序方法
  5. 团队项目-第一次Scrum 会议
  6. BETA0
  7. MVC从Controller到view进行传值的方法
  8. linux安装软件的几种方法
  9. UVA1316 Supermarket
  10. [CTSC2017][bzoj4903] 吉夫特 [状压dp+Lucas定理]