主席树/树状数组。给一个区间,多次询问[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. linux 输入子系统(3)----事件处理(input_handler层)

    输入子系统主要设计input_dev.input_handler.input_handle.如下: [1]每个struct input_dev代表一个输入设备 struct input_dev { c ...

  2. 生成XML文件,通过实体生成XML文件

    实体 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xm ...

  3. MVC5+EF6+BootStrap3.3.5 博客系统之项目搭建(一)

    环境:vs2013,sql2008R2 引用版本:MVC5,EF6,BootStrap3.3.5 在之前一直都是webfrom开发,虽然开发简单:但是有很多不足的地方.在之前开发都是webfrom+M ...

  4. schtasks命令遇见ERROR: The request is not supported.

    执行schtasks命令的环境,下文为机器1:windows server 2008 r2 工作任务(Schedules)所在的机器环境,下文为机器2:windows server 2003 当在机器 ...

  5. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  6. 拼音操作工具类 - PinyinUtil.java

    拼音操作工具类,提供字符串转换成拼音数组.汉字转换成拼音.取汉字的首字母等方法. 源码如下:(点击下载 -PinyinUtil.java.pinyin4j-2.5.0.jar ) import net ...

  7. Mapped Statements collection does not contain value for

    这是由pojo的映射文件的命名空间引起的错误. 按照以下格式即可:命名空间中一定要含有mapper.xxMapper这样的格式,否则出现以上错误. <?xml version="1.0 ...

  8. MVC EF异常-“序列化类型为 XX 的对象时检测到循环引用”

    原因:在EF实体中,两个互为主外键关系的实体类的导航属性相互引用. 解决方法一:删除一个不需要的类的导航属性 方法二:使用DTO模型 方法三:直接返回需要的属性(不能包括相互引用的属性)

  9. 1027-Quicksum

    描述 A checksum is an algorithm that scans a packet of data and returns a single number. The idea is t ...

  10. struts2 标签库 介绍

    struts2.0里的标签没有分类,只用在jsp头文件加上<%@ taglib prefix="s" uri="/struts-tags" %>就能 ...