Byteasar works for the BAJ company, which sells computer games.html
The BAJ company cooperates with many courier companies that deliver the games sold by the BAJ company to its customers.c++
Byteasar is inspecting the cooperation of the BAJ company with the couriers.dom
He has a log of successive packages with the courier company that made the delivery specified for each package.oop
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.ui
Byteasar wants to find out which courier companies dominated in certain periods of time, if any.spa
Help Byteasar out!code
Write a program that determines a dominating courier company or that there was none.htm
給一個數列,每次詢問一個區間內有沒有一個數出現次數超過一半,沒有這種數輸出0blog
輸入格式:
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
給一個數列,每次詢問一個區間內有沒有一個數出現次數超過一半
前置技能:
主席樹
主席樹學習傳送門
對原序列建主席樹,對於一段區間,咱們先嚐試左子樹是否能夠知足條件,再查右子樹,若是都不能就返回0
其實若是會主席樹,這道題挺顯然的
#include<bits/stdc++.h> #define in(i) (i=read()) #define il extern inline #define rg register #define mid ((l+r)>>1) #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #define lol long long using namespace std; const lol N=1e6+10; lol read() { lol ans=0, f=1; char i=getchar(); while (i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();} while (i>='0' && i<='9') ans=(ans<<1)+(ans<<3)+(i^48), i=getchar(); return ans*f; } int n,m,tot,num; int a[N],b[N],rt[N<<6]; struct Chair_Tree { int l,r,v; }t[N<<6]; void build(int &u,int l,int r) { u=++tot; if(l==r) {t[u].v=a[l]; return;} build(t[u].l,l,mid); build(t[u].r,mid+1,r); } void insert(int &u,int l,int r,int pre,int p) { t[u=++tot]=t[pre]; t[u].v++; if(l==r) return; if(p<=mid) insert(t[u].l,l,mid,t[pre].l,p); else insert(t[u].r,mid+1,r,t[pre].r,p); } int query(int x,int y,int l,int r,int k) { if(l==r) return b[l]; int ln=t[t[y].l].v-t[t[x].l].v; int rn=t[t[y].r].v-t[t[x].r].v; if(k<=ln) return query(t[x].l,t[y].l,l,mid,k); if(k<=rn) return query(t[x].r,t[y].r,mid+1,r,k); return 0; } int main() { in(n), in(m); for (int i=1;i<=n;i++) in(a[i]),b[i]=a[i]; sort(b+1,b+1+n); num=unique(b+1,b+1+n)-b-1; for (int i=1;i<=n;i++) { int p=lower_bound(b+1,b+1+num,a[i])-b; insert(rt[i],1,num,rt[i-1],p); } for (int i=1,l,r,k;i<=m;i++) { in(l),in(r), k=(r-l+1)/2+1; printf("%d\n",query(rt[l-1],rt[r],1,num,k)); } }