P3567 [POI2014]KUR-Couriers

时间:2023-03-08 22:12:25
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, P3567 [POI2014]KUR-Couriers and P3567 [POI2014]KUR-Couriers (P3567 [POI2014]KUR-Couriers), 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 P3567 [POI2014]KUR-Couriers to (at most) P3567 [POI2014]KUR-Couriers.

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

The P3567 [POI2014]KUR-Couriers lines that follow specify the time period queries, one per line.

Each query is specified by two integers, P3567 [POI2014]KUR-Couriers and P3567 [POI2014]KUR-Couriers (P3567 [POI2014]KUR-Couriers), separated by a single space.

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

In tests worth P3567 [POI2014]KUR-Couriers of total score, the condition P3567 [POI2014]KUR-Couriers holds, and in tests worth P3567 [POI2014]KUR-Couriers of total score P3567 [POI2014]KUR-Couriers.

输出格式:

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

(Thus a total of P3567 [POI2014]KUR-Couriers 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 P3567 [POI2014]KUR-Couriers 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

说明

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

题解

板子题练练手

刚开始还以为区间指的是数的区间……结果WAWA大哭……

后来才发现如果是数的区间我好像根本不会做……

老老实实敲了个主席树板子

不懂得可以看看大佬的讲解

 //minamoto
#include<bits/stdc++.h>
#define N 500005
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
char obuf[<<],*o=obuf;
void print(int x){
if(x>) print(x/);
*o++=x%+;
}
int sum[N*],L[N*],R[N*],t[N];
int n,q,cnt=;
void update(int last,int &now,int l,int r,int x){
if(!now) now=++cnt;
sum[now]=sum[last]+;
if(l==r) return;
int mid=(l+r)>>;
if(x<=mid) R[now]=R[last],update(L[last],L[now],l,mid,x);
else L[now]=L[last],update(R[last],R[now],mid+,r,x);
}
int query(int u,int v,int l,int r,int k){
if(l==r) return l;
int x=sum[L[v]]-sum[L[u]],y=sum[R[v]]-sum[R[u]];
int mid=(l+r)>>;
if(x*>k) return query(L[u],L[v],l,mid,k);
if(y*>k) return query(R[u],R[v],mid+,r,k);
return ;
}
int main(){
//freopen("testdata.in","r",stdin);
n=read(),q=read();
for(int i=;i<=n;++i){
int x=read();
update(t[i-],t[i],,n,x);
}
while(q--){
int x,y;
x=read(),y=read();
int k=query(t[x-],t[y],,n,y-x+);
print(k);
*o++='\n';
}
fwrite(obuf,o-obuf,,stdout);
return ;
}