https://ac.nowcoder.com/acm/contest/887/E

树状数组做法(代码有注释)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=4e5+;
int x[M],y[M],l[M],r[M],ans[M<<],tot;
ll bit1[M<<],bit2[M<<];
int lowbit(int x){
return x&(-x);
}
void add(ll bit[], int p, int x) {
while(p<=tot){
bit[p]+=x;
p+=lowbit(p);
}
}
ll query(ll bit[], int p) {
ll ans = ;
while(p>){
ans+=bit[p];
p-=lowbit(p);
}
return ans;
}
int main(){
int n;
scanf("%d",&n);
ll a1,a2,b1,b2,c1,c2,m1,m2;
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++){
if(i>){
x[i]=(a1*x[i-]+b1*x[i-]+c1)%m1;
y[i]=(a2*y[i-]+b2*y[i-]+c2)%m2;
}
l[i]=min(x[i],y[i])+;
r[i]=max(x[i],y[i])+;
ans[++tot]=l[i];
ans[++tot]=r[i]+; }
///离散化处理
sort(ans+,ans+tot+);
tot=unique(ans+,ans+tot+)-ans-;
ll nownum=;
for(int i=;i<=n;i++){ nownum+=(r[i]-l[i]+);///加上当前区间数的数目 ,为了达到用数目找中位数的目的
int L=lower_bound(ans+,ans+tot+,l[i])-ans;///找到l r分别在离散化后的位置
int R=lower_bound(ans+,ans+tot+,r[i]+)-ans;
///bit1 记录的是总共数的数目的前缀和,所以左右端点就那样赋值,前缀和起来就是总数:r[i]-l[i]+1;
add(bit1,L,-l[i]);
add(bit1,R,r[i]+);
//bit2 记录L的数目的前缀和
add(bit2,L,);
add(bit2,R,-);
///二分找
L=,R=1e9;
while(L<R){
int midd=(L+R)>>;
int pos=upper_bound(ans+,ans+tot+,midd)-ans-;
///若查询到query(bit2)不等于0,说明midd前面有若干个左区间端点 没有对应的右区间端点,即query(bit1)算不完全,所以要加还给temp;
ll tmp=query(bit1,pos)+query(bit2,pos)*(midd+);
if(tmp<(nownum+)/){///用数目找中位数
L=midd+;
}else{
R=midd;
}
}
printf("%d\n",L);
}
return ;
}

利用数目找中位数(牛客第七场E)的更多相关文章

  1. 牛客第七场 Sudoku Subrectangles

    链接:https://www.nowcoder.com/acm/contest/145/J来源:牛客网 You have a n * m grid of characters, where each ...

  2. 牛客第七场 Minimum Cost Perfect Matching 规律

    题意:1-n-1个数和1-n-1个数两两匹配,每次匹配将两个数的值进行与运算,要求每次匹配与运算的和相加最小,问满足匹配的配对方式 分析:打表前10个数与运算最小的匹配,我们发现,从n-1开始按位取反 ...

  3. 数据流中的中位数 牛客网 剑指Offer

    数据流中的中位数 牛客网 剑指Offer 题目描述 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就 ...

  4. 牛客第三场多校 H Diff-prime Pairs

    链接:https://www.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy has solved lots of problem involving calcul ...

  5. 牛客第五场 G max 思维

    链接:https://www.nowcoder.com/acm/contest/143/G来源:牛客网 Give two positive integer c, n. You need to find ...

  6. uestc summer training #9 牛客第三场 BFS计数

    G.coloring tree BFS计数 题目:给你n(<=5000)个节点的一颗树 你有K(<=5000)种颜色 你可以给每一个节点染一种颜色 总共有Kn种染色方法 在一种染色方法中 ...

  7. 牛客网第二场Jfarm(随机化+二维前缀和)

    链接:https://www.nowcoder.com/acm/contest/140/J 来源:牛客网 White Rabbit has a rectangular farmland of n*m. ...

  8. 牛客网第一场E题 Removal

    链接:https://www.nowcoder.com/acm/contest/139/E 来源:牛客网 Bobo has a sequence of integers s1, s2, ..., sn ...

  9. 牛客网第一场 A Monotonic Matrix

    链接:https://www.nowcoder.com/acm/contest/139/A来源:牛客网 Count the number of n x m matrices A satisfying ...

随机推荐

  1. HashMap的fast-fail和ConcurrentHashMap的fail-safe实例

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 Java HashMap遍历过程中如果元素被修改会导致遍历失败,ConcurrentHashMap则不会有 ...

  2. 直击JDD | 京东技术全景图首次展示 四大重磅智能技术驱动产业未来!

    11月19日,主题为"突破与裂变"的2019京东全球科技探索者大会(JDDiscovery)在京盛大开幕,京东集团展示了完整的技术布局与先进而丰富的对外技术服务,对外明确诠释了&q ...

  3. 浅谈tcp协议

    tcp协议     三次握手四次挥手                 优点: 稳定,可靠   应用:发邮件                 缺点: 有延迟 占用系统资源多               ...

  4. 运行roscore出现unable to contact my own server无法启动小海龟的部分故障问题解决

    运行roscore后,出现下图这种情况(unable to contact my own server) 原因是找不到http://后面那些,ping不到域名或IP. 参考http://www.ros ...

  5. [浅学]POST、GET、PUT、DELETE 请求

    HTTP定义了与服务器交互的不同的方法,最基本的是POST.GET.PUT.DELETE,与其比不可少的URL的全称是资源描述符,我们可以这样理解: url描述了一个网络上资源,而post.get.p ...

  6. java通过HSSFWorkbook导出xls文件

    使用swgger2.Restlet等接口工具有bug导致导出失败,测试直接使用浏览器 //导出列表-新 @UserRoleJudgment(authpos = SystemControllerLog. ...

  7. Golang解析json的几种方法

    Golang解析json的几种方法 概要 使用Golang调用其它平台API接口时总会被多层的json串给恶心到,我记录一下自己解析json的几种方法. 一.自带的json包 func JsonUnm ...

  8. no.2淘宝架构背后——零售业务中台架构设计探讨及实践读后感

    2017年8月12日,袋鼠云首席架构师正风在“网易博学实践日:大数据与人工智能技术大会”进行<淘宝架构演进背后——零售业务中台架构设计探讨及实践>演讲分享.传统零售行业如何选择应对新经济模 ...

  9. springBoot中mybatis错误之 Property 'configuration' and 'configLocation' can not specified with together 解决

    mybatis.config-location与mybatis.config-locations不同 mybatis.config-location不加载全局配置文件

  10. node 配置文件

    # cat ~/.npmrc prefix=E:/Private/nodejs #registry=http://r.cnpmjs.org/ registry=http://registry.npm. ...