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) ...
随机推荐
- Chrome浏览器添加控件
1. 添加user agent swithcher模拟浏览器内核 方法: 下载user agent switcher后,打开谷歌浏览器,先复制chrome://extensions/ 到浏览器地址栏 ...
- 2种方式解决vue路由跳转未匹配相应路由避免出现空白页面或者指定404页面
https://www.cnblogs.com/goloving/p/9254084.html https://www.cnblogs.com/goloving/p/9254084.html 1.路由 ...
- 一位数组的最大子数组(debug版)
package 数组; import java.util.Arrays; import java.util.Scanner; public class Sum { public static void ...
- spring boot集成redis实现session共享
1.pom文件依赖 <!--spring boot 与redis应用基本环境配置 --> <dependency> <groupId>org.springframe ...
- mysql----------mysql的一些常用命令
1.查询一张表中某个字段重复值的记录 select id,cert_number from (select id,cert_number,count(*)as n from 表明 group by c ...
- 克隆Rockey6加密狗复制资料
克隆Rockey6加密狗复制资料下载 描述:Rockey6加密狗复制克隆方法Rockey6加密狗复制案例解析! 一.用OD加载DLL,并分析: 10001320 >/$ B8 4C140000 ...
- elastic-job集成到springboot教程,和它的一个异常处理办法:Sharding item parameters '1' format error, should be int=xx,int=xx
先说这个Sharding item parameters '1' format error, should be int=xx,int=xx异常吧,这是在做动态添加调度任务的时候出现的,网上找了一会没 ...
- 工厂方法 Factory Method
背景:有一个应用框架,它可以向用户显示多个文档.在这个框架中,两个主要的抽象是类Application和Document.这两个类都是抽象的.客户必须通过它们的子类来做与举替应用相关的实现. 分析:因 ...
- (转载记录)Active Directory 灾难恢复
部分适用于Windows Server 2003. 在IT环境中谁也不能保证软硬件永远没有故障:那么就需要我们IT能够未雨绸缪,尽量避免故障发生,如果故障发生了,我们需要把损失降到最小:那么就需要我们 ...
- 洛谷 K短路(魔法猪学院)
A*+迪杰特斯拉... 第十一个点卡爆 不管了 #include<iostream> #include<algorithm> #include<cstring> # ...