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

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


对,题目很简单,给一个数列,每次询问一个区间内有没有一个数出现次数超过一半

很明显是主席树的板子题

首先把数字一个一个插到树中,一个建一次;

那么对于区间 [ L , R ] [L , R] [L,R],我们只需要看在第 L L L次到第 R R R次中加的元素,就是说用第 R R R次建的减去第 L − 1 L-1 L1次建的树的值,

怎么判断呢?

其实很简单:先看树的左半部分,如果数出现的次数小于 ( L + R ) / 2 (L+R)/2 (L+R)/2,那么一定不在这半部分,右边也类似,就这么递归的查找

哦,如果两边都不行,就说明不存在这样的数,直接返回 0 0 0就可以了

	int o=tree[tree[R].lc].ans-tree[tree[L].lc].ans;
	int O=tree[tree[R].rc].ans-tree[tree[L].rc].ans;
	if(o+o>k)return query(tree[L].lc,tree[R].lc,l,mid,k);//k是区间长度,即(L+R)/2
	if(O+O>k)return query(tree[L].rc,tree[R].rc,mid+1,r,k);

核心代码只有这些,其他的就只是板子啦

顺带一提,此题的数据范围应是 500000 500000 500000,题目中没有提到 ( ( (第一次开了 9500000 9500000 9500000的主席树竟然 R E RE RE Q A Q ) QAQ) QAQ)

贴上完整的:
#include<iostream>
#include<cstdio>
#include<ctype.h>
using namespace std;
inline int read(){
	int x=0,f=0;char ch=getchar();
	while(!isdigit(ch))f|=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return f?-x:x;
}
struct Tree{int lc,rc,ans;}tree[9500007];
int root[500007],tot;
void insert(int &rt,int last,int l,int r,int k){
	rt=++tot;
	tree[rt]=tree[last];
	tree[rt].ans++;
	if(l==r)return ;
	int mid=l+r>>1;
	if(k<=mid)insert(tree[rt].lc,tree[last].lc,l,mid,k);
	else insert(tree[rt].rc,tree[last].rc,mid+1,r,k);
}
int query(int L,int R,int l,int r,int k){
	if(l==r)return l;
	int mid=l+r>>1;
	int o=tree[tree[R].lc].ans-tree[tree[L].lc].ans;
	int O=tree[tree[R].rc].ans-tree[tree[L].rc].ans;
	if(o+o>k)return query(tree[L].lc,tree[R].lc,l,mid,k);
	if(O+O>k)return query(tree[L].rc,tree[R].rc,mid+1,r,k);
	return 0;
}
int main(){
	int n=read(),m=read();
	for(int i=1;i<=n;++i){
		int a=read();insert(root[i],root[i-1],1,n,a);
	}
	for(int i=1;i<=m;++i){
		int l=read(),r=read();
		printf("%d\n",query(root[l-1],root[r],1,n,r-l+1));
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值