P2839 [国家集训队]middle
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的更多相关文章
- [洛谷P2839][国家集训队]middle
题目大意:给你一个长度为$n$的序列$s$.$Q$个询问,问在$s$中的左端点在$[a,b]$之间,右端点在$[c,d]$之间的子段中,最大的中位数. 强制在线. 题解:区间中位数?二分答案,如果询问 ...
- 洛谷P2839 [国家集训队]middle 主席树_二分
Code: #include <cstdio> #include <algorithm> #include <cstring> #include <strin ...
- Luogu P2839 [国家集训队]middle
题目 首先我们考虑解决中位数一类问题的常用手段:二分\(mid\),将大于等于它的设为\(1\),小于它的设为\(−1\),判断区间和是否\(\ge0\). 对于询问\(a,b,c,d\),二分完\( ...
- [国家集训队]middle 解题报告
[国家集训队]middle 主席树的想法感觉挺妙的,但是这题数据范围这么小,直接分块草过去不就好了吗 二分是要二分的,把\(<x\)置\(-1\),\(\ge x\)的置\(1\),于是我们需要 ...
- [国家集训队]middle
[国家集训队]middle 题目 解法 开\(n\)颗线段树,将第\(i\)颗线段树中大于等于第\(i\)小的数权值赋为1,其他的则为-1,对于每个区间维护一个区间和,最大前缀和,最大后缀和. 然后二 ...
- CF484E Sign on Fence && [国家集训队]middle
CF484E Sign on Fence #include<bits/stdc++.h> #define RG register #define IL inline #define _ 1 ...
- 【LG2839】[国家集训队]middle
[LG2839][国家集训队]middle 题面 洛谷 题解 按照求中位数的套路,我们二分答案\(mid\),将大于等于\(mid\)的数设为\(1\),否则为\(-1\). 若一个区间和大于等于\( ...
- BZOJ.2653.[国家集训队]middle(可持久化线段树 二分)
BZOJ 洛谷 求中位数除了\(sort\)还有什么方法?二分一个数\(x\),把\(<x\)的数全设成\(-1\),\(\geq x\)的数设成\(1\),判断序列和是否非负. 对于询问\(( ...
- luogu2839 [国家集训队]middle
题目链接:洛谷 题目大意:给定一个长度为$n$的序列,每次询问左端点在$[a,b]$,右端点在$[c,d]$的所有子区间的中位数的最大值.(强制在线) 这里的中位数定义为,对于一个长度为$n$的序列排 ...
随机推荐
- 定制选择范围的按钮RangeButton
定制选择范围的按钮RangeButton 效果: 源码: RangeButton.h 与 RangeButton.m // // RangeButton.h // PulsingView // // ...
- UITextField使用的一些细节
UITextField使用的一些细节 这篇博文是我自己使用UITextField的一些总结,并没有太多营养,并会持续更新. 2014.9.15 ---------------------------- ...
- vmware查看HBA卡、网卡驱动、firmware版本信息
在 ESXi 5.x 中,swfw.sh 命令随 vm-support 支持包收集工具一起提供.swfw.sh 命令可用来识别连接到主机的硬件的固件和驱动程序版本.要运行此命令,请使用该路径: # / ...
- 为什么php+apache本地站点访问超级慢
/etc/hosts中必然有一行为127.0.0.1,作用是什么呢? 特点: (1)127.0.0.1不光是unix系统,linux也好,windows也好,都会有这个循回地址的.(2)在IP地址的规 ...
- 面对对象程序设计_task2_1001.A+B Format (20)
Someting about 1001.A+B Format (20) 问题描述及我所写的代码:click here → My Task 看到这个题目的时候,我的想法很简单,直接判断直接输出,因为给定 ...
- 推荐一个好用的以多tab标签方式打开windows CMD的工具
最近我在做基于nodejs的微服务开发,需要在windows命令行里启动很多微服务.我的windows 10任务栏是这样子的: 我想找一款能像下图Chrome标签页这样打开windows 10 CMD ...
- Qimage QBuffer
QImage image;QByteArray ba;QBuffer buffer(&ba);buffer.open(QIODevice::WriteOnly);image.save(& ...
- python第六课——判断结构
1.判断结构 格式三: ① if 条件表达式1: 语句块1 elif 条件表达式2: 语句块2 elif 条件表达式3: 语句块3 elif 条件表达式y: 语句块y else: 语句块z ② 执行流 ...
- No Spring WebApplicationInitializer types detected on classpath异常的解决
1.问题描述,当配置成 时,通过地址栏访问默认路径的index.jsp报404错误,如果手动在浏览器输入“http://localhost:8080/index.jsp”,则出现源码文件,然后观察ec ...
- centos6.2/6.3/6.4+nginx+mysql5.5+php5.3.14
一.安装所需软件包yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype free ...