SPOJ - 3267. D-query 主席树求区间个数
主席树的又一种写法。 从后端点开始添加主席树, 然后如果遇到出现过的元素先把那个点删除, 再更新树, 最后查询区间就好了。
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
//#define lson l,m,rt<<1
//#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = (int)1e9+;
const int N = 3e4 + ;
const int M = 1e6 + ;
int root[N], lson[M], rson[M], cnt[M];
int a[N];
map<int,int> mp;
int tot;
int Build(int l, int r){
int now = ++tot;
cnt[now] = ;
if(l < r){
int m = l+r >> ;
lson[now] = Build(l, m);
rson[now] = Build(m+, r);
}
return now;
}
int Update(int l, int r, int pre, int c, int v){
int now = ++tot;
cnt[now] = cnt[pre] + v;
if(l < r){
int m = l+r >> ;
if(c <= m){
rson[now] = rson[pre];
lson[now] = Update(l, m, lson[pre], c, v);
}
else {
lson[now] = lson[pre];
rson[now] = Update(m+, r, rson[pre], c, v);
}
}
return now;
}
int Query(int l, int r, int p, int R){
if(l == r) return cnt[p];
int m = l+r >> ;
if(R <= m){
return Query(l, m, lson[p], R);
}
else {
return Query(m+,r,rson[p],R) + cnt[lson[p]];
}
}
int main(){
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
root[n+] = Build(, n);
for(int i = n; i >= ; i--){
if(mp[a[i]] == ){
root[i] = Update(, n, root[i+], i, );
}
else {
int tmp = Update(, n, root[i+], mp[a[i]], -);
root[i] = Update(, n, tmp, i, );
}
mp[a[i]] = i;
}
int m, l, r;
scanf("%d", &m);
for(int i = ; i <= m; i++){
scanf("%d%d", &l, &r);
printf("%d\n", Query(,n,root[l],r));
}
return ; }
SPOJ-3267
SPOJ - 3267. D-query 主席树求区间个数的更多相关文章
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- SPOJ 3267. D-query (主席树,查询区间有多少个不相同的数)
3267. D-query Problem code: DQUERY English Vietnamese Given a sequence of n numbers a1, a2, ..., an ...
- SPOJ DQUERY (主席树求区间不同数个数)
题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...
- 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) ...
- SPOJ - DQUERY 主席树求区间有多少个不同的数(模板)
D-query Time Limit: 227MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submit Status ...
- 主席树——求区间第k个不同的数字(向右密集hdu5919)
和向左密集比起来向右密集只需要进行小小的额修改,就是更新的时候从右往左更新.. 自己写的被卡死时间.不知道怎么回事,和网上博客的没啥区别.. /* 给定一个n个数的序列a 每次询问区间[l,r],求出 ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)
题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...
- 主席树——求区间[l,r]不同数字个数的模板(向左密集 D-query)
主席树的另一种用途,,(还有一种是求区间第k大,区间<=k的个数) 事实上:每个版本的主席树维护了每个值最后出现的位置 这种主席树不是以权值线段树为基础,而是以普通的线段树为下标的 /* 无修改 ...
随机推荐
- 【Docker】unauthorized: incorrect username or password
昨天朋友推荐玩玩 Docker.虽然之前就听过,但一直不清楚干嘛的,也没去搞过,虽然前段时间就装了,但一直没打开.这两天刚开始熟悉,就遇到了点小问题.一番 Google 之后解决了,记录一下. CLI ...
- 【Android Studio】常用快捷键
1. 删除一行:Ctrl + X 更新中……
- MySQL中一些关于索引的知识点
什么是索引 索引是一种数据结构,其作用就是用来提高数据查询效率.比较常用的比喻就是将其类比为书籍的目录.通过目录可以精确的找到某一章节的内容所在页. 在数据量较小的时候使用索引其实也没有什么意义,即使 ...
- golang文档、中文、学习文档
Golang中文文档地址 http://zh-golang.appspot.com/doc/ Golang非英文文档地址: https://github.com/golang/go/wiki/NonE ...
- zabbix监控WEB网站性能
一直在纠结用什么实例来给大家演示呢?想来想去还是官方的好,那我们怎么用zabbix监控web性能和可用性呢?我们这边分为几个步骤:打开网站.登陆.登陆验证.退出,一共4个小step,看实例. 检测流程 ...
- 大型系列课程之-七夕告白之旅vbs篇
也许,世间所有的美好的东西,都是需要仪式感的,遇到了一年一度的七夕节,怎么过这个节日,成了很多心中有爱的人关注的事情,七夕不浪漫,人间不值得,七夕,发源于中国,这个美好的节日,来自动人的神话故事传说牛 ...
- 【JDK】JDK源码分析-CyclicBarrier
概述 CyclicBarrier 是并发包中的一个工具类,它的典型应用场景为:几个线程执行完任务后,执行另一个线程(回调函数,可选),然后继续下一轮,如此往复. 打个通俗的比方,可以把 CyclicB ...
- Go中的异常处理
1. errors包 Go 有一个预先定义的 error 接口类型 : type error interface { Error() string } 错误值用来表示异常状态.Go也提供了一个包:er ...
- Jenkins使用aqua-microscanner-plugin进行容器漏洞扫描
官方地址:https://github.com/jenkinsci/aqua-microscanner-plugin Step1 在jenkins安装"Aqua MicroScanner&q ...
- 如何添加JWT生成的token在请求头中
前言 在我们使用JWT来做用户的验证时,我们登陆生成对应的token,并加入到请求的参数中发送到后台提供相关的权限校验.这个时候我们需要使用到传递请求头参数传递的问题,下面是两种方式. 1.ajax提 ...