题目链接: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 (线段树+离散化 区间为点)的更多相关文章

  1. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  2. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...

  3. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

  4. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  5. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  6. 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...

  7. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  8. 2019牛客暑期多校训练营(第二场)J-Subarray(思维)

    >传送门< 前言 这题我前前后后看了三遍,每次都是把网上相关的博客和通过代码认真看了再思考,然并卵,最后终于第三遍也就是现在终于看懂了,其实懂了之后发现其实没有那么难,但是的的确确需要思维 ...

  9. 2019牛客暑期多校训练营(第一场)-A (单调栈)

    题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...

  10. 2019牛客暑期多校训练营(第一场)A - Equivalent Prefixes(单调栈)

    题意 给定两个$n$个元素的数组$a,b$,它们的前$p$个元素构成的数组是"等价"的,求$p$的最大值."等价"的意思是在其任意一个子区间内的最小值相同. $ ...

随机推荐

  1. MySQL基础练习01--牛客网

    目录 1 查找最晚入职员工的信息 2 查找入职第三晚的员工信息 3 查找当前薪水详情及部门编号 4 查找所有员工入职时的薪水情况 5 查找已分配员工姓名 6 查找员工姓名 7 查找涨薪找过15次的员工 ...

  2. Codeforces Round #402 (Div. 2) D题 【字符串二分答案+暴力】

    D. String Game Little Nastya has a hobby, she likes to remove some letters from word, to obtain anot ...

  3. OSError: cannot open resource(pillow错误处理)

    https://www.jianshu.com/p/c64ae3e9b196 pillow使用备忘之OSError: cannot open resource错误处理 在使用pillow过程中,Pyt ...

  4. 【线性代数】Linear Algebra Big Picture

    Abstract: 通过学习MIT 18.06课程,总结出的线性代数的知识点相互依赖关系,后续博客将会按照相应的依赖关系进行介绍.(2017-08-18 16:28:36) Keywords: Lin ...

  5. ETL测试场景和测试用例设计

    前段时间做了些数据测试相关的工作,找了些相关方面的资料,也跟一些一线厂的同学聊了下数据测试方面的东西,然后在团队内部形成了一个初级的数据测试的规范流程以及测试需要进行的场景设计和测试用例设计的方案. ...

  6. P2089 烤鸡

    题目背景 猪猪hanke得到了一只鸡 题目描述 猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10种配料(芥末.孜然等),每种配料可以放1—3克, ...

  7. 修改tomcat控制台的标题

    Tomcat的bin目录下,创建一个名为setenv.bat的文件. setenv.bat  编辑内容 : set TITLE = 想要命名的标题名称  保存修改.重新启动. 第二种. 修改tomca ...

  8. mysql:unknown variable 'default-character-set=utf8'

    1.修改my.cnf后,执行 service mysql restart 重启数据库失败 service mysql restart Shutting down MySQL.. SUCCESS! St ...

  9. MyBatis-Plus的一些问题

    图一图二分别是搜索设计师的动态sql,已知这个字段是integer类型.为什么用=号查询的时候会显示查询超时.但是我把sql打印出来的结果直接去执行,时间在一秒是可以出来结果的. 但是用like一个i ...

  10. ArcGIS Python 坐标系信息

    ############################################################# import arcpy import os from arcpy impo ...