OJ题号:BZOJ3524、BZOJ2223、洛谷3567

思路:

维护一颗可持久化权值线段树,记录每次加入数字时,不同数字出现的个数。
对于每一个询问$[l,r]$,同时查询以$r$和$l-1$为根的线段树,每次比较两个节点左右字子树的权值和,如果大于$[l,r]$区间的一半就说明这一子区间可能有答案,递归查询即可。

 #include<cstdio>
#include<cctype>
#include<cstring>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=,SZ=;
class FotileTree {
private:
int val[SZ],sz,left[SZ],right[SZ];
int newnode() {
return sz++;
}
public:
FotileTree() {
sz=;
memset(val,,sizeof val);
}
int root[N];
int build(const int b,const int e) {
int new_p=newnode();
if(b==e) return new_p;
int mid=(b+e)>>;
left[new_p]=build(b,mid);
right[new_p]=build(mid+,e);
return new_p;
}
int modify(const int p,const int b,const int e,const int x) {
int new_p=newnode();
val[new_p]=val[p]+;
if(b==e) return new_p;
int mid=(b+e)>>;
if(x<=mid) left[new_p]=modify(left[p],b,mid,x),right[new_p]=right[p];
if(x>mid) right[new_p]=modify(right[p],mid+,e,x),left[new_p]=left[p];
return new_p;
}
int query(const int p1,const int p2,const int b,const int e,const int k) {
if(b==e) return b;
int mid=(b+e)>>;
if(val[left[p2]]-val[left[p1]]>k) return query(left[p1],left[p2],b,mid,k);
if(val[right[p2]]-val[right[p1]]>k) return query(right[p1],right[p2],mid+,e,k);
return ;
}
};
FotileTree t;
int main() {
int n=getint(),m=getint();
t.root[]=t.build(,n);
for(int i=;i<=n;i++) t.root[i]=t.modify(t.root[i-],,n,getint());
while(m--) {
int l=getint(),r=getint();
printf("%d\n",t.query(t.root[l-],t.root[r],,n,(r-l+)>>));
}
return ;
}

[POI2014]Couriers的更多相关文章

  1. BZOJ 3524: [Poi2014]Couriers [主席树]

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

  2. BZOJ 3524: [Poi2014]Couriers

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

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

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

  4. 3524: [Poi2014]Couriers -- 主席树

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MB Description 给一个长度为n的序列a.1≤a[i]≤n.m组 ...

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

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

  6. 主席树||可持久化线段树||BZOJ 3524: [Poi2014]Couriers||BZOJ 2223: [Coci 2009]PATULJCI||Luogu P3567 [POI2014]KUR-Couriers

    题目:[POI2014]KUR-Couriers 题解: 要求出现次数大于(R-L+1)/2的数,这样的数最多只有一个.我们对序列做主席树,每个节点记录出现的次数和(sum).(这里忽略版本差值问题) ...

  7. Bzoj3524 [Poi2014]Couriers

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

  8. BZOJ 3542 [Poi2014]Couriers ——可持久化线段树

    [题目分析] 查找区间内出现次数大于一半的数字. 直接用主席树,线段树上维护区间大小,由于要求出现次数大于一半,每到一个节点可以分治下去. 时间复杂度(N+Q)logN [代码] #include & ...

  9. 【BZOJ3524】 [Poi2014]Couriers

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

  10. 【bzoj 3524】[Poi2014]Couriers

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

随机推荐

  1. XmlDocument根据节点的属性值获取节点

    string targetParm = string.Format("STUDENTS/STUDENT[@NO='{0}']", targetValue);//生成目标获取节点的参 ...

  2. 转 利用 Console 来学习、调试JavaScript

    利用 Console 来学习.调试JavaScript   一  什么是 Console Console 是用于显示 JS和 DOM 对象信息的单独窗口.并且向 JS 中注入1个 console 对象 ...

  3. Vue源码

    参考文章:http://hcysun.me/2017/03/03/Vue%E6%BA%90%E7%A0%81%E5%AD%A6%E4%B9%A0/?utm_source=qq&utm_medi ...

  4. 温故而知新--JavaScript书摘(三)

    前言 毕业到入职腾讯已经差不多一年的时光了,接触了很多项目,也积累了很多实践经验,在处理问题的方式方法上有很大的提升.随着时间的增加,愈加发现基础知识的重要性,很多开发过程中遇到的问题都是由最基础的知 ...

  5. STL整理之set

    转载请注明出处,部分内容引自李煜东<算法竞赛进阶指南> 前置知识:    C++.C语言入门 Set是什么 Set是C++STL中提供的容器,set是数学上的集合——具有唯一性,即每个元素 ...

  6. python接口自动化测试二十九:yaml配置文件的写和读

    # 先安装ruamel.yaml模块 写入配置文件: import os# 先安装ruamel.yaml模块from ruamel import yaml # 将字典写入到yamldict = { ' ...

  7. python接口自动化测试十一:传参数:data与json

    # 传json参数 import requests url = 'xxxxxxxx' body = {     'xxx': 'xxx',     'xxx': 'xxx' } # body是json ...

  8. 步步为营-12-Dictionary-翻译

    说明:https://pan.baidu.com/s/1nvPqhDJ所需文件在此目录下对应的位置 1 先做一个简单的英汉翻译词典.先搭UI页面 2 将百度网盘中提供的资料放置到bin\debug目录 ...

  9. selenium+python谷歌驱动配置

    1.打开chrome 输入 “chrome://version/”来查看chrome版本 2.访问此网站  http://chromedriver.storage.googleapis.com/ind ...

  10. 异构平台同步(mysql-->oracle)

    https://www.cnblogs.com/andy6/p/6159060.html