SPOJ - DQUERY
题目链接:传送门
题目大意:一个容量 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的更多相关文章
- [主席树]SPOJ DQUERY
题目链接 题意:n个数 m个查询 查询的是[l, r]区间内不相同的数的个数 没有修改,因此静态的主席树就好了 将重复的元素建树即可 query的时候加起来,用区间长度(r-l+1)去减就是答案 (q ...
- SPOJ DQUERY树状数组离线or主席树
D-query Time Limit: 227MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submit Status ...
- SPOJ DQUERY 离线树状数组+离散化
LINK 题意:给出$(n <= 30000)$个数,$q <= 2e5$个查询,每个查询要求给出$[l,r]$内不同元素的个数 思路:这题可用主席树查询历史版本的方法做,感觉这个比较容易 ...
- SPOJ DQUERY D-query (在线主席树/ 离线树状数组)
版权声明:本文为博主原创文章,未经博主允许不得转载. SPOJ DQUERY 题意: 给出一串数,询问[L,R]区间中有多少个不同的数 . 解法: 关键是查询到某个右端点时,使其左边出现过的数都记录在 ...
- SPOJ - DQUERY(区间不同数+树状数组)
链接:SPOJ - DQUERY 题意:求给定区间不同数的个数(不更新). 题解:离线+树状数组. 对所求的所有区间(l, r)根据r从小到大排序.从1-n依次遍历序列数组,在树状数组中不断更新a[i ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- SPOJ DQUERY:D-query
主席树/树状数组.给一个区间,多次询问[l,r]内有多少个不重复的元素.每个前缀都建线段树,询问直接r的[l,r]就可以了.(似乎对主席树有一点了解了?...话说spoj好高级的样子... #incl ...
- SPOJ D-query(莫队算法模板)
题目链接:http://www.spoj.com/problems/DQUERY/ 题目大意:给定一个数组,每次询问一个区间内的不同元素的个数 解题思路:直接套莫队的裸题 #include<cs ...
- SPOJ - DQUERY 主席树
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32356 Given a sequence of n numbers ...
- 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 ...
随机推荐
- tar -h 参数
1.1.1 tar命令参数-h,-h参数会把软链接指向的文件也打包. [root@ob2 mytmp]# ll total 8 -rw-r--r--. 1 root root 910 Aug 12 2 ...
- DataGridView使用技巧十二:DataGridView Error图标表示的设定
为了提醒用户注意,DataGridView可以使用Error图标来突出显示. Error图标可以在单元格和行头内表示,但不能在列头上显示. 1.ErrorText属性 当设定单元格/行的ErrorTe ...
- Juint测试
添加"Juint测试"组件: 之后不用写在main方法里面便可以直接测试:
- 执行sql出现No Dialect mapping for JDBC type: -9错误
name的数据类型是nvarchar吗?如果是改为varchar类型应该能运行,但是这个治标不治本,这是hibernate没映射nvarchar 刚才整理了一下varchar和nvarchar的区别, ...
- jQuery EasyUI教程之datagrid应用
一.利用jQuery EasyUI的DataGrid创建CRUD应用 对网页应用程序来说,正确采集和管理数据通常很有必要,DataGrid的CRUD功能允许我们创建页面来列表显示和编辑数据库记录.本教 ...
- 单精度浮点数(float)加法计算出错
场景: 一个float型的变量赋值1170601,加上19000000,结果出现错误. 原因: float占用4个字节(32位)存储空间,包括符号位1位,阶码位8位,尾数23位.浮点数精度与它的尾数有 ...
- 时间操作(struct tm、time_t)求指定日期 前n天的日期
1.在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFINED struct tm { int tm_sec; /* 秒–取值 ...
- Python3.4下使用sqlalchemy
一. 1.用sudo apt-get install python3-numpy之后,会默认把numpy安装到 /usr/lib/python3/dist-packages目录下,而且版本比较低. ...
- java设计模式——多例模式
★ 缓存在单例中的使用 缓存在编程中使用很频繁,有着非常重要的作用,它能够帮助程序实现以空间换取时间,通 常被设计成整个应用程序所共享的一个空间,现要求实现一个用缓存存放单例对象的类. 说明:该 ...
- e558. 在Applet中多图片交互显示
This is the simplest applet to animate an array of images. In practice, you should use double-buffer ...