2019牛客暑期多校训练营(第七场)-E Find the median (线段树+离散化 区间为点)
题目链接:https://ac.nowcoder.com/acm/contest/887/E
题意:给出L[i],R[i],每次添加L[i]...R[i],求出此时的中位数。
思路:因为添加的数范围为1e9,首先想到要用离散化,这里是用一个点来表示一个区间。

将右区间加一的主要目的是优化处理,将区间最后一个元素并入到前面,自己模拟一下就能明白啦。
然后用线段树维护每个节点所包含的元素个数,用懒惰标记laz表示加的次数,然后每次查询时若左子树的元素个数足够则在左子树查询,否则在右子树查询。
详见代码:
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std; typedef long long LL;
const int maxn=4e5+; struct node{
int l,r,laz;
LL sz;
}tr[maxn<<]; int n;
LL X[maxn],Y[maxn],A1,A2,B1,B2,C1,C2,M1,M2;
vector<int> vc; void build(int v,int l,int r){
tr[v].l=l,tr[v].r=r;
if(l==r){
return;
}
int mid=(l+r)>>;
build(v<<,l,mid);
build(v<<|,mid+,r);
} void pushdown(int v){
tr[v<<].sz+=(vc[tr[v<<].r+]-vc[tr[v<<].l])*tr[v].laz;
tr[v<<].laz+=tr[v].laz;
tr[v<<|].sz+=(vc[tr[v<<|].r+]-vc[tr[v<<|].l])*tr[v].laz;
tr[v<<|].laz+=tr[v].laz;
tr[v].laz=;
} void update(int v,int l,int r){
if(l<=tr[v].l&&r>=tr[v].r){
tr[v].sz+=(vc[tr[v].r+]-vc[tr[v].l]);
tr[v].laz+=;
return;
}
if(tr[v].laz) pushdown(v);
int mid=(tr[v].l+tr[v].r)>>;
if(l<=mid) update(v<<,l,r);
if(r>mid) update(v<<|,l,r);
tr[v].sz=tr[v<<].sz+tr[v<<|].sz;
} int query(int v,LL k){
if(tr[v].l==tr[v].r){
int tmp=tr[v].sz/(vc[tr[v].l+]-vc[tr[v].l]);
return vc[tr[v].l]+(k-)/tmp;
}
if(tr[v].laz) pushdown(v);
if(k<=tr[v<<].sz) return query(v<<,k);
else return query(v<<|,k-tr[v<<].sz);
} int main(){
scanf("%d",&n);
scanf("%lld%lld%lld%lld%lld%lld",&X[],&X[],&A1,&B1,&C1,&M1);
scanf("%lld%lld%lld%lld%lld%lld",&Y[],&Y[],&A2,&B2,&C2,&M2);
for(int i=;i<=n;++i){
X[i]=(A1*X[i-]%M1+B1*X[i-]%M1+C1)%M1;
Y[i]=(A2*Y[i-]%M2+B2*Y[i-]%M2+C2)%M2;
}
for(int i=;i<=n;++i){
++X[i],++Y[i];
if(X[i]>Y[i]) swap(X[i],Y[i]);
vc.push_back(X[i]),vc.push_back(Y[i]+);
}
sort(vc.begin(),vc.end());
vc.erase(unique(vc.begin(),vc.end()),vc.end());
LL sum=;
int cnt=vc.size();
build(,,cnt-);
for(int i=;i<=n;++i){
sum+=Y[i]-X[i]+;
int x,y;
x=lower_bound(vc.begin(),vc.end(),X[i])-vc.begin();
y=lower_bound(vc.begin(),vc.end(),Y[i]+)-vc.begin();
update(,x,y-);
printf("%d\n",query(,(sum+)>>));
}
return ;
}
2019牛客暑期多校训练营(第七场)-E Find the median (线段树+离散化 区间为点)的更多相关文章
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...
- [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem
链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 2019牛客暑期多校训练营(第二场)J-Subarray(思维)
>传送门< 前言 这题我前前后后看了三遍,每次都是把网上相关的博客和通过代码认真看了再思考,然并卵,最后终于第三遍也就是现在终于看懂了,其实懂了之后发现其实没有那么难,但是的的确确需要思维 ...
- 2019牛客暑期多校训练营(第一场)-A (单调栈)
题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...
- 2019牛客暑期多校训练营(第一场)A - Equivalent Prefixes(单调栈)
题意 给定两个$n$个元素的数组$a,b$,它们的前$p$个元素构成的数组是"等价"的,求$p$的最大值."等价"的意思是在其任意一个子区间内的最小值相同. $ ...
随机推荐
- 从斐波那契数列看java方法的调用过程
先看斐波那契数列的定义: 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为 ...
- 新西达电调初始化代码,使用nodejs ffi技术调用wiringpi,代码使用typescript编写
这是我设计的F450四轴飞行器飞控代码的一部分 运行在orangepi-zero上,操作系统是armbian,思路是使用node-ffi调用wiringpi的so库与GPIO通信,然后控制端逻辑代码使 ...
- python定时任务-sched模块
通过sched模块可以实现通过自定义时间,自定义函数,自定义优先级来执行函数. schedule = sched.scheduler( time.time,time.sleep) schedule是一 ...
- elasticsearch利用head插件
restful接口使用方法 RESTful接口URL的格式: http://localhost:9200///[] 其中index.type是必须提供的. id是可选的,不提供es会自动生成. ind ...
- 服务器多进程powershell导致服务器瘫痪问题解决
1.公司服务器多次无法访问,经查多由于开启了多个powershell进程,网上查询是被挖矿了,可通过将powershell应用程序重命名解决. 2.然而重命名的时候发现需要trustedInstall ...
- JDK与CGlib动态代理的实现
应用的原型为 执行者:房屋中介Agency(分为JDKAgency.CGlibAgency) 被代理对象:程序员Programmer 被代理对象的实现接口:租户Tenement(CGlibAgency ...
- mysql的启动问题
用cmd启动MySQL (net start mysql )时出现(发生系统错误 5. 拒绝访问)这样的错误是因为cmd 权限太低了需要提高cmd权限才行(即使管理员权限) 如下图cmd所示: ...
- outlier异常值检验算法之_箱型图(附python代码)
python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...
- 使用预设半透明鼠标Cursor
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- JVM源码分析之JDK8下的僵尸(无法回收)类加载器[z]
[z]http://lovestblog.cn/blog/2016/04/24/classloader-unload/ 概述 这篇文章基于最近在排查的一个问题,花了我们团队不少时间来排查这个问题,现象 ...