【BZOJ3524】Couriers
题面
Description
给一个长度为\(n\)的序列\(a\)。\(1\le a[i]\le n\)。
\(m\)组询问,每次询问一个区间\([l,r]\),是否存在一个数在\([l,r]\)中出现的次数大于\((r-l+1)/2\)。如果存在,输出这个数,否则输出\(0\)。
Input
第一行两个数\(n\),\(m\)。
第二行\(n\)个数,\(a[i]\)。
接下来\(m\)行,每行两个数\(l,r\),表示询问\([l,r]\)这个区间。
Output
\(m\)行,每行对应一个答案。
Sample Input
7 5
1 1 3 2 3 4 3
1 3
1 4
3 7
1 7
6 6
Sample Output
1
0
3
0
4
HINT
\(n,m\le 500000\)
分析
主席树维护权值线段树,直接在树上二分。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<climits>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<ctime>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
inline char nc(){
/*
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
*/return getchar();
}
inline void read(int &x){
char c=nc();int b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline void read(LL &x){
char c=nc();LL b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline void read(char &x){
for (x=nc();!(x=='('||x==')');x=nc());
}
inline int read(char *s)
{
char c=nc();int len=1;
for(;!(c=='('||c==')');c=nc()) if (c==EOF) return 0;
for(;(c=='('||c==')');s[len++]=c,c=nc());
s[len++]='\0';
return len-2;
}
int wt,ss[19];
inline void print(int x){
if (x<0) x=-x,putchar('-');
if (!x) putchar(48); else {
for (wt=0;x;ss[++wt]=x%10,x/=10);
for (;wt;putchar(ss[wt]+48),wt--);}
}
inline void print(LL x){
if (x<0) x=-x,putchar('-');
if (!x) putchar(48); else {for (wt=0;x;ss[++wt]=x%10,x/=10);for (;wt;putchar(ss[wt]+48),wt--);}
}
int T,n,m,s,k,h[500010],b[500010],d[500010];
struct ST
{
int lc,rc,sum;
}a[10000010];
void build(int l,int r,int x)
{
s++;x=s;
if (l==r) {a[x].lc=a[x].rc=0;return ;}
int mid=(l+r)>>1;
a[x].lc=s+1;build(l,mid,x);
a[x].rc=s+1;build(mid+1,r,x);
}
void change(int x,int y,int z,int xx,int l,int r) //新结点编号,,,对应结点编号,[l,r]
{
if (l==r) {a[x].sum=a[xx].sum+z;return ;}
int mid=(l+r)>>1;
if (y<=mid)
{
a[x].rc=a[xx].rc;
s++;a[x].lc=s;
change(s,y,z,a[xx].lc,l,mid);
}
else
{
a[x].lc=a[xx].lc;
s++;a[x].rc=s;
change(s,y,z,a[xx].rc,mid+1,r);
}
a[x].sum=a[a[x].lc].sum+a[a[x].rc].sum;
}
int query(int x,int y,int z,int l,int r)
{
//printf("%d %d %d %d\n",l,r,z,a[y].sum-a[x].sum);
if (l==r)
{
if (a[y].sum-a[x].sum>z) return b[l];
else return 0;
}
if (a[a[y].lc].sum-a[a[x].lc].sum>z) return query(a[x].lc,a[y].lc,z,l,(l+r)>>1);
else if (a[a[y].rc].sum-a[a[x].rc].sum>z) return query(a[x].rc,a[y].rc,z,((l+r)>>1)+1,r);
else return 0;
}
int main()
{
read(n);read(m);
memset(a,0,sizeof(a));
for (int i=1;i<=n;i++)
read(d[i]),b[i]=i;
s=0;
build(1,n,1);
h[0]=1;
for (int i=1;i<=n;i++)
{
s++;h[i]=s;
change(s,d[i],1,h[i-1],1,n);
}
int x,y,z;
while(m--)
{
read(x);read(y);
print(query(h[x-1],h[y],(y-x+1)/2,1,n));putchar('\n');
}
return 0;
}
【BZOJ3524】Couriers的更多相关文章
- 【BZOJ3524】Couriers(主席树)
题意:给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. n,m≤5000 ...
- 【BZOJ3524】 [Poi2014]Couriers
Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. ...
- 【bzoj3524】【Poi2014】【Couriers】可持久化线段树(主席树)水题
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62485671 向大(hei)佬(e)势力学(di ...
- 【bzoj3524】[Poi2014]Couriers 主席树
题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...
- 【BZOJ 3524】【Poi2014】Couriers 可持久化线段树
为什么这个主席树叫可持久化线段树,我不知道,具体得问达神.我无限T,然后DaD3zZ一针见血地指出了我的N*50爆内存导致无限编译超时O)ZO)ZO)Z真是太神啦.以图为鉴: 达神题解传送门:http ...
- 【BZOJ】【3524】【POI2014】Couriers
可持久化线段树 裸可持久化线段树,把区间第K大的rank改成num即可……(往儿子走的时候不减少) 苦逼的我……MLE了一次(N*30),RE了一次(N*10)……数组大小不会开…… 最后开成N*20 ...
- 【BZOJ3524/2223】[Poi2014]Couriers 主席树
[BZOJ3524][Poi2014]Couriers Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大 ...
- 【BZOJ】3524: [Poi2014]Couriers
[算法]主席树 [题解]例题,记录和,数字出现超过一半就递归查找. 主席树见[算法]数据结构 #include<cstdio> #include<algorithm> #inc ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
随机推荐
- CSS简易学习笔记
学习地址:http://www.w3school.com.cn/css/index.asp cnblog不能把格式复制上来,有格式文字版:https://github.com/songzhenhua/ ...
- ansible自动部署Keepalived实现Nginx服务双机热备
脚本实现通过ansible-playbook自动化安装Keepalived和配置,主要解决问题如下: Keepalived自动化安装: keepalived_vrid配置,自动根据vip获取最后一段作 ...
- day06_03 购物车讲解01
1.0 思路 1.1 列表嵌套 # name1 = ['mac','book','bike','kindle'] a = [[1,2,3],'alex',(2,3,4)] print(a[0]) #& ...
- 【java并发编程实战】第一章笔记
1.线程安全的定义 当多个线程访问某个类时,不管允许环境采用何种调度方式或者这些线程如何交替执行,这个类都能表现出正确的行为 如果一个类既不包含任何域,也不包含任何对其他类中域的引用.则它一定是无状态 ...
- 孤荷凌寒自学python第十四天python代码的书写规范与条件语句及判断条件式
孤荷凌寒自学python第十四天python代码的书写规范与条件语句及判断条件式 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 在我学习过的所有语言中,对VB系的语言比较喜欢,而对C系和J系 ...
- JAVA相似算法的运用
今天要处理问题是把一个产品的名字按照其内容对比文档转换出它的中文名. 但是这个文档感觉不全,产品种类有多又杂. 如果像这样写的话 if(xxx.contains()) else if() ... 不知 ...
- jsp处理
jsp处理步骤: 浏览器发送一个HTTP请求给服务器. Web服务器识别出这是一个对JSP网页的请求,并且将该请求传递给JSP引擎.通过使用URL或者.jsp文件来完成. JSP引擎从磁盘中载入JSP ...
- CentOS Linux上搭建PPPoE服务器及拨号设置
CentOS Linux上搭建PPPoE服务器及拨号设置 搭建PPPoE,成功了的话,就觉得超级简单,在CentOS Linux更是5步左右就能搞定. 1.安装pppoe,安装完成后,会有pppoe- ...
- [洛谷P3805]【模板】manacher算法
题目大意:给你一个字符串,求出它的最长回文字段 题解:$manacher$算法 卡点:$p$数组未开两倍空间 C++ Code: #include <cstdio> #include &l ...
- codeforces 834 D. The Bakery
codeforces 834 D. The Bakery(dp + 线段树优化) 题意: 给一个长度为n的序列分成k段,每段的值为这一段不同数字的个数,最大化划分k端的值 $n <= 35000 ...