题目描述
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.
输入输出样例
说明
给一个数列,每次询问一个区间内有没有一个数出现次数超过一半
题解
板子题练练手
刚开始还以为区间指的是数的区间……结果WAWA大哭……
后来才发现如果是数的区间我好像根本不会做……
老老实实敲了个主席树板子
不懂得可以看看大佬的讲解
1 //minamoto 2 #include<bits/stdc++.h> 3 #define N 500005 4 using namespace std; 5 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) 6 char buf[1<<21],*p1=buf,*p2=buf; 7 inline int read(){ 8 #define num ch-'0' 9 char ch;bool flag=0;int res; 10 while(!isdigit(ch=getc())) 11 (ch=='-')&&(flag=true); 12 for(res=num;isdigit(ch=getc());res=res*10+num); 13 (flag)&&(res=-res); 14 #undef num 15 return res; 16 } 17 char obuf[1<<24],*o=obuf; 18 void print(int x){ 19 if(x>9) print(x/10); 20 *o++=x%10+48; 21 } 22 int sum[N*20],L[N*20],R[N*20],t[N]; 23 int n,q,cnt=0; 24 void update(int last,int &now,int l,int r,int x){ 25 if(!now) now=++cnt; 26 sum[now]=sum[last]+1; 27 if(l==r) return; 28 int mid=(l+r)>>1; 29 if(x<=mid) R[now]=R[last],update(L[last],L[now],l,mid,x); 30 else L[now]=L[last],update(R[last],R[now],mid+1,r,x); 31 } 32 int query(int u,int v,int l,int r,int k){ 33 if(l==r) return l; 34 int x=sum[L[v]]-sum[L[u]],y=sum[R[v]]-sum[R[u]]; 35 int mid=(l+r)>>1; 36 if(x*2>k) return query(L[u],L[v],l,mid,k); 37 if(y*2>k) return query(R[u],R[v],mid+1,r,k); 38 return 0; 39 } 40 int main(){ 41 //freopen("testdata.in","r",stdin); 42 n=read(),q=read(); 43 for(int i=1;i<=n;++i){ 44 int x=read(); 45 update(t[i-1],t[i],1,n,x); 46 } 47 while(q--){ 48 int x,y; 49 x=read(),y=read(); 50 int k=query(t[x-1],t[y],1,n,y-x+1); 51 print(k); 52 *o++=' '; 53 } 54 fwrite(obuf,o-obuf,1,stdout); 55 return 0; 56 }