[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

说明

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

我们直接把读入的数字插入到主席树中,

然后对于询问[i,j],

在[1..n]中我们看看小于mid的数字有多少个,显然如果个数的两倍<=j-i+1那么[1..mid]中就不存在,

不然我们再看看大于mid的数字有多少个,同理,

如果两个都不行,就返回0,递归搞一搞就好了。

因为左右的数字个数=2×mid

所以左边>mid,那么右边<mid

虽然数字个数>mid并不代表左边有解,但代表右边一定无解

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 int root[15000001],n,m,ch[15000001][2],sum[15000001],a[500001],ans,pos;
 8 void update(int &rt,int l,int r,int x)
 9 {
10   int Rt=rt;
11   rt=++pos;
12   sum[rt]=sum[Rt]+1;
13   ch[rt][0]=ch[Rt][0];ch[rt][1]=ch[Rt][1];
14   if (l==r)
15     return;
16   int mid=(l+r)/2;
17   if (x<=mid) update(ch[rt][0],l,mid,x);
18   else update(ch[rt][1],mid+1,r,x);
19 }
20 int query(int L,int R,int l,int r,int x)
21 {
22   if (l==r) return l;
23   int mid=(l+r)/2;
24   int zyys1=sum[ch[R][0]]-sum[ch[L][0]],zyys2=sum[ch[R][1]]-sum[ch[L][1]];
25   if (zyys1>x) return query(ch[L][0],ch[R][0],l,mid,x);
26   if (zyys2>x) return query(ch[L][1],ch[R][1],mid+1,r,x);
27   return 0;
28 }
29 int main()
30 {int i,l,r;
31   cin>>n>>m;
32   for (i=1;i<=n;i++)
33     {
34       scanf("%d",&a[i]);
35     }
36   for (i=1;i<=n;i++)
37     {
38       root[i]=root[i-1];
39       update(root[i],1,n,a[i]);
40     }
41   for (i=1;i<=m;i++)
42     {
43       scanf("%d%d",&l,&r);
44       ans=query(root[l-1],root[r],1,n,(r-l+1)/2);
45       printf("%d\n",ans);
46     }
47 }

 

posted @ 2018-01-07 19:26  Z-Y-Y-S  阅读(247)  评论(0编辑  收藏  举报