题目大意:给你一个序列,求某个区间出现次数大于一半的数是什么

主席树裸题,刷刷水题提升自信= =

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define il inline
#define N 500100
using namespace std;
//re
int n,m,tot;
int a[N],root[N];
struct Seg{
int ls,rs,sum;
}seg[N*]; int gc()
{
int rett=,fh=;char p=getchar();
while(p<''||p>''){if(p=='-')fh=-;p=getchar();}
while(p>=''&&p<=''){rett=(rett<<)+(rett<<)+p-'';p=getchar();}
return rett*fh;
}
il void pushup(int rt){seg[rt].sum=seg[seg[rt].ls].sum+seg[seg[rt].rs].sum;}
void build(int l,int r,int rt)
{
if(l==r) return;
int mid=(l+r)>>;
seg[rt].ls=++tot,build(l,mid,tot);
seg[rt].rs=++tot,build(mid+,r,tot);
}
void update(int x,int l,int r,int rt1,int rt2,int val)
{
if(l==r) {seg[rt2].sum+=val;return;}
int mid=(l+r)>>;
if(x<=mid){
if(!seg[rt2].ls||seg[rt2].ls==seg[rt1].ls){
seg[rt2].ls=++tot,seg[seg[rt2].ls].sum=seg[seg[rt1].ls].sum;
if(!seg[rt2].rs) seg[rt2].rs=seg[rt1].rs;
}update(x,l,mid,seg[rt1].ls,seg[rt2].ls,val);
}else{
if(!seg[rt2].rs||seg[rt2].rs==seg[rt1].rs){
seg[rt2].rs=++tot,seg[seg[rt2].rs].sum=seg[seg[rt1].rs].sum;
if(!seg[rt2].ls) seg[rt2].ls=seg[rt1].ls;
}update(x,mid+,r,seg[rt1].rs,seg[rt2].rs,val);
}pushup(rt2);
}
int query(int l,int r,int rt1,int rt2,int len)
{
if(l==r) return l;
int mid=(l+r)>>;
if(seg[seg[rt2].ls].sum-seg[seg[rt1].ls].sum>=len)
return query(l,mid,seg[rt1].ls,seg[rt2].ls,len);
if(seg[seg[rt2].rs].sum-seg[seg[rt1].rs].sum>=len)
return query(mid+,r,seg[rt1].rs,seg[rt2].rs,len);
return ;
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) a[i]=gc();
root[]=++tot,build(,n,tot);
for(int i=;i<=n;i++)
root[i]=++tot,update(a[i],,n,root[i-],root[i],);
int x,y;
while(m--)
{
x=gc(),y=gc();
printf("%d\n",query(,n,root[x-],root[y],(y-x+)/+));
}
return ;
}

最新文章

  1. openstack学习(一)kvm-libvirt
  2. matplotlib绘制多组 散点连线图【用于对比】待实现
  3. GC之一--GC 的算法分析、垃圾收集器、内存分配策略介绍
  4. 120条Photoshop新手必看技巧
  5. 获取SqlServer当前链接数
  6. 数组length属性的一些特性
  7. 80C51学习 蜂鸣器
  8. 老生常谈:关于undo表空间的使用率
  9. 找出生成json中的error_code,并加以处理
  10. Windows下搭建Redis集群
  11. .net core Error -4090 EADDRNOTAVAIL address not available”
  12. ASP.NET MVC之验证终结者篇
  13. sqlserver 2012隐藏查询结果窗口
  14. linux进程篇 (三) 进程间的通信2 信号通信
  15. Nginx从搭建到配置支持HTTPS
  16. count(*) 和count(1) 有区别吗
  17. linux uart驱动&mdash;&mdash;uart原理(一)
  18. http协议详解-经典篇
  19. 【aspnetcore】添加自定义json配置文件
  20. Hart协议

热门文章

  1. 训练1-o
  2. django rest-farme-work 的使用(1)
  3. MAVEN 的常用命令
  4. Linux 中常用的基础命令二
  5. 移植Mplayer到OK6410开发板
  6. POJ 2157 How many ways??
  7. 基于mybatis的CRUD
  8. BA-siemens-PXC模块调试
  9. java根据汉字获取全拼和首字母
  10. AngularJS 下拉列表demo