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. maven的动态打包功能

    对于maven而言,打包是其一个非常重要的功能,不仅仅是简单的编译打包的概念,其还通过各种插件支持各种灵活的打包策略.现举一个例子讲解如何动态实现一个web项目的打包: 需求: 现需要对一个web项目 ...

  2. C++:__stdcall详解

    原文地址:http://www.cnblogs.com/songfeixiang/p/3733661.html 对_stdcall 的理解(上)在C语言中,假设我们有这样的一个函数:int funct ...

  3. ADC自动转接功能Lua实现

    一.背景介绍: 虽然使用Mod_fifo和mod_callcenter可以做呼叫中心的应用,但在实现应用中,这两个模块很难客制化需求,再此我用Lua实现了5路客服(1000-1004),一个呼叫中心号 ...

  4. 随机森林学习-2-sklearn

    # -*- coding: utf-8 -*- """ RandomForestClassifier skleran的9个模型在3份数据上的使用. 1. 知识点: skl ...

  5. RestTemplate -springwebclient

    1 使用jar版本 - spring-web-4.3.8.RELEASE.jar 场景:backend,post请求远端,header中加入accessToken,用于权限控制 HttpHeaders ...

  6. notepad++ 快捷键大全、notepad常用快捷键

    Notepad++ 快捷键 大全, notepad++也情有独钟,最近发现了一个快捷键,就是选中单词,ctrl+shift+enter.不过现在想知道一个快捷键,假设有三行代码,选中后一般按TAB就可 ...

  7. 测试开发之前端——No4.HTML5中的事件属性

    HTML5的事件属性. 属性 值 描述 onafterprint script 在打印文档之后运行脚本 onbeforeprint script 在文档打印之前运行脚本 onbeforeonload ...

  8. Deep Learning系统实训之三:卷积神经网络

    边界填充(padding):卷积过程中,越靠近图片中间位置的像素点越容易被卷积计算多次,越靠近边缘的像素点被卷积计算的次数越少,填充就是为了使原来边缘像素点的位置变得相对靠近中部,而我们又不想让填充的 ...

  9. 【python】中文提取,判断,分词

    参考: http://www.cnblogs.com/kaituorensheng/p/3595879.html https://github.com/fxsjy/jieba 判断是否包含中文 def ...

  10. MACE(3)-----工程化

    作者:十岁的小男孩 QQ:929994365 能下者,上. 前言 本文是MACE的第三步即MACE环境编译出来的库在Android工程中的使用.在第一篇博文中通过mace官方提供的安卓工程进行调试,本 ...