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 ...
随机推荐
- linux网口驱动实现(待续)
驱动位于linux/driver/net下. 1. 寄存器读写 asm/io.h #define __raw_writeb(v,a) (__chk_io_ptr(a), *(volatile un ...
- iOS边练边学--(Quartz2D)图片裁剪,带圆环的裁剪
一.图片裁剪,示意图 二.带圆环的图片裁剪示意图
- Ajax-ajax实例1-动态加载的 FAQ
动态加载 FAQ 的过程主要是利用 XMLHttpRequest(以下简称 XHR)对象与服务端通信,根据用户单击的感兴趣问题动态将内容加载到页面中.在具体实现时,有两点要注意的内容. 1 .对每个问 ...
- shell脚本----for循环
1.方法1 #!/bin/bash for((i=1;i<10;i++)) do echo $i done 保存为for1.sh 直接sh for1.sh 会报错: Syntax error ...
- Python if-else and while
if-elif语法 if condition: [tab键] command elif condition: [tab键] command elif condition: [tab键] command ...
- Spring in Action 4th 学习笔记
约定: 一.@Xxx Class 表示被@Xxx注解的类.同理还有@Xxx注解的字段或方法. 例如:@Bean Method. 二.@Component Class 同时代指 @Controller. ...
- ssm框架搭建并演示获取json字符串
为方便起见,使用oracle自带的scott账户中的emp表作为数据源. 预期效果: 1.输入URL:http://localhost:8888/ssm/getEmp?empno=7369 2.返回结 ...
- Hadoop参数汇总
linux参数 以下参数最好优化一下: 文件描述符ulimit -n 用户最大进程 nproc (hbase需要 hbse book) 关闭swap分区 设置合理的预读取缓冲区 Linux的内核的IO ...
- XP 终端服务组件 ,SP3 多用户补丁(替换)文件
如附件 termsrv.dll 5.1.2600.5512 目前存在一个问题:每个用户只能使用一个会话.不能像2003+那样,一个用户使用多个会话. 待查找解决方案中............... ...
- golang包管理
https://studygolang.com/articles/8413 https://studygolang.com/articles/10523