【BZOJ 3524】【Poi2014】Couriers 可持久化线段树
为什么这个主席树叫可持久化线段树,我不知道,具体得问达神。我无限T,然后DaD3zZ一针见血地指出了我的N*50爆内存导致无限编译超时O)ZO)ZO)Z真是太神啦。以图为鉴:

达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50644748
然后是我根本没法跟DaD3zZ相比的拙劣的代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define read(x) x=getint()
using namespace std;
const int N=500010;
inline int max(const int &a,const int &b){return a>b?a:b;}
inline int min(const int &a,const int &b){return a<b?a:b;}
inline void swapp(int &a,int &b){int c=a;a=b;b=c;}
inline int getint(){char c;int ret=0,k=1;for(c=getchar();c<'0'||c>'9';c=getchar())if(c=='-')k=-1;for(;c>='0'&&c<='9';c=getchar())ret=ret*10+c-'0';return ret*k;}
int key,a[N],n,m,root[N],cnt=0;
struct node{int l,r,s;}T[N*20];
inline void update(int l,int r,int &pos){
T[++cnt]=T[pos]; pos=cnt; T[pos].s++;
if (l==r) return;
int mid=(l+r)>>1;
if (key<=mid) update(l,mid,T[pos].l); else update(mid+1,r,T[pos].r);
}
inline int query(int L,int R){
int l=1,r=n,x=L,y=R,mid,s;
while (l<r){
if (T[y].s-T[x].s<=key) return 0;
mid=(l+r)>>1;
if (T[T[y].l].s-T[T[x].l].s>key) r=mid,x=T[x].l,y=T[y].l;
else if (T[T[y].r].s-T[T[x].r].s>key) l=mid+1,x=T[x].r,y=T[y].r;
else return 0;
}return l;
}
int main(){
read(n); read(m);
for1(i,1,n) read(a[i]);
for1(i,1,n){
root[i]=root[i-1]; key=a[i];
update(1,n,root[i]);
}int x,y;
while (m--){
read(x); read(y); key=(y-x+1)/2;
printf("%d\n",query(root[x-1],root[y]));
}return 0;
}
然后就可以了
【BZOJ 3524】【Poi2014】Couriers 可持久化线段树的更多相关文章
- BZOJ 3542 [Poi2014]Couriers ——可持久化线段树
[题目分析] 查找区间内出现次数大于一半的数字. 直接用主席树,线段树上维护区间大小,由于要求出现次数大于一半,每到一个节点可以分治下去. 时间复杂度(N+Q)logN [代码] #include & ...
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- BZOJ-3524 Couriers 可持久化线段树
可持久化线段树,其实就是类主席树了.. 3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 128 MB Submit: 1124 Sol ...
- BZOJ 3524: [Poi2014]Couriers
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1905 Solved: 691[Submit][St ...
- bzoj 2653 二分答案+可持久化线段树
首先离散化,然后我们知道如果对于一个询问的区间[l1,r1],[l2,r2],我们二分到一个答案x,将[l1,r2]区间中的元素大于等于x的设为1,其余的设为-1,那么如果[l1,r1]的最大右区间和 ...
- 主席树||可持久化线段树||BZOJ 3524: [Poi2014]Couriers||BZOJ 2223: [Coci 2009]PATULJCI||Luogu P3567 [POI2014]KUR-Couriers
题目:[POI2014]KUR-Couriers 题解: 要求出现次数大于(R-L+1)/2的数,这样的数最多只有一个.我们对序列做主席树,每个节点记录出现的次数和(sum).(这里忽略版本差值问题) ...
- BZOJ 3524 [Poi2014]Couriers(可持久化线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3524 [题目大意] 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个 ...
- bzoj 2653 middle (可持久化线段树)
middle Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1981 Solved: 1097[Submit][Status][Discuss] D ...
- BZOJ 3524 [Poi2014]Couriers(二分+蒙特卡罗)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3524 [题目大意] 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个 ...
随机推荐
- EM basics- the Maxwell Equations
All the two important problems that the EM theory trys to describe and explain are propogation and r ...
- python之2048
#-*- coding:utf-8 -*- import curses from random import randrange, choice # generate and place new ti ...
- Java 枚举活用
/** * 联盟神兽.神兽资源枚举 * * @author TonyCody * @author 13-9-22 * @time 下午5:35 */ public enum LegionDogzTyp ...
- ajax载入数据是小细节
今天看了一个点子: 在 ajax 导入数据的 div中添加一些样式,比如:我们正紧急抢救 增加趣味性,有解决数据卡壳问题
- kvm虚拟化管理平台WebVirtMgr部署-完整记录(安装ubuntu虚拟机)-(5)
之前介绍了在webvirtmgr平台下创建centos,windows server 2008的虚拟机,今天说下创建ubuntu虚拟机的过程. (1)首先下载ubuntu16.04的iso镜像放到/u ...
- /etc/profile和~/.bash_profile的区别
/etc/profile是全局的,是私有的 /etc/profile用于整个系统所有用户, ~/.bash_profile, ~/.profile和~/.bashrc 用于各个用户,这里的" ...
- cookies,sessionStorage和localStorage的区别
联系: sessionStorage和localStorage一样,都是用来缓存客户端缓存信息. 他们都只能存储字符串类型对象. 区别: localStorage的生命周期是永久的,除非用户主动清除浏 ...
- 关于ZIP大文件压缩
实测:4.76 GB一个单文件压缩没有什么问题. import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...
- 装系统提示缺少所需的CD/DVD驱动器设备驱动程序
昨晚用ultraISO和win7 旗舰版(ultimate)的镜像做了个启动U盘,插在自己新电脑上安装过程中提示“缺少所需的CD/DVD驱动器设备驱动程序”,用网上的很多办法都不行,最后找官网的客服问 ...
- String类及常用方法
在学习String类之前,先看一道笔试题:new String("abc")创建了几个对象? 答案: 两个对象, 一个对象是 位于字符串常量池中,一个对象是位于堆内存中. 原因:主 ...