洛谷 P3567 [POI2014]KUR-Couriers

题目:https://www.luogu.org/problemnew/show/P3567

题目描述

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-1棵和第R棵线段树相减,

那么,得到的不就是第L棵线段树到第R棵主席树上的所有节点,

也就是这段区间内的所有元素建的一颗线段树?

 

我们在选出的这颗线段树上乱搞。

注意条件:超过一半!超过一半!超过一半!不能等于!

如果这个区间的一侧超过一半了,那么另一侧势必小于一半,

那么我们就可以往左往右选取区间,缩小范围,

这比一个一个枚举要快了很多。

 

好了蒟蒻的代码献上:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 #define ll long long
 8 const int maxn=500005;
 9 struct node{
10     node *l,*r;
11     int v;
12 }tree[maxn*30],*root[maxn],*cnt;
13 int n,m,tot,tmp;
14 int a[maxn],r[maxn];
15 int le,ri;
16 //r changed a original
17 node *update(node *lst,int l,int r){
18     node *ret=++cnt;
19     *ret=*lst;
20     ret->v++;
21     if (l==r){
22         return ret;
23     }
24     int mid=l+r>>1;
25     if (tmp<=mid) ret->l=update(ret->l,l,mid);
26     else ret->r=update(ret->r,mid+1,r);
27     return ret;
28 }
29 int query(node *u,node *v,int l,int r){
30     if (l==r){
31         return a[l];
32     }
33     int mid=l+r>>1,p=ri-le+1;
34     int p1=v->l->v-u->l->v;
35     int p2=v->r->v-u->r->v;
36     if (2*p1>p) return query(u->l,v->l,l,mid);
37     else if (2*p2>p) return query(u->r,v->r,mid+1,r);
38     else return 0;
39 }
40 int main(){
41     scanf("%d%d",&n,&m);
42     for (int i=1;i<=n;++i){
43         scanf("%d",&r[i]);
44         a[i]=r[i];
45     }
46     sort(a+1,a+1+n);
47     tot=unique(a+1,a+1+n)-a-1;
48     for (int i=1;i<=n;++i){
49         r[i]=lower_bound(a+1,a+1+tot,r[i])-a;
50     }
51     cnt=root[0]=tree->l=tree->r=tree;
52     for (int i=1;i<=n;++i){
53         tmp=r[i];
54         root[i]=update(root[i-1],1,tot);
55     }
56     while (m--){
57         scanf("%d%d",&le,&ri);
58         printf("%d\n",query(root[le-1],root[ri],1,tot));
59     }
60     return 0;
61 }
View Code

 

posted @ 2018-02-25 21:13  lonlyn  阅读(184)  评论(0编辑  收藏  举报