主席树/树状数组。给一个区间,多次询问[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. location跳转和header跳转的区别

    1:header("location:url") 跳转之前不能有任何输出,如果想在header之前有输出,则要修改php.ini文件.具体 output_handler =mb_o ...

  2. ajax post 时 form数据serialize()

    $.post(UrlAddData, $(".AddForm").serialize(), function (data) { if (data.result) { $.liger ...

  3. Django的templates模版

    前面介绍的都是django.http.HttpResponse 把内容直接返回到网页上,这次介绍通过渲染模版的方法来显示内容 步骤: 1.创建一个项目(略) 2.创建一个app(略) 3.添加项目到s ...

  4. 【BZOJ 1087】[SCOI2005]互不侵犯King

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  5. 解决mybatis查询返回结果值串查

    方式一: 通过as 指定 大写重名列的 别名 方式二: 命名数据库中表名时 每个表的主键 id 要起不同的名称, 避免主键重复(但是子表的外键可以和主表的id主键重名, 你想啊, 从表的外键性质不就是 ...

  6. PlayerPrefs存储数据在本地的存储位置

    PlayerPrefs存储数据时,其在windows的存储路径是注册表: HKEY_CURRENT_USER Software CompanyName ProjectName 其中的CompanyNa ...

  7. iOS sqlite 增删改查 简单封装(基于 FMDB)

    /** *  对 sqlite 的使用进行简单封装,仅涉及简单的单表 增删改查 * *  基于 FMDB * *  操作基于 model ,数据库表字段与 model 属性一一对应,对 model 整 ...

  8. HTTP协议返回代码含义

    1XX 代码这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个1xx 响应 100 – 继续101 – 切换协议2XX 代码这类状态代码表明服务器成功地接受了客户端请求,一般日 ...

  9. springMVC数据封装成POJO

    springMVC把前台的数据封装为POJO与struts2的封装形式不同.struts2需要在控制器声明需封装的POJO,而springMVC不需要任何准备工作,只需在相应的方法的参数中加上需封装的 ...

  10. js注册登录审核

    <script type="text/javascript"> $(function(){ $("#sendSms").click(function ...