Find the median(2019年牛客多校第七场E题+左闭右开线段树)
题目链接
题意
每次往集合里面添加一段连续区间的数,然后询问当前集合内的中位数。
思路
思路很好想,但是卡内存。
当时写的动态开点线段树没卡过去,赛后机房大佬用动态开点过了,\(tql\)。
卡不过去就只能离散化加左闭右开线段树写了。
代码
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson (rt<<1),L,mid
#define rson (rt<<1|1),mid + 1,R
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 800000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
int n, tot, x, y, xx, yy, m1, m2, a1, a2, b1, b2, c1, c2;
int L[maxn], R[maxn], num[maxn*2], lazy[maxn*4];
LL sum[maxn*4];
void push_up(int rt) {
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void push_down(int rt, int l, int r) {
if(!lazy[rt]) return;
int x = lazy[rt];
lazy[rt] = 0;
int mid = (l + r) >> 1;
lazy[rt<<1] += x, lazy[rt<<1|1] += x;
sum[rt<<1] += 1LL * x * (num[mid+1]-num[l]);
sum[rt<<1|1] += 1LL * x * (num[r+1]-num[mid+1]);
}
void update(int l, int r, int rt, int L, int R) {
if(l <= L && R <= r) {
sum[rt] += num[R+1] - num[L];
++lazy[rt];
return;
}
push_down(rt, L, R);
int mid = (L + R) >> 1;
if(r <= mid) update(l, r, lson);
else if(l > mid) update(l, r, rson);
else {
update(l, mid, lson);
update(mid + 1, r, rson);
}
push_up(rt);
}
int query(LL all, int rt, int L, int R) {
if(L == R) {
LL pos = sum[rt] / (num[R+1] - num[L]);
pos = num[L] + (all - 1) / pos;
return pos;
}
push_down(rt, L, R);
int mid = (L + R) >> 1;
if(sum[rt<<1] >= all) return query(all, lson);
else return query(all - sum[rt<<1], rson);
}
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d", &n);
scanf("%d%d%d%d%d%d", &x, &xx, &a1, &b1, &c1, &m1);
scanf("%d%d%d%d%d%d", &y, &yy, &a2, &b2, &c2, &m2);
L[1] = min(x, y) + 1, R[1] = max(x, y) + 1;
L[2] = min(xx, yy) + 1, R[2] = max(xx, yy) + 1;
num[++tot] = L[1], num[++tot] = R[1] + 1;
num[++tot] = L[2], num[++tot] = R[2] + 1;
for(int i = 3; i <= n; ++i) {
int num1 = ((1LL * a1 * xx % m1 + 1LL * b1 * x % m1) % m1 + c1) % m1;
int num2 = ((1LL * a2 * yy % m2 + 1LL * b2 * y % m2) % m2 + c2) % m2;
L[i] = min(num1, num2) + 1, R[i] = max(num1, num2) + 1;
x = xx, xx = num1;
y = yy, yy = num2;
num[++tot] = L[i], num[++tot] = R[i] + 1;
}
sort(num + 1, num + tot + 1);
tot = unique(num + 1, num + tot + 1) - num - 1;
LL all = 0;
for(int i = 1; i <= n; ++i) {
all += R[i] - L[i] + 1;
L[i] = lower_bound(num + 1, num + tot + 1, L[i]) - num;
R[i] = lower_bound(num + 1, num + tot + 1, R[i] + 1) - num;
update(L[i], R[i]-1, 1, 1, tot);
printf("%d\n", query((all + 1) /2, 1, 1, tot));
}
return 0;
}
Find the median(2019年牛客多校第七场E题+左闭右开线段树)的更多相关文章
- Distance(2019年牛客多校第八场D题+CDQ+树状数组)
题目链接 传送门 思路 这个题在\(BZOJ\)上有个二维平面的版本(\(BZOJ2716\)天使玩偶),不过是权限题因此就不附带链接了,我也只是在算法进阶指南上看到过,那个题的写法是\(CDQ\), ...
- 2019年牛客多校第四场 B题xor(线段树+线性基交)
题目链接 传送门 题意 给你\(n\)个基底,求\([l,r]\)内的每个基底是否都能异或出\(x\). 思路 线性基交板子题,但是一直没看懂咋求,先偷一份咖啡鸡板子写篇博客吧~ 线性基交学习博客:传 ...
- Palindrome Mouse(2019年牛客多校第六场C题+回文树+树状数组)
目录 题目链接 题意 思路 代码 题目链接 传送门 题意 问\(s\)串中所有本质不同的回文子串中有多少对回文子串满足\(a\)是\(b\)的子串. 思路 参考代码:传送门 本质不同的回文子串肯定是要 ...
- generator 1(2019年牛客多校第五场B题+十进制矩阵快速幂)
目录 题目链接 思路 代码 题目链接 传送门 思路 十进制矩阵快速幂. 代码 #include <set> #include <map> #include <deque& ...
- Explorer(2019年牛客多校第八场E题+线段树+可撤销并查集)
题目链接 传送门 题意 给你一张无向图,每条边\(u_i,v_i\)的权值范围为\([L_i,R_i]\),要经过这条边的条件是你的容量要在\([L_i,R_i]\),现在问你你有多少种容量使得你可以 ...
- 2019年牛客多校第三场 F题Planting Trees(单调队列)
题目链接 传送门 题意 给你一个\(n\times n\)的矩形,要你求出一个面积最大的矩形使得这个矩形内的最大值减最小值小于等于\(M\). 思路 单调队列滚动窗口. 比赛的时候我的想法是先枚举长度 ...
- 2019牛客多校第八场 F题 Flowers 计算几何+线段树
2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...
- 牛客多校第三场 G Removing Stones(分治+线段树)
牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...
- 2020牛客多校第八场K题
__int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...
随机推荐
- ping-pong buffer
1 什么是pingpong? pingpong是一种数据缓存的手段,通过pingpong操作可以提高数据传输的效率. 2 什么时候需要pingpong? 在两个模块间交换数据时,上一级处理的结果不能马 ...
- 远程windows
1. 起因 因为经常用teamviewer,所以断定我是商业用户,不允许我用了.想买一个授权,结果太贵了,1700多.使用了很多其他的,向日葵卡顿,有的窗口点不到,vnc慢,效果差,卡顿,还收费,等等 ...
- 分布式事务解决方案(一) 2阶段提交 & 3阶段提交 & TCC
参考文档:http://blog.jobbole.com/95632/https://yq.aliyun.com/articles/582282?spm=a2c4e.11163080.searchbl ...
- 【Gamma】Scrum Meeting 9
目录 写在前面 进度情况 任务进度表 燃尽图 照片 写在前面 例会时间:6.7 22:30-23.00 例会地点:微信群语音通话 代码进度记录github在这里 进度情况 任务进度表 注:点击链接跳转 ...
- html页面添加左侧滑动菜单与内容部分的滚动条
html + css + jquery 展示地址:https://migloo.gitee.io/front 或 https://www.igloo.xin/front 思路: 1.通过jquery ...
- Maven -------------- Eclipse 安装maven ,配置setting文件
1.设置maven路径 Window->Preferences->Maven->Installations-> 选择maven的路径,如果原来有低版本的建议删除 选择好后点击f ...
- 使用springboot mybatis 查询时实体类中的驼峰字段值为null
看到返回结果以后主要分析了一下情况: 实体类的get.set方法确实 mapper.xml文件中的resultMap.resultType等原因导致 数据库中数据存在问题 经过检查与验证发现以上都不存 ...
- 【数据结构与算法】单链表操作(C++)
#include <stdio.h> #include <malloc.h> /*单链表节点定义*/ typedef struct LNode { int data; //da ...
- lower_bound()和upper_bound()
lower_bound()和upper_bound() 是方便的在有序数组中二分查找的函数,并且在STL其他数据结构中也提供该方法(如map和set). 但是两函数并不是二分查找"小于&qu ...
- stm32 普通IO口模拟串口通信
普通IO口模拟串口通信 串口通信协议 串口传输 默认 波特率9600 1起始位 1停止位 其他0 数据位是8位(注意图上的给错了). 传输时,从起始位开始,从一个数据的低位(LSB)开始发送,如图从左 ...