题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32356

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.

题意:给出很多询问,求出每次询问数列中连续区间的不同的数的个数。

算法分析:算法思想是主席树,这个算法真是神奇得没话说,必须得崇拜呀。由于刚接触这个算法思想,所以主席树是参照kuangbin大牛的模板来写的。

引用主席树的发明人说过的两句话:

这个东西是当初我弱不会划分树的时候写出来替代的一个玩意..被一小撮别有用心的人取了很奇怪的名字> <  。

想法是对原序列的每一个前缀[1..i]建立出一颗线段树维护值域上每个数的出现次数,然后发现这样的树是可以减的,然后就没有然后了 。

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = maxn*+; int n,q,tot;
int an[maxn];
int T[maxn],lson[M],rson[M],c[M];
int build(int l,int r)
{
int root=tot++;
c[root]=;
if (l!=r)
{
int mid=(l+r)>>;
lson[root]=build(l,mid);
rson[root]=build(mid+,r);
}
return root;
}
int update(int root,int pos,int val)
{
int newroot=tot++,tmp=newroot;
c[newroot]=c[root]+val;
int l=,r=n;
while (l<r)
{
int mid=(l+r)>>;
if (pos<=mid)
{
lson[newroot]=tot++ ;rson[newroot]=rson[root];
newroot=lson[newroot] ;root=lson[root];
r=mid;
}
else
{
rson[newroot]=tot++ ;lson[newroot]=lson[root];
newroot=rson[newroot] ;root=rson[root];
l=mid+;
}
c[newroot]=c[root]+val;
}
return tmp;
}
int query(int root,int pos)
{
int ret=;
int l=,r=n;
while (pos<r)
{
int mid=(l+r)>>;
if (pos<=mid) {r=mid ;root=lson[root] ;}
else {ret += c[lson[root] ] ;root=rson[root] ;l=mid+ ;}
}
return ret+c[root];
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
tot=;
for (int i= ;i<=n ;i++) scanf("%d",&an[i]);
T[n+]=build(,n);
map<int,int> mp;
mp.clear();
for (int i=n ;i>= ;i--)
{
if (mp.find(an[i])==mp.end())
T[i]=update(T[i+],i,);
else
{
int tmp=update(T[i+],mp[an[i] ],-);
T[i]=update(tmp,i,);
}
mp[an[i] ]=i;
}
scanf("%d",&q);
while (q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",query(T[l],r));
}
}
return ;
}

SPOJ - DQUERY 主席树的更多相关文章

  1. SPOJ - DQUERY 主席树求区间有多少个不同的数(模板)

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

  2. D-query SPOJ - DQUERY 主席树查询区间内不同数出现的次数

    我们不以权值建立主席树,而是区间端点作为值建立线段树,一个个插入a[i],我们发现这个数之前是存在的,就需要在上个版本的主席树上减去原来的位置,并加上现在的位置,这样我们在i版本的主席树,维护1-r中 ...

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

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

  4. Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)

    Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...

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

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

  6. SPOJ - TTM 主席树

    给你一个系列\(a[1...n]\),要求可以区间求和,区间更新,也可以回溯过去 经典的主席树板子题,很久以前做的题了,代码太丑回炉重写 PS.题目标题To The Moon也是我最喜欢的游戏之一 这 ...

  7. $SP$3267 $DQUERY - D-query$ 主席树

    正解:主席树 解题报告: 传送门! 一直在做$dp$题好久没做做别的了,,,所以来做点儿别的练练手,,,不然以前学的全忘了要/$kk$ 然后这题好像可以莫队/主席树/线段树/树状数组? 我就先只港下主 ...

  8. SPOJ - DQUERY D-query 主席树

    题意; 给一个数列$\{ a_i\}$ 一些询问$(l_i,r_i)$ 问你$[l,r]$有多少个不同元素 题解: 其实本质思路和离线化处理询问的线段树/树状数组写法差不多,对区间$[x,r]$来说, ...

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

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

随机推荐

  1. 管理员 修改MySQL 5.7.9 新版本的root密码方法以及一些新变化整理

    MySQL 5.7版本开始,增强密码验证机制,网上说安装的时候会在/root/.mysql_secret  文件中生成默认密码,这一点自 5.7.6版本以后也去掉了. 针对如果生成默认密码,网上有一个 ...

  2. .NET String.Format 方法 线程安全问题

    碰到这个问题 是在和淘宝做信息交互的时候, 接收别人N年前的代码. 代码逻辑很简单,就是取得信息 数据库查询  响应请求返回结果. 最近淘宝的人反映说 N多账户使用的是一个单号.理论上来说 是应该每次 ...

  3. 线程操作API

    线程操作API 1.currentThread 2.getId() .getName().getPriority().getStart.isAlive().isDaemon().isInterrupt ...

  4. PBOC规范(2.0->3.0)对照表

    1    数据方面 TAG                                               PBOC2.0                                 ...

  5. SharePoint 项目的死法(一)

    SharePoint是Microsoft的一个巨NB的产品, 从可查到的数据来看, 财富500强中已经有超过80%的企业已经使用了SharePoint的不同版本,从项目实施的经验来看, 个人感觉这个数 ...

  6. 通过DB_LINK按照分区表抽取数据

    DB:11.2.0.3.0OS:oracle-linux 5.7 建表语句:create table YOON.YOON_HIS(  c_id              NUMBER not null ...

  7. 详谈 oracle 索引 (笔记)

    1.oracle索引空值问题 当在有空值得列上建立单列索引时,如果搜索条件为 is null 在解释计划中可以看到,对于此列oracle并没有使用索引查询: 但是当建立的是多列索引是,就会按照索引来进 ...

  8. UITextField使用的相关方法

    1.设置占位符 textField.placeholder = @“”; 2.设置暗文输入 textField.secureTextEntry = YES;   3.设置键盘类型 textField. ...

  9. 编写可维护的JavaScript之简易模版

    /* * 正则替换%s * @para arg1(text) 需要替换的模版 * @para arg2 替换第一处%s * @para arg3 替换第二处%s * 返回替换后的字符串 */ var ...

  10. ios中怎么处理键盘挡住输入框

    //此前要遵循UITextFieldDelegate代理.并且设置该文本框为当前控制器的代理 //开始编辑的时候让整个view的高度网上移动 - (void)textFieldDidBeginEdit ...