P3567 [POI2014]KUR-Couriers

P3567 [POI2014]KUR-Couriers

题目描述

Byteasar works for the BAJ company, which sells computer games.

The BAJ company cooperates with many courier companies that deliver the games sold by the BAJ company to its customers.

Byteasar is inspecting the cooperation of the BAJ company with the couriers.

He has a log of successive packages with the courier company that made the delivery specified for each package.

He wants to make sure that no courier company had an unfair advantage over the others.

If a given courier company delivered more than half of all packages sent in some period of time, we say that it dominated in that period.

Byteasar wants to find out which courier companies dominated in certain periods of time, if any.

Help Byteasar out!

Write a program that determines a dominating courier company or that there was none.

给一个数列,每次询问一个区间内有没有一个数出现次数超过一半

输入输出格式

输入格式:

 

The first line of the standard input contains two integers,  and  (), separated by a single space, that are the number of packages shipped by the BAJ company and the number of time periods for which the dominating courier is to be determined, respectively.

The courier companies are numbered from  to (at most) .

The second line of input contains  integers,  (), separated by single spaces;  is the number of the courier company that delivered the -th package (in shipment chronology).

The  lines that follow specify the time period queries, one per line.

Each query is specified by two integers,  and  (), separated by a single space.

These mean that the courier company dominating in the period between the shipments of the -th and the -th package, including those, is to be determined.

In tests worth  of total score, the condition  holds, and in tests worth  of total score .

 

输出格式:

 

The answers to successive queries should be printed to the standard output, one per line.

(Thus a total of  lines should be printed.) Each line should hold a single integer: the number of the courier company that dominated in the corresponding time period, or  if there was no such company.

 

输入输出样例

输入样例#1: 复制

7 5
1 1 3 2 3 4 3
1 3
1 4
3 7
1 7
6 6

输出样例#1: 复制

1
0
3
0
4

说明

给一个数列,每次询问一个区间内有没有一个数出现次数超过一半 n,m≤500000

 

写的第二个主席树题目了 一样还是模板题

询问区间有没有一个数出现的次数超过一半

和普通的主席树的差别在于 查找函数中 普通的主席树不是在左边就是在右边 但是这个题目中 多一种情况 没有这个点

所以要多一点点

int query(int u,int v,int l,int r,int k)
{
	if(l==r) return l;
	int x=sum[lc[v]]-sum[lc[u]];//左边
	int y=sum[rc[v]]-sum[rc[u]];//右边
	int mid=(l+r)/2;
	if(x>k) return query(lc[u],lc[v],l,mid,k);//在左边
	else if(y>k) return query(rc[u],rc[v],mid+1,r,k);//在右边
	else return 0;//不存在这个点
}

话不多说 上代码:

#include<bits/stdc++.h>
using namespace std;
#define M 500005<<5
int n,m,cnt,p,q,a[M],b[M],rc[M],lc[M],sum[M],l,r,tt[M];
void build(int &t,int l,int r)
{
	t=++cnt;
	if(l==r) return;
	int mid=(l+r)/2;
	build(lc[t],l,mid);
	build(rc[t],mid+1,r);
}//建空树 
int update(int o,int l,int r,int p)
{
	int oo=++cnt;
	lc[oo]=lc[o];
	rc[oo]=rc[o];
	sum[oo]=sum[o]+1;
	if(l==r) return oo;
	int mid=(l+r)/2;
	if(p<=mid)	lc[oo]=update(lc[oo],l,mid,p);
	else rc[oo]=update(rc[oo],mid+1,r,p);
	return oo;
}
int query(int u,int v,int l,int r,int k)
{
	if(l==r) return l;
	int x=sum[lc[v]]-sum[lc[u]];//左边
	int y=sum[rc[v]]-sum[rc[u]];//右边
	int mid=(l+r)/2;
	if(x>k) return query(lc[u],lc[v],l,mid,k);//在左边
	else if(y>k) return query(rc[u],rc[v],mid+1,r,k);//在右边
	else return 0;//不存在这个点
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) 
	{
		scanf("%d",&a[i]);
		b[i]=a[i];
	}
	sort(b+1,b+n+1);
	q=unique(b+1,b+n+1)-b-1;
	build(tt[0],1,q);
	for(int i=1;i<=n;i++)
	{
		p=lower_bound(b+1,b+q+1,a[i])-b;
		tt[i]=update(tt[i-1],1,q,p);
	}
	while(m--)
	{
		scanf("%d%d",&l,&r);
		printf("%d\n",b[query(tt[l-1],tt[r],1,q,(r-l+1)/2)]);
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值