BZOJ3524 & LOJ2432:[POI2014]代理商Couriers——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3524
给一个长度为n的序列a。1≤a[i]≤n。
m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2。如果存在,输出这个数,否则输出0。
水一发题解,重题:BZOJ2223:[Coci2009]PATULJCI
#include<cstdio> #include<queue> #include<cctype> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; const int N=5e5+5; inline int read(){ int X=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=getchar();} while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } struct tree{ int l,r,sum; }tr[N*25]; int rt[N],n,m,q,pool; inline void insert(int &y,int &x,int l,int r,int p){ tr[x=++pool]=tr[y]; tr[x].sum++; if(l==r)return; int mid=(l+r)>>1; if(p<=mid)insert(tr[y].l,tr[x].l,l,mid,p); else insert(tr[y].r,tr[x].r,mid+1,r,p); return; } inline int query(int nl,int nr,int l,int r,int k){ if(l==r)return l; int d1=tr[tr[nr].l].sum-tr[tr[nl].l].sum; int d2=tr[tr[nr].r].sum-tr[tr[nl].r].sum; int mid=(l+r)>>1; if(d1>k)return query(tr[nl].l,tr[nr].l,l,mid,k); if(d2>k)return query(tr[nl].r,tr[nr].r,mid+1,r,k); return 0; } int main(){ n=read();q=read(); for(int i=1;i<=n;i++)insert(rt[i-1],rt[i],1,n,read()); for(int i=1;i<=q;i++){ int l=read(),r=read(); int ans=query(rt[l-1],rt[r],1,n,(r-l+1)>>1); if(ans)printf("%d\n",ans); else puts("0"); } return 0; }
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/ +
+++++++++++++++++++++++++++++++++++++++++++