【题目大意】

给一个长度为n的序列a。1≤a[i]≤n。
m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2。如果存在,输出这个数,否则输出0。

【思路】

只有query部分需要稍作修改。由于每个节点代表的是一个大小区间数的总数,所以只需判断左右子数的sum值是否大于(r-l+1)/2,满足继续往左右子树查询。没有满足条件的就返回0。由于当前的节点的sum值一定是大于(r-l+1)/2的,如果l==r直接返回l即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define lson l,m
#define rson m+1,r
using namespace std;
const int MAXN=+;
int n,m,d,a[MAXN],hash[MAXN],tot;
int T[MAXN],L[MAXN<<],R[MAXN<<],sum[MAXN<<]; 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 lrt,int rrt,int l,int r,int k)
{
if (l==r) return l;
int m=(l+r)>>;
if (sum[L[rrt]]-sum[L[lrt]]>k) return query(L[lrt],L[rrt],lson,k);
if (sum[R[rrt]]-sum[R[lrt]]>k) return query(R[lrt],R[rrt],rson,k);
return ;
} void init()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) scanf("%d",&a[i]),hash[i]=a[i];
sort(hash+,hash+n+);
d=unique(hash+,hash+n+)-(hash+); tot=;
T[]=build(,d);
for (int i=;i<=n;i++)
{
int x=lower_bound(hash+,hash+d+,a[i])-hash;
//注意离散化之后是从1开始,所以减去的是hash而不是hash+1
T[i]=update(T[i-],,d,x);
}
} void solve()
{
for (int i=;i<m;i++)
{
int l,r;
scanf("%d%d",&l,&r);
int ans=query(T[l-],T[r],,d,(r-l+)>>);
printf("%d\n",hash[ans]);
}
} int main()
{
init();
solve();
return ;
}

【主席树】BZOJ3524-[Poi2014]Couriers的更多相关文章

  1. [BZOJ2223][BZOJ3524][Poi2014]Couriers 主席树

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2436  Solved: 960[Submit][St ...

  2. BZOJ3524[Poi2014]Couriers——主席树

    题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...

  3. BZOJ3524: [Poi2014]Couriers(主席树)

    题意 题目链接 Sol 严格众数只会出现一次,那么建出主席树,维护子树siz,直接在树上二分即可 #include<bits/stdc++.h> #define LL long long ...

  4. bzoj3524: [Poi2014]Couriers(主席树)

    主席树(可持久化权值线段树)初探... 修改一个点只对树上logn个点有影响,所以新建logn个点就行了,总共新建mlogn个点. 查询一个区间[l,r],相当于将数一个一个加进树,询问第l到第r次操 ...

  5. BZOJ3524 [Poi2014]Couriers 【主席树】

    题目 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入格式 第一 ...

  6. bzoj3524 [Poi2014]Couriers/2223 [Coci 2009]PATULJCI

    题目链接1 题目链接2 主席树模板题 两题有细节不同 #include<algorithm> #include<iostream> #include<cstdlib> ...

  7. 主席树初探--BZOJ3524: [Poi2014]Couriers

    n<=500000个数,m<=500000个询问,每次问区间里出现次数>(R-L+1)的数字是谁,没有输出0. 写了带修改发现不会不带修改了.... 不带修改的话,n个点,每个点表示 ...

  8. Bzoj3524 [Poi2014]Couriers

    Description 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0 ...

  9. 【BZOJ3524/2223】[Poi2014]Couriers 主席树

    [BZOJ3524][Poi2014]Couriers Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大 ...

随机推荐

  1. Use JPath but not recursively loop a JObject to modify the values.

    I am dealing with a Json file, I parsed it into jObject, I have another list which flattened the pro ...

  2. CodeForces - 1004B

    Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she ...

  3. hash算法搜索获得api函数地址的实现,"kernel32.dll", "CreateThread"

    我们一般要获得一个函数的地址,通常采用的是明文,例如定义一个api函数字符串"MessageBoxA",然后在GetProcAddress函数中一个字节一个字节进行比较.这样弊端很 ...

  4. 链接 DB App.config 解析

    <?xml version="1.0" encoding="utf-8"?><configuration> <startup> ...

  5. ubuntu16.04 安装 python3.6, 并创建虚拟环境(使用python3.6)

    ubuntu16.04 安装 python3.6, 并创建虚拟环境(使用python3.6) ubuntu16.04中默认安装了 python2.7 python3 python3.5.2 (注意 : ...

  6. EF – 8.多对多关联

    5.6.10 <多对多关联(上)> 时长:9分57秒 难度:难 5.6.11<多对多关联(下)> 时长:8分50秒 难度:难 如果单独地把多对多关联的CRUD拿出来讲,确实比较 ...

  7. 用tomcat配置https自签名证书,解决 ios7.1以上系统, 苹果inHouse发布

    用tomcat配置https自签名证书,解决 ios7.1以上系统苹果inHouse发布不能下载安装的问题教程,话说,我其实最讨厌配置某某环境了,因为某一个小环节一旦出错,你的所有工作往往会功亏一篑, ...

  8. ci框架hook钩子

    <code>//启动hooks //app/config/config.php $config['enable_hooks'] = TRUE; //hooks配置 ///app/confi ...

  9. 关于Web2.0概念的一篇小短文

    Web2.0程序设计的第一篇作业,写了就顺手放上来吧. 在互联网泡沫破裂数年后,Tim O'Reilly与John Battelle总结了互联网产业复兴过程中出现的一系列现象,在2004年举办的第一届 ...

  10. jquery datatable的详细用法

    1,首先需要引用下面两个文件 <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css ...