【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组询问,每次询问一个 ...
随机推荐
- 边工作边刷题:70天一遍leetcode: day 88
Unique Word Abbreviation 要点: 简单题,主要是理解题意.no other words have the same abbreviation as the given word ...
- Centos7网络监控
EPEL是企业版 Linux 附加软件包的简称,EPEL是一个由Fedora特别兴趣小组创建.维护并管理的,针对 红帽企业版 Linux(RHEL)及其衍生发行版(比如 CentOS.Scientif ...
- linux强制用户下线
Linux系统为多用户多任务系统,因此允许多个用户登录到系统,有时候,我们需要强制某些用户下线. 前提:必须是root权限操作:(1)使用who查看目前有哪些用户登录了服务器,见下图 [root@vm ...
- java 15-2 Collection的高级功能测试
A: boolean addAll(Collection c):添加一个集合的元素 ,所有的元素,无论是否重复 B: boolean removeAll(Collection c):移除一个集合的 ...
- 使用JspStudy集成环境快速部署jsp项目
1. 安装jdk 本人网盘资源:https://yunpan.cn/ckZLNbqxkDYYe (提取码:b5e8) 去jdk官网下载最新的jdk: http://www.oracle.com/tec ...
- sqlzoo.net刷题4
SELECT name, continent FROM world a WHERE population > ( FROM world b WHERE a.continent = b.conti ...
- Spring2.5与JDK8的集成问题
Spring2.5不支持JDK8及其以上的版本,因为有一段校验JDK版本的代码,当JDK版本大于1.7之后,会识别成JDK1.4之前的.会报版本太低的错误. /* * Copyright 2002-2 ...
- RDLC系列之七 条码打印
参考: C# 条码标签打印程序,RDLC报表动态显示多条码标签的方法 http://www.cnblogs.com/vice/p/4105898.html 我做的思路是:不使用数据库存储image的b ...
- int 与Integer的用法与区别
1.int是基本类型,直接存取数值,Integer是对象,用一个引用指向这个对象. 2.java中的数据类型分为基本数据类型和复杂数据类型,int是前者,Integer是后者(也就是一个类). 3.初 ...
- Asp.net设计模式笔记之二:应用程序分离与关注点分离
本次笔记主要涉及的内容如下: 1.将智能UI(SmartUI)反模式重构成分层方式的示例代码 2.分层设计与传统的Asp.net WebForm模型(代码后植)相比具有的优势 3.逻辑分层概念以及分离 ...