BZOJ 3524 [POI2014]KUR-Couriers (主席树)
2024-10-01 09:44:17
题目大意:给你一个序列,求某个区间出现次数大于一半的数是什么
主席树裸题,刷刷水题提升自信= =
#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 ;
}
最新文章
- openstack学习(一)kvm-libvirt
- matplotlib绘制多组 散点连线图【用于对比】待实现
- GC之一--GC 的算法分析、垃圾收集器、内存分配策略介绍
- 120条Photoshop新手必看技巧
- 获取SqlServer当前链接数
- 数组length属性的一些特性
- 80C51学习 蜂鸣器
- 老生常谈:关于undo表空间的使用率
- 找出生成json中的error_code,并加以处理
- Windows下搭建Redis集群
- .net core Error -4090 EADDRNOTAVAIL address not available”
- ASP.NET MVC之验证终结者篇
- sqlserver 2012隐藏查询结果窗口
- linux进程篇 (三) 进程间的通信2 信号通信
- Nginx从搭建到配置支持HTTPS
- count(*) 和count(1) 有区别吗
- linux uart驱动&mdash;&mdash;uart原理(一)
- http协议详解-经典篇
- 【aspnetcore】添加自定义json配置文件
- Hart协议