SPOJ DQUERY D-query(主席树 区间不同数个数)
题意:问你区间有几个不同的数
思路:主席树nb。我们知道主席树每一个root都存着一棵权值线段树,现在我们在每个root中存位置,也就是01表示这个位置存不存在。然后我们用一个fa[a[i]]表示a[i]这个数在前面出现的位置。如果没有在前面出现过,那么我们直接把这个位置变成1,;如果出现过了,我们就把当前root节点下的线段树的fa[a[i]]位置减掉。也就是说我每个数字存在的位置现在只出现当前区间的最右边的那个位置,那我直接每次询问L R区间个数,那么我直接遍历root[R]线段树的L R区间,看一下有几个位置就好了。
代码:
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + ;
const int M = maxn * ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int a[maxn], root[maxn], tot;
int n, m;
struct node{
int lson, rson;
int sum;
}T[maxn * ];
int fa[maxn];
void init(){
tot = ;
memset(fa, -, sizeof(fa));
memset(T, , sizeof(T));
} void update(int l, int r, int &now, int pre, int pos, int v){
T[++tot] = T[pre], now = tot;
T[now].sum += v;
if(l == r) return;
int m = (l + r) >> ;
if(pos <= m)
update(l, m, T[now].lson, T[pre].lson, pos, v);
else
update(m + , r, T[now].rson, T[pre].rson, pos, v);
}
int query(int l, int r, int L, int R, int now){
if(L <= l && R >= r){
return T[now].sum;
}
int m = (l + r) >> , sum = ;
if(L <= m)
sum += query(l, m, L, R, T[now].lson);
if(R > m)
sum += query(m + , r, L, R, T[now].rson);
return sum;
}
int main(){
int ca = ;
init();
scanf("%d", &n);
int cnt = 1e6;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
if(fa[a[i]] == -){
update(, cnt, root[i], root[i - ], i, );
fa[a[i]] = i;
}
else{
update(, cnt, root[i], root[i - ], i, );
update(, cnt, root[i], root[i], fa[a[i]], -);
fa[a[i]] = i;
}
}
scanf("%d", &m);
while(m--){
int L, R;
scanf("%d%d", &L, &R);
printf("%d\n", query(, cnt, L, R, root[R]));
} return ;
}
SPOJ DQUERY D-query(主席树 区间不同数个数)的更多相关文章
- HDU 5919 Sequence II(主席树+区间不同数个数+区间第k小)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5919 题意:给出一串序列,每次给出区间,求出该区间内不同数的个数k和第一个数出现的位置(将这些位置组 ...
- hdu 5919 主席树(区间不同数的个数 + 区间第k大)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- SPOJ DQUERY (主席树求区间不同数个数)
题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...
- hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)
题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...
- zoj2112 树状数组+主席树 区间动第k大
Dynamic Rankings Time Limit: 10000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Subm ...
- HDU 4348 To the moon(主席树区间修改)
题意 给你一个区间,支持如下操作: 在一段区间内加上一个值,并生成一个历史版本 查询某个版本下一段区间内的和 回到一个历史版本上并舍弃之后的版本 做法 这就是主席树区间修改裸题啦QwQ 上一篇博客我讲 ...
- HDU 4348 主席树区间更新
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- SPOJ:D-query(非常规主席树求区间不同数的个数)
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...
随机推荐
- Nginx 安装后 相关错误解决
1,安装 https://blog.csdn.net/wxyjuly/article/details/79443432 nobody 改为了 www /configure --user=www --g ...
- gitlab自动备份和定时删除
GitLab数据手动备份1.GitLab默认备份目录为/var/opt/gitlab/backups,可以修改/etc/gitlab/gitlab.rb里面的默认存放备份文件目录,这里使用默认备份目录 ...
- 学号 20175313《Java程序设计》 第七周学习总结
目录 一.教材学习内容总结 二.教材学习中的问题和解决过程 三.代码托管 四.心得体会 五.学习进度条 六.参考资料 一.教材学习内容总结 第八章主要内容 了解String类 常量对象:常量池中的数据 ...
- [jquery.validate]自定义方法实现"手机号码或者固定电话"的逻辑验证
最近项目开发中遇到这样的需求“手机号码或者固话至少填写一个”,如下图所示: 项目采用的jquery.validate.js验证组件,目前组件不支持这种“或”逻辑的验证,于是就自己定义一个 jQuery ...
- 2019-04-27 Python之有关文件的学习
一.文件 1.简介 二.文件的相关操作 1.打开文件 例如: f = open("F:\\text.txt", 'r') 只读模式,不可写入 2.读取文件 例如: f.readl ...
- levmar : Levenberg-Marquardt库编译
levmar : Levenberg-Marquardt 是非线性优化的一个库 1.使用CMake生成sln项目,编译 clapack库 在levmar工程中,打开misc.c文件,在最开始添加#in ...
- mysql 状态锁 连接数
show OPEN TABLES where In_use > 0; show status like 'Table%'; SELECT * FROM information_schema.in ...
- vue页面无操作10分钟内调转到登录页面
https://blog.csdn.net/lbPro0412/article/details/83864454 页面在设定时间内无任何操作(鼠标的点击.滑动.路由的切换.是否请求接口等),跳转到登录 ...
- 实验吧MD5之守株待兔解题思路
解题链接 http://ctf5.shiyanbar.com/misc/keys/keys.php 解题思路 首先我们多打开几次解题链接,发现系统密钥大约在一秒钟左右变一次,所以联想到时间戳. 解题过 ...
- 构建stm32最小系统板注意事项
由于学习工作的需要自己定制stm32系统板,以下将关键的注意事项进行解释,欢迎交流.(以stm32f103为例) 1.VBAT引脚 在主流的设计中,VBAT与0欧的电阻串联,接至3.3V. 2.OSC ...