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. bitcoind

    Bitcoin Core Daemon version v0.15.1.0-g7b57bc998f Usage: bitcoind [options] Start Bitcoin Core Daemo ...

  2. [mysql8 报错] 关闭ONLY_FULL_GROUP_BY

    bug原因: 对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中.简而言之,就是SELECT后面接的列必须 ...

  3. poj 3013 最短路SPFA算法

    POJ_3013_最短路 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23630 ...

  4. Linux 常用命令全拼

    pwd: print work directory 打印当前目录 显示出当前工作目录的绝对路径 ps: process status(进程状态,类似于windows的任务管理器) 常用参数:-auxf ...

  5. 单细胞测序|单细胞基因组|单细胞转录组|Gene editing|

    单细胞测序 单细胞基因组学 测量理由是单细胞的时间空间特异性. Gene expression&co-expression 比较正常cell与疾病cell,正常organ与疾病organ,看出 ...

  6. ubuntu16.04 pcl安装教程

    https://blog.csdn.net/zkj126521/article/details/80157351 https://blog.csdn.net/e_small/article/detai ...

  7. Java 进制转换(二进制(负),八进制,十进制,十六进制),位运算、逻辑运算(2)

    负数的二进制表现形式:其实就是该数的绝对值取反+1. 进制转换(二进制,八进制,十进制,十六进制),原理解析 十六进制的表现形式: (2)(与.异或.左移.右移.三元运算符)

  8. ServletContext实现网站计数器

    在网站开发中,有很多功能需要使用ServletContext,比如: 1.网站计数器 2.网站在线用户的显示 3.简单的聊天系统 总之,如果是涉及到不用用户共享数据,而这些数据量不大,同时又不希望写入 ...

  9. [原]排错实战——解决Tekla通过.tsep安装插件失败的问题

    原总结调试排错troubleshootteklaprocess monitorsysinternals 缘起 最近同事使用.tsep安装Tekla插件的时候,Tekla提示该插件已经存在了,需要卸载后 ...

  10. OpenCV学习与应用

    1.VS2019配置OpenCVhttps://blog.csdn.net/weixin_42274148/article/details/85321091 2.Python中使用PIL快速实现灰度图 ...