主席树/树状数组。给一个区间,多次询问[l,r]内有多少个不重复的元素。每个前缀都建线段树,询问直接r的[l,r]就可以了。(似乎对主席树有一点了解了?。。。话说spoj好高级的样子。。。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
const int nmax=30005;
const int inf=0x7f7f7f7f;
struct node{
node *l,*r;int sum;
};
node nodes[nmax*25],*pt=nodes,*root[nmax];
int a[nmax],b[nmax],vis[nmax*40];
node* build(int l,int r){
node* op=pt++;op->sum=0;
if(l==r) return op;
int mid=(l+r)>>1;
op->l=build(l,mid);op->r=build(mid+1,r);
return op;
}
node* update(int p,int add,node* t,int l,int r){
node* op=pt++;op->sum=t->sum+add;
if(l==r) return op;
int mid=(l+r)>>1;
if(p<=mid) op->r=t->r,op->l=update(p,add,t->l,l,mid);
else op->l=t->l,op->r=update(p,add,t->r,mid+1,r);
return op;
}
int query(int tl,int tr,int l,int r,node* t){
if(tl<=l&&tr>=r) return t->sum;
int ans=0,mid=(l+r)>>1;
if(tl<=mid) ans+=query(tl,tr,l,mid,t->l);
if(tr>mid) ans+=query(tl,tr,mid+1,r,t->r);
return ans;
}
int main(){
int n,m;
while(scanf("%d",&n)!=EOF){
pt=nodes;clr(vis,0);
REP(i,1,n) a[i]=read(); root[0]=build(1,n);
REP(i,1,n){
node* t=update(i,1,root[i-1],1,n);
if(vis[a[i]]) root[i]=update(vis[a[i]],-1,t,1,n);
else root[i]=t;
vis[a[i]]=i;
} m=read();
REP(i,1,m){
int s=read(),t=read();
printf("%d\n",query(s,t,1,n,root[t]));
}
}
return 0;
}

  

Time Limit: 227MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu

Submit Status

Description

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

Hint

Added by: Duc
Date: 2008-10-26
Time limit: 0.227s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: All except: ERL JS NODEJS PERL 6 VB.net
Resource: © VNOI

SPOJ DQUERY:D-query的更多相关文章

  1. [主席树]SPOJ DQUERY

    题目链接 题意:n个数 m个查询 查询的是[l, r]区间内不相同的数的个数 没有修改,因此静态的主席树就好了 将重复的元素建树即可 query的时候加起来,用区间长度(r-l+1)去减就是答案 (q ...

  2. SPOJ DQUERY树状数组离线or主席树

    D-query Time Limit: 227MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submit Status ...

  3. SPOJ DQUERY D-query (在线主席树/ 离线树状数组)

    版权声明:本文为博主原创文章,未经博主允许不得转载. SPOJ DQUERY 题意: 给出一串数,询问[L,R]区间中有多少个不同的数 . 解法: 关键是查询到某个右端点时,使其左边出现过的数都记录在 ...

  4. SPOJ DQUERY 离线树状数组+离散化

    LINK 题意:给出$(n <= 30000)$个数,$q <= 2e5$个查询,每个查询要求给出$[l,r]$内不同元素的个数 思路:这题可用主席树查询历史版本的方法做,感觉这个比较容易 ...

  5. SPOJ - DQUERY(区间不同数+树状数组)

    链接:SPOJ - DQUERY 题意:求给定区间不同数的个数(不更新). 题解:离线+树状数组. 对所求的所有区间(l, r)根据r从小到大排序.从1-n依次遍历序列数组,在树状数组中不断更新a[i ...

  6. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  7. 【SPOJ】375. Query on a tree(树链剖分)

    http://www.spoj.com/problems/QTREE/ 这是按边分类的. 调试调到吐,对拍都查不出来,后来改了下造数据的,拍出来了.囧啊啊啊啊啊啊 时间都花在调试上了,打hld只用了半 ...

  8. SPOJ - DQUERY 主席树

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32356 Given a sequence of n numbers ...

  9. SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)

    DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...

随机推荐

  1. XZ压缩最新压缩率之王

    xz这个压缩可能很多都很陌生,不过您可知道xz是绝大数linux默认就带的一个压缩工具. 之前xz使用一直很少,所以几乎没有什么提起. 我是在下载phpmyadmin的时候看到这种压缩格式的,phpm ...

  2. liger 的 ligerDialog 的使用。

    /// 查看页面,跳出一个新的添加窗口 function AddMessage() { $.ligerDialog.open({ url: UrlAddMessage, height: , width ...

  3. 移植openssl

    1.官网(ftp://ftp.openssl.org/source/old/0.9.x/)下载openssl-0.9.8k.tar.gz2.交叉编译openssl CC=arm-hisiv400-li ...

  4. Oracle监听器—静态注册

    注册就是将数据库作为一个服务注册到监听程序.客户端不需要知道数据库名和实例名,只需要知道该数据库对外提供的服务名就可以申请连接到数据库.这个服务名可能与实例名一样,也有可能不一样. 注册分: 1. 静 ...

  5. You have new mail in /var/spool/mail/root 烦不烦你?

    http://blog.csdn.net/yx_l128125/article/details/7425182

  6. EXTJS4.2 控件之Grid 行点击事件

    listeners: { 'itemclick': function (view, record, item, index, e) { //Ext.MessageBox.alert("标题& ...

  7. EasyUI + EF + MVC4 后台截图

    到目前完成的页面截图,完成了增删改查几项功能的技术测试,在解决几个小问题,就重新设计结构开始一个完整的后台开发,坚持用博客和云笔记记录开发过程.

  8. Xcode 合并分支报错

    原理和操作步骤见如下转载的两篇文章, 我所使用的 svn 客户端软件是 Mac 下面的 Versions.app v1.06 这个版本包含一个多人开发的bug bug 的解决方案见我之前转载的两篇文章 ...

  9. TWaver初学实战——如何在EasyUI中插入TWaver

    TWaver是一款强大的图形界面开发组件,可以很方便地集成到其他开发工具中.今天就用一个小例子来操练如何结合TWaver和EasyUI进行网页开发. 准备工作 俗话说他山之玉可以直接拿来,EasyUI ...

  10. storm sum aggregate 原语 聚合 本地测试

    编写storm程序,对数据进行聚合并且写入到mysql, 本文  主要说明数据中有多个字段需要进行sum或其他操作时的程序写法 1.主程序main方法,storm 拓扑运行入口 public clas ...