P2839 [国家集训队]middle


好妙的题啊,,,,

首先二分一个答案k,把数列里>=k的数置为1,<k的数置为0

这样数列最大和>=0就是k>=中位数,<0就是k<中位数

数列的最大和很好求哇

左边的最大后缀+中间+右边的最大前缀

主席树搞搞

完事了

// It is made by XZZ
#include<cstdio>
#include<algorithm>
#define il inline
#define rg register
#define vd void
#define sta static
using namespace std;
il int gi(){
rg int x=0,f=1;rg char ch=getchar();
while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
const int maxn=20010;
int a[maxn],b[maxn];
const int maxd=500001;
struct node{
int tot,lmax,rmax;
node operator+(const node&x)const{
return (node){tot+x.tot,max(lmax,tot+x.lmax),max(x.rmax,rmax+x.tot)};
}
}s[maxd];
int rt[maxn],ls[maxd],rs[maxd],id;
struct yyb{int p,a;}c[maxn];
il bool operator<(const yyb&a,const yyb&b){return a.a<b.a;}
#define mid ((l+r)>>1)
il vd build(int&x,int l,int r){
x=++id;
s[x]=(node){r-l+1,r-l+1,r-l+1};
if(l==r)return;
build(ls[x],l,mid),build(rs[x],mid+1,r);
}
il vd update(int&x,int l,int r,const int&p,const int&o){
++id,ls[id]=ls[x],rs[id]=rs[x],s[id]=s[x];
x=id;
if(l==r){s[x]=(node){o,o,o};return;}
if(p<=mid)update(ls[x],l,mid,p,o);
else update(rs[x],mid+1,r,p,o);
s[x]=s[ls[x]]+s[rs[x]];
}
il node query(const int&x,int l,int r,const int&L,const int&R){
if(L>R)return(node){0,0,0};
if(L<=l&&r<=R)return s[x];
if(L<=mid)
if(mid<R)return query(ls[x],l,mid,L,R)+query(rs[x],mid+1,r,L,R);
else return query(ls[x],l,mid,L,R);
else return query(rs[x],mid+1,r,L,R);
}
#undef mid
int main(){
freopen("nt2012_middle.in","r",stdin);
freopen("nt2012_middle.out","w",stdout);
int n=gi(),m;
for(rg int i=1;i<=n;++i)a[i]=b[i]=gi();
sort(b+1,b+n+1);m=unique(b+1,b+n+1)-b-1;
for(rg int i=1;i<=n;++i)a[i]=lower_bound(b+1,b+m+1,a[i])-b;
for(rg int i=1;i<=n;++i)c[i]=(yyb){i,a[i]+1};
sort(c+1,c+n+1);
build(rt[1],1,n);
int p=1;
for(rg int i=1;i<=n;++i){
while(p<c[i].a)rt[p+1]=rt[p],++p;
if(c[i].a<=m)update(rt[c[i].a],1,n,c[i].p,-1);
}
while(p<m)rt[p+1]=rt[p],++p;
int l1,r1,l2,r2,l3,r3,l,r,mid,fafa[4],lst=0,sum;
int q=gi();
while(q--){
for(rg int i=0;i<4;++i)fafa[i]=(gi()+lst)%n+1;
sort(fafa,fafa+4);
l1=fafa[0],r1=fafa[1],l2=fafa[1]+1,r2=fafa[2]-1,l3=fafa[2],r3=fafa[3];
l=1,r=m;
while(l<r){
mid=((l+r)>>1)+1;
sum=query(rt[mid],1,n,l1,r1).rmax+query(rt[mid],1,n,l2,r2).tot+query(rt[mid],1,n,l3,r3).lmax;
(sum>=0)?l=mid:r=mid-1;
}
printf("%d\n",lst=b[l]);
}
return 0;
}

P2839 [国家集训队]middle的更多相关文章

  1. [洛谷P2839][国家集训队]middle

    题目大意:给你一个长度为$n$的序列$s$.$Q$个询问,问在$s$中的左端点在$[a,b]$之间,右端点在$[c,d]$之间的子段中,最大的中位数. 强制在线. 题解:区间中位数?二分答案,如果询问 ...

  2. 洛谷P2839 [国家集训队]middle 主席树_二分

    Code: #include <cstdio> #include <algorithm> #include <cstring> #include <strin ...

  3. Luogu P2839 [国家集训队]middle

    题目 首先我们考虑解决中位数一类问题的常用手段:二分\(mid\),将大于等于它的设为\(1\),小于它的设为\(−1\),判断区间和是否\(\ge0\). 对于询问\(a,b,c,d\),二分完\( ...

  4. [国家集训队]middle 解题报告

    [国家集训队]middle 主席树的想法感觉挺妙的,但是这题数据范围这么小,直接分块草过去不就好了吗 二分是要二分的,把\(<x\)置\(-1\),\(\ge x\)的置\(1\),于是我们需要 ...

  5. [国家集训队]middle

    [国家集训队]middle 题目 解法 开\(n\)颗线段树,将第\(i\)颗线段树中大于等于第\(i\)小的数权值赋为1,其他的则为-1,对于每个区间维护一个区间和,最大前缀和,最大后缀和. 然后二 ...

  6. CF484E Sign on Fence && [国家集训队]middle

    CF484E Sign on Fence #include<bits/stdc++.h> #define RG register #define IL inline #define _ 1 ...

  7. 【LG2839】[国家集训队]middle

    [LG2839][国家集训队]middle 题面 洛谷 题解 按照求中位数的套路,我们二分答案\(mid\),将大于等于\(mid\)的数设为\(1\),否则为\(-1\). 若一个区间和大于等于\( ...

  8. BZOJ.2653.[国家集训队]middle(可持久化线段树 二分)

    BZOJ 洛谷 求中位数除了\(sort\)还有什么方法?二分一个数\(x\),把\(<x\)的数全设成\(-1\),\(\geq x\)的数设成\(1\),判断序列和是否非负. 对于询问\(( ...

  9. luogu2839 [国家集训队]middle

    题目链接:洛谷 题目大意:给定一个长度为$n$的序列,每次询问左端点在$[a,b]$,右端点在$[c,d]$的所有子区间的中位数的最大值.(强制在线) 这里的中位数定义为,对于一个长度为$n$的序列排 ...

随机推荐

  1. 为了让开发者写MaxCompute SQL更爽,DataWorks 增强SQL 编辑器功能

    众所周知,数据开发和分析的同学每天都要花大量时间写MaxCompute SQL:Dataworks作为数据开发的IDE直接影响着大家的开发效率,这次新上线的Dataworks我们在编辑体验上做了很多工 ...

  2. 铁乐学python_day04-作业

    1,写代码,有如下列表,按照要求实现每一个功能 li = ['alex', 'wusir', 'eric', 'rain', 'alex'] 计算列表的长度并输出 print(len(li)) 答:结 ...

  3. openoffice centos7.4 安装

    其他是配置好java环境后操作 1.下载软件 http://www.openoffice.org/download/other.html 2.安装 tar xf Apache_OpenOffice_4 ...

  4. mac lnmp

    Mac下安装LNMP(Nginx+PHP5.6)环境 Mac下安装LNMP(Nginx+PHP5.6)环境 安装Homebrew 最近工作环境切换到Mac,所以以OS X Yosemite(10.10 ...

  5. python第九课——while死循环

    2.3.无限循环/死循环: 何时发生无限循环? 循环条件永远为True,就出现了无限循环 [注意] 无限循环是需要避免的,因为它极其占用系统资源: 但是配合我们之后讲的break等关键字,就会变得更有 ...

  6. 【node.js】全局变量、常用工具、文件系统

    学习链接:http://www.runoob.com/nodejs/nodejs-global-object.html 在 JavaScript 中,通常 window 是全局对象, 而 Node.j ...

  7. 1、JVM-走进java

    1.1.概述 Java不仅仅是一门编程语言,还是一个由一系列计算机软件和规范形成的技术体系,这个技术体系提供了完整的用于软件开发和跨平台部署的支持环境,并广泛应用于嵌入式系统.移动终端.企业服务器.大 ...

  8. jmeter验证WEB页面的href链接请求

    1. 第一步: 创建Samper_HTTP请求,打开测试页面 2. 第二步: 创建后置处理器_正则表达式(也有其他方式,这里仅介绍正则) 如图 3. 第三步 创建逻辑控制器_ForEach控制器,配置 ...

  9. 集合之保持compareTo和equals同步

    在Java中我们常使用Comparable接口来实现排序,其中compareTo是实现该接口方法.我们知道compareTo返回0表示两个对象相等,返回正数表示大于,返回负数表示小于.同时我们也知道e ...

  10. 初识Qt布局管理器

    Qt布局管理器的类有4种,它们分别为QHBoxLayout.QVBoxLayout.QGridLayout和QStackLayout.其中,QHBoxLayout实现水平布局,QVBoxLayout实 ...