SPOJ - 3267

主席树的又一种写法。 从后端点开始添加主席树, 然后如果遇到出现过的元素先把那个点删除, 再更新树, 最后查询区间就好了。

 #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 主席树求区间个数的更多相关文章

  1. SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  2. SPOJ 3267. D-query (主席树,查询区间有多少个不相同的数)

    3267. D-query Problem code: DQUERY English Vietnamese Given a sequence of n numbers a1, a2, ..., an  ...

  3. SPOJ DQUERY (主席树求区间不同数个数)

    题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...

  4. 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) ...

  5. SPOJ - DQUERY 主席树求区间有多少个不同的数(模板)

    D-query Time Limit: 227MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submit Status ...

  6. 主席树——求区间第k个不同的数字(向右密集hdu5919)

    和向左密集比起来向右密集只需要进行小小的额修改,就是更新的时候从右往左更新.. 自己写的被卡死时间.不知道怎么回事,和网上博客的没啥区别.. /* 给定一个n个数的序列a 每次询问区间[l,r],求出 ...

  7. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)

    题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...

  9. 主席树——求区间[l,r]不同数字个数的模板(向左密集 D-query)

    主席树的另一种用途,,(还有一种是求区间第k大,区间<=k的个数) 事实上:每个版本的主席树维护了每个值最后出现的位置 这种主席树不是以权值线段树为基础,而是以普通的线段树为下标的 /* 无修改 ...

随机推荐

  1. ZK安装、ZK配置、ZK集群部署

    今天心血来潮,想搞一下zookeeper集群.具体步骤记录下吧~嘻嘻

  2. memcached.c 源码分析

    上文分析了memcached的autoconf过程以及configure, make过程,可以看到,memcached可执行文件是由memcached-memcached.o以及其他文件连接后编译出来 ...

  3. Intent 常用方法总结

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本文主要是总结Intent 常用的方法,并封装成Utils类中 主要涉及以下内容 ...

  4. FTP服务端部署

    FTP服务端搭建(本地用户登入:使用本地用户和密码登入)1.文件配置:vsftpd.conf: 主配置文件ftpusers: 指定哪些用户不能访问FTP服务器user_list: 指定的用户是否可以访 ...

  5. PCB学习总结

    一.电子设计流程概述:项目立项------元件创库----------原理图设计-----------封装绘制-------pcb设计--------生产文件的输出--------pcb文件加工 二. ...

  6. API开发之接口安全(二)-----sign校验

    上一章 我们说了 sign的生成 那么 我们如何确定这个sign的准确性呢 下来 我们说说 校验sign的那些事 在拿到header里面的内容之后 我们首先需要对其内容的基本参数做一个校验 我们补充下 ...

  7. 中间件增强框架之InterceptFramework

    本文讲解MOF中的InterceptFramework框架.该框架可以在应用启动过程中获取画像信息,实现应用画像数据采集和存储. 一.前言 在智能运维中,应用服务所使用的组件及JAR包等相关信息非常重 ...

  8. 从0到1发布一个npm包

    从0到1发布一个npm包 author: @TiffanysBear 最近在项目业务中有遇到一些问题,一些通用的方法或者封装的模块在PC.WAP甚至是APP中都需要使用,但是对于业务的PC.WAP.A ...

  9. windows--OSError: [Errno 22] Invalid argument: '\u202aE:/desk/Desktop/test.txt' 读取文件的坑

    准备打开文件时,报了如下错误: 在路径中出现了这个Unicode 202a字符,导致了这个错误. 这玩意是哪里来的? 复制windows文件属性的时候复制下图中的路径而来的. 解释: 这个字符的含义是 ...

  10. .NET中的缓存

    构建高性能的应用程序的非常重要一项就是使用缓存.使用缓存可以避免重新从读取服务器端读取数据,节省数据从客户端到服务器间往返的时间,同时也减轻了服务器数据存取的压力.如果客户端非常频繁地读取服务器上的数 ...