题目链接

题意: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. ORACLE 语句关联统计

    很久不用SQL语句了,貌似入职新公司后,又回归到了三年前的SQL时代,一写一坨的SQL好吧,也当回归一下过去的知识. 下面是统计2月份某数据的计费统计 select t.telno as 主号,VID ...

  2. Winform webBrowser 不跳转网页

    private void webBrowser1_NewWindow(object sender, CancelEventArgs e) { string url = ((WebBrowser)sen ...

  3. canvas 绘点图

    canvas 绘点图 项目中需要一个记录点实时变动的信息,在此记录一下: <!DOCTYPE html> <html lang="en"> <head ...

  4. HBase多条件筛选查询方案

    最近的项目需要使用Hbase做实时查询,由于Hbase只支持一级索引,也就是使用rowkey作为索引查询,所以对于多条件筛选查询的支持不够,在不建立二级索引的情况下,只能使用Hbase API中提供的 ...

  5. Linux内核学习方法

    Makefile不是Make Love 从前在学校,混了四年,没有学到任何东西,每天就是逃课,上网,玩游戏,睡觉.毕业的时候,人家跟我说Makefile我完全不知,但是一说Make Love我就来劲了 ...

  6. Sybase ASE报错:server Error: 8242, Severity: 16, State: 1

    昨天上午,同事反映某系统在执行存储过程的过程中报错了,报错的信息异常如下: 05:00000:00009:2014/06/09 15:45:30.34 server Error: 8242, Seve ...

  7. ios自定义选择器ActionSheetPicker改进版

    ios自带的UIDataPicker和UIDatePicker最大的毛病就是没有带确定和取消这两个按钮,而ActionSheetPicker是以上两个选择器的开源封装.但是这个东东也有些小问题,就是没 ...

  8. Most People Aren’t.

    Most people want to be fit, most people aren't. Most people want to build a successful business, mos ...

  9. ASP.NET Web - 服务器控件

    控件 HTML 说明 Label <span> 返回一个包含文本的span元素 Literal static text 返回简单的静态文本.使用Literal控件,可以根据客户应用程序转换 ...

  10. 让CALayer的shadowPath跟随bounds一起做动画改变-b

    在iOS开发中,我们经常需要给视图添加阴影效果,最简单的方法就是通过设置CALayer的shadowColor.shadowOpacity.shadowOffset和shadowRadius这几个属性 ...