题目链接

题意:n个数 m个查询

查询的是[l, r]区间内不相同的数的个数

没有修改,因此静态的主席树就好了

将重复的元素建树即可

query的时候加起来,用区间长度(r-l+1)去减就是答案

(query的是[l, r]之间重复元素的个数)

 typedef long long LL;
#define lson l, m
#define rson m+1, r
const int N=;
int L[N<<], R[N<<], sum[N<<];
int tot;
int a[N], T[N];
int read()
{
char ch=' ';
int ans=;
while(ch<'' || ch>'')
ch=getchar();
while(ch<='' && ch>='')
{
ans=ans*+ch-'';
ch=getchar();
}
return ans;
} int build(int l, int r)
{
int rt=(++tot);
sum[rt]=;
if(l<r)
{
int m=(l+r)>>;
L[rt]=build(lson);
R[rt]=build(rson);
}
return rt;
} int update(int pre, int l, int r, int x)
{
int rt=(++tot);
L[rt]=L[pre], R[rt]=R[pre], sum[rt]=sum[pre]+;
if(l<r)
{
int m=(l+r)>>;
if(x<=m)
L[rt]=update(L[pre], lson, x);
else
R[rt]=update(R[pre], rson, x);
}
return rt;
} int query(int u, int v, int l, int r, int k)
{
if(l>=k)
return sum[v]-sum[u];
int m=(l+r)>>;
int ans=;
if(m>=k)
ans+=query(L[u], L[v], lson, k);
ans+=query(R[u], R[v], rson, k);
return ans;
} int main()
{
tot=;
int n=read();
// scanf("%d", &n);
for(int i=; i<=n; i++)
{
// scanf("%d", &a[i]);
a[i]=read();
}
T[]=;
map<int, int> mp;
mp.clear();
for(int i=; i<=n; i++)
{
if(mp.find(a[i])!=mp.end())
T[i]=update(T[i-], , n, mp[a[i]]);
else
T[i]=T[i-];
mp[a[i]]=i;
}
int m=read();
// scanf("%d", &m);
while(m--)
{
int l, r;
l=read(), r=read();
// scanf("%d%d", &l, &r);
printf("%d\n", r-l+-query(T[l-], T[r], , n, l));
}
}
/*
5
1 1 2 1 3
3
1 5
2 4
3 5
*/

SPOJ DQUERY

[主席树]SPOJ DQUERY的更多相关文章

  1. SPOJ DQUERY树状数组离线or主席树

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

  2. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

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

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

  4. SPOJ DQUERY D-query (在线主席树/ 离线树状数组)

    版权声明:本文为博主原创文章,未经博主允许不得转载. SPOJ DQUERY 题意: 给出一串数,询问[L,R]区间中有多少个不同的数 . 解法: 关键是查询到某个右端点时,使其左边出现过的数都记录在 ...

  5. SPOJ 3267 D-query(离散化+在线主席树 | 离线树状数组)

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

  6. SPOJ - DQUERY 主席树

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32356 Given a sequence of n numbers ...

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

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

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

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

随机推荐

  1. 浅析 GRUB 如何加载 linux kernel

    前言 对于 GRUB 的加载流程,网上绝大部分都是写对 menu.lst, grub.cfg 这些 GRUB 配置文件的编写流程,就像是写脚本语言一样,用些关键字就能让 PC机能正确启动桌面 Linu ...

  2. iframe整理学习笔记

    朋友问了一个比较怪的问题,iframe下自适应的问题,因为很少使用iframe的原因,怀着对iframe的疑惑采用了一点点实践;以下frame表示针对的iframe元素 解决的方法:对iframe进行 ...

  3. Nginx,LVS,HAProxy,负载均衡之选择

    Nginx的优点:性能好,可以负载超过1万的并发.功能多,除了负载均衡,还能作Web服务器,而且可以通过Geo模块来实现流量分配.社区活跃,第三方补丁和模块很多支持gzip proxy缺点:不支持se ...

  4. PHP获取当前日期或指定日期的所在月的第一天和最后一天

    <?php function getthemonth($date) { $firstday = date('Y-m-01', strtotime($date)); $lastday = date ...

  5. ros的源码阅读

    测试代码,使用xmlrpc与roscore通信 ros的框架是使用rpc与server端通信,server维护topic的publisher,subscriber,param server,servi ...

  6. uImage、zImage、bzImage、vlinzx区别

    在网络中,不少服务器采用的是Linux系统.为了进一步提高服务器的性能,可能需要根 据特定的硬件及需求重新编译Linux内核.编译Linux 内核,需要根据规定的步骤进行,编译内核过程中涉及到几个重要 ...

  7. 扒一扒各大电商网站的m站都用的什么前端技术输入日志标题

    凡客首页使用Swiper和zepto,没有使用jquery , 静态首页+js交互,  资源加载使用 lazyLoad X-AspNet-Version: 4.0.30319 X-AspNetMvc- ...

  8. 从零开始学ios开发(五):IOS控件(2),Slider

    下面继续学习ios的其他控件,这次会使用到的控件有Slider,当然还有一些之前已经使用过的控件Label. 这次我们不新建一个project了,当然如果你愿意重新创建一个新的项目也完全可以,我们还是 ...

  9. advance 模板 怎么生成module

    advance 模板 怎么生成module namespace写什么如果是前台呢就是 frontend\modules\modulename\Module@我叫红领巾 module id有什么用bak ...

  10. 在Windows下忘记MySQL最高用户权限密码的解决方案

    1.打开MySQL配置文件 my.ini中,添加上skip-grant-tables,可以添加到文件的末尾或者是这添加到[mysqld]的下面(直接添加在my.ini文件最后亲测可以,但是在[mysq ...