题目链接:传送门

题目大意:一个容量 n 的数组, m次询问,每次询问 [x,y]内不同数的个数

题目思路:主席树(注意不是权值线段树而是位置线段树)

         也就是按一般线段树的逻辑来写只是用主席树实现而已

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 55005
#define maxn 30010
typedef pair<int,int> PII;
typedef long long LL;
LL read(){
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,k,ans,v,a[N],b[N],L,R;
int loc[N],root[N<<],sz;
map<int,int>M;
struct Node{int l,r,v;}seg[N*];
void update(int &rot,int rt,int l,int r,int pos){
seg[++sz]=seg[rot],rot=sz;
seg[rot].v+=v;
if(l==r)return;
int mid=l+r>>;
if(pos<=mid)update(seg[rot].l,lson,pos);
else update(seg[rot].r,rson,pos);
}
int query(int rt,int l,int r,int x,int y,int rot){
if(x<=l&&r<=y)return seg[rot].v;
int mid=l+r>>;
int temp=;
if(x<=mid)temp+=query(lson,x,y,seg[rot].l);
if(y>mid) temp+=query(rson,x,y,seg[rot].r);
return temp;
}
int main(){
int i,j,group,x,y,Case=;
n=read();
for(i=;i<=n;++i)a[i]=read(),b[i]=a[i];
sort(b+,b+n+);
int _n=unique(b+,b+n+)-b-;
for(i=;i<=_n;++i)M[b[i]]=i;
for(i=;i<=n;++i){
v=;
if(!loc[M[a[i]]])update(root[i]=root[i-],,,n,i);
else{
int temp;
v=-;update(temp=root[i-],,,n,loc[M[a[i]]]);
v=;update(root[i]=temp,,,n,i);
}
loc[M[a[i]]]=i;
}
m=read();
for(i=;i<=m;++i){
x=read(),y=read();
printf("%d\n",query(,,n,x,y,root[y]));
}
return ;
}

SPOJ - DQUERY的更多相关文章

  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 离线树状数组+离散化

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

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

    版权声明:本文为博主原创文章,未经博主允许不得转载. SPOJ DQUERY 题意: 给出一串数,询问[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 DQUERY:D-query

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

  8. SPOJ D-query(莫队算法模板)

    题目链接:http://www.spoj.com/problems/DQUERY/ 题目大意:给定一个数组,每次询问一个区间内的不同元素的个数 解题思路:直接套莫队的裸题 #include<cs ...

  9. SPOJ - DQUERY 主席树

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

  10. 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. java资料——顺序存储结构和链式存储结构(转)

    顺序存储结构 主要优点 节省存储空间,随机存取表中元素 缺    点 插入和删除操作需要移动元素 在计算机中用一组地址连续的存储单元依次存储线性表的各个数据元素,称作线性表的顺序存储结构. 顺序存储结 ...

  2. jQuery 之正则表达式篇

    从本文开始,我将陆续的更新关于jQuery源代码的博客.首先,jQuery源代码分析一直是我的一个计划和追求.查看jQuery源代码,探索大牛们深邃的思想,精神的碰撞.Google 搜索不难发现,探索 ...

  3. 网络打印协议之LPR或RAW

    网络打印端口 标准 TCP/IP 端口监视器 对于连接到网络的打印设备,标准的 TCP/IP 端口监视器就是最佳选择.标准端口监视器增强了LPR(line printer remote,远程行式打印机 ...

  4. linux中hive无法使用删除键或无反应

    用的工具secureCRT连接linux服务器的,登录到hive中后无法使用删除键. 修改如下: 会话选项 -> 终端|仿真 ->  选择终端为linux.

  5. UVA 11019 Matrix Matcher 矩阵匹配器 AC自动机 二维文本串查找二维模式串

    链接:https://vjudge.net/problem/UVA-11019lrjP218 matrix matcher #include<bits/stdc++.h> using na ...

  6. [转]Private Libraries、Referenced Libraries、Dependency Libraries的区别

    一.v4.v7.v13的作用和用法 1.Android Support V4, V7, V13是什么? 本质上就是三个java library. 2.为什么要有support库?   是为了解决软件的 ...

  7. 错误将UIViewController当做UITableViewController来用

  8. C# - 简单介绍TaskScheduler

    task Scheduler根据定义 The task Scheduler by the definition blurb. “Is the class where the usage context ...

  9. (三)使用预定义模型QDirModel的例子

    使用预定义模型QDirModel的例子 Main.cpp #include <QApplication> #include "directoryviewer.h" in ...

  10. variable `xxx' has initializer but incomplete type

    错误:variable `xxx' has initializer but incomplete type 原因:xxx对应的类型没有找到,只把xxx声明了但是没给出定义.编译器无从确认你调用的构造函 ...