题目

给一个长度为n的序列a。1≤a[i]≤n。

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

输入格式

第一行两个数n,m。

第二行n个数,a[i]。

接下来m行,每行两个数l,r,表示询问[l,r]这个区间。

输出格式

m行,每行对应一个答案。

输入样例

7 5

1 1 3 2 3 4 3

1 3

1 4

3 7

1 7

6 6

输出样例

1

0

3

0

4

提示

【数据范围】

n,m≤500000

2016.7.9重设空间,但未重测!

题解

主席树板题

我都不好意思拿出来写博客

#include<iostream>
#include<cstdio>
#include<algorithm>
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define lbt(x) (x & -x)
using namespace std;
const int maxn = 500005,maxm = 10000005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
return out * flag;
}
int rt[maxn],siz = 0,n,m,sum[maxm],ls[maxm],rs[maxm];
void update(int& u,int pre,int l,int r,int pos){
sum[u = ++siz] = sum[pre] + 1;
if (l == r) return;
ls[u] = ls[pre]; rs[u] = rs[pre];
int mid = l + r >> 1;
if (mid >= pos) update(ls[u],ls[pre],l,mid,pos);
else update(rs[u],rs[pre],mid + 1,r,pos);
}
int query(int u,int v,int l,int r,int len){
if (l == r) return sum[u] - sum[v] > len ? l : 0;
int mid = l + r >> 1,t = sum[ls[u]] - sum[ls[v]];
if (t > len) return query(ls[u],ls[v],l,mid,len);
else return query(rs[u],rs[v],mid + 1,r,len);
}
int main(){
n = read(); m = read();
REP(i,n) update(rt[i],rt[i - 1],1,n,read());
while (m--){
int l = read(),r = read();
printf("%d\n",query(rt[r],rt[l - 1],1,n,(r - l + 1) >> 1));
}
return 0;
}

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/2223】[Poi2014]Couriers 主席树

    [BZOJ3524][Poi2014]Couriers Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[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. BZOJ 3524: [Poi2014]Couriers [主席树]

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

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

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

  8. BZOJ2223/3524:[POI2014] Couriers(主席树)

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

  9. BZOJ_3524_[Poi2014]Couriers_主席树

    BZOJ_3524_[Poi2014]Couriers_主席树 题意:给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r- ...

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

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

随机推荐

  1. 安装mysql提示This application requires .NET framework 4.0.

    问题描述:安装MySQL社区版时遇到This application requires .NET framework 4.0. 解决方法:在http://search.microsoft.com/zh ...

  2. HDU 2045 LELE的RPG难题

    递推 枚举起点状态 #include <algorithm> #include <iostream> #include <cstring> #include < ...

  3. 正则表达式通用匹配ip地址及主机检测

    在使用正则表达式匹配ip地址时如果不限定ip正确格式,一些场景下可能会产生不一样的结果,比如ip数值超范围,ip段超范围等,在使用正则表达式匹配ip地址时要注意几点: 1,字符界定:使用  \< ...

  4. k8s的secret基本概念及案例

    secret相对于configMap,功能上是相似的但是secret是以其他编码方式去记录配置信息的,但是也可以被解读,只不过有技术门槛,不是那么容易就被解读.使用base64可以解码:echo ** ...

  5. python从入门到精通之30天快速学python视频教程

    点击了解更多Python课程>>> python从入门到精通之30天快速学python视频教程 课程目录: python入门教程-1-Python编程语言历史及特性.mkv pyth ...

  6. Mysql关闭和修改密码

    数据库的关闭方法: 1.优雅的关闭数据库的方法:mysqladmin -uroot -p123456 shutdown 2.脚本关闭:/etc/init.d/mysqld stop 3.使用kill信 ...

  7. Class:向传统类模式转变的构造函数

    前言 JS基于原型的'类',一直被转行前端的码僚们大呼惊奇,但接近传统模式使用class关键字定义的出现,却使得一些前端同行深感遗憾而纷纷留言:"还我独特的JS"."净搞 ...

  8. Python学习笔记(五)之Python操作Redis、mysql、mongodb数据库

    操作数据库 一.数据库 数据库类型主要有关系型数据库和菲关系型数据库. 数据库:用来存储和管理数的仓库,数据库是通过依据“数据结构”将数据格式化,以记录->表->库的关系存储.因此数据查询 ...

  9. Watchmen CodeForces - 650A

    Watchmen CodeForces - 650A Watchmen are in a danger and Doctor Manhattan together with his friend Da ...

  10. ACM-ICPC 2018 徐州赛区网络预赛

    A. Hard to prepare #include <bits/stdc++.h> using namespace std; ; ]; ]; int main() { int T; i ...