2019牛客暑期多校训练营(第七场) E 线段树+离散化
题意:按照一定的公式给出若干个$<l,r>$,每次往一个序列中加上l到r的数字,并输出中位数。
思路:需要将每个$区间$离散化,比如把$[1,2]$变成$[1,3)$,也就是$[1,2)$和$[2,3)$,这样做才能完整的表达区间,否则如[2,2]这样的区间就会出现问题。
所以我们将每一个$[l,r+1)$离散化,设$(x)$代表x离散化后的数字,每次更新的时候,右区间应该是$(r+1)-1$。比如原来只有一个区间$[2,3]$,我们表示成$[2,4)$,离散化后我们更新的区间是$[1,1]$,加的数字的个数是$ve[2]-ve[1]$,ve是原数组。
查询和更新思路一样。
#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
#define pb(a) push_back(a)
#define rep(i,x,n) for(int i=x;i<=n;++i)
#define dep(i,n,x) for(int i=n;i>=x;--i)
using namespace std;
typedef long long ll;
int n,m;
const int inf=0x3f3f3f3f;
const int maxn=4e5+;
ll x[maxn],y[maxn],A1,B1,C1,M1,A2,B2,C2,M2;
vector<ll >ve;
ll sz[maxn<<],lazy[maxn<<];
void add(int o,int l,int r,ll f){
sz[o]+=(ve[r+]-ve[l])*f;
lazy[o]+=f;
}
void pushdown(int o,int l,int r){
if(!lazy[o]||l==r)return;
int mid=(l+r)>>;
add(o<<,l,mid,lazy[o]);
add(o<<|,mid+,r,lazy[o]);
lazy[o]=;
}
void update(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr){
add(o,l,r,);
return;
}
int mid=(l+r)>>;
pushdown(o,l,r);
if(ql<=mid)update(o<<,l,mid,ql,qr);
if(mid<qr)update(o<<|,mid+,r,ql,qr);
sz[o]=sz[o<<]+sz[o<<|];
} ll query(int o,int l,int r,int num){
if(l==r){
ll tot=sz[o]/(ve[l+]-ve[l]);
return ve[l]+(num-)/tot;
}
pushdown(o,l,r);
int mid=(l+r)>>;
if(sz[o<<]>=num)return query(o<<,l,mid,num);
return query(o<<|,mid+,r,num-sz[o<<]);
} int main(){
int n;
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);
rep(i,,n){
x[i]=(A1*x[i-]+B1*x[i-]+C1)%M1;
y[i]=(A2*y[i-]+B2*y[i-]+C2)%M2;
} rep(i,,n){
x[i]++,y[i]++;
if(x[i]>y[i]) swap(x[i],y[i]);
ve.push_back(x[i]); ve.push_back(y[i]+);
}
sort(ve.begin(),ve.end());
ve.erase(unique(ve.begin(),ve.end()),ve.end());
int cnt=ve.size();
rep(i,,n){ x[i]=lower_bound(ve.begin(),ve.end(),x[i])-ve.begin();
y[i]=lower_bound(ve.begin(),ve.end(),y[i]+)-ve.begin();
update(,,cnt,x[i],y[i]-);
printf("%lld\n",query(,,cnt,(sz[]+)/));
}
}
2019牛客暑期多校训练营(第七场) E 线段树+离散化的更多相关文章
- 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$的最大值."等价"的意思是在其任意一个子区间内的最小值相同. $ ...
随机推荐
- Navicat for MySQL使用手记
摘要 在管理MySQL数据库的图形化工具中,最为熟知的就是phpMyAdmin和Mysql-Front了,今天跟大家分享另外一个管理mysql数据库的另外一个利器---Navicat MySQL. N ...
- 2018-11-24-C#-7.0
title author date CreateTime categories C# 7.0 lindexi 2018-11-24 16:32:58 +0800 2018-2-13 17:23:3 + ...
- nginx 自启动
转载:https://www.cnblogs.com/cxscode/p/8262319.html 安装Nginx 下载windows版nginx (http://nginx.org/download ...
- Linux --赋予普通用户root 权限
Linux的普通用户在安装一些东西的时候或者执行命令的时候,终端始终会提示权限不够,我们会将这个普通用户赋予root权限,但是,和root还是有区别的,因为只能执行root规定好的一些操作命令. 1. ...
- Laravel Route Resource 方法
新增的 resource 方法将遵从 RESTful 架构为用户资源生成路由.该方法接收两个参数,第一个参数为资源名称,第二个参数为控制器名称. Route::resource('users', 'U ...
- 通过js渲染高层级DOM实现网页加水印
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- javascript基础总结之实例(一)
样式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- cookie中文转码
//cookie中文转码 var GB2312UnicodeConverter = { //转码 ToUnicode: function(str) { //中文转unicode return esca ...
- hdu第九场多校
02:线段树两次扫描 #include<bits/stdc++.h> #include<vector> using namespace std; #define maxn 30 ...
- 回滚线段树+bitset优化01背包——cf981E
/*首先考虑如何计算一个点的可能凑出的值,这就是一个01可行性背包问题那么再拓展到一段区间[1..n]的点上,每个query都可以看做是一段区间上的点[l,r]加上一个体积为x的物品,转换到01背包上 ...