牛课练习赛34 Flittle w and Discretization 主席树维护Mex
主席树维护Mex。
每个右端点 r 维护出一棵 在[1, r ] 区间中 其他所有的 值离这个 r 最近的的位置是多少。
然后询问区间[L,R]的时候,从rt[R] 出发,然后如果左儿子的中所有出线位置的最小值 >= L, 则说明他们所有的点都出线在这个区间内了,然后往右走。
否则就说明左边有点没有出线过,需要往左走。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N =3e5+ ;
struct Node{
int ls, rs, mn, num;
Node(){ls = rs = mn = num = ;}
}tr[N<<];
int rt[N];
int tot;
int build(int l, int r){
int x = ++tot;
if(l == r) return x;
int m = l+r >> ;
tr[x].ls = build(l,m);
tr[x].rs = build(m+,r);
return x;
}
int Update(int L, int p, int lst, int l, int r){
int x = ++tot;
tr[x] = tr[lst];
if(l == r){
tr[x].mn = p;
++tr[x].num;
return x;
}
int m = l+r >> ;
if(L <= m) tr[x].ls = Update(L, p, tr[lst].ls, l, m);
else tr[x].rs = Update(L, p, tr[lst].rs, m+, r);
tr[x].mn = min(tr[tr[x].ls].mn, tr[tr[x].rs].mn);
tr[x].num = tr[tr[x].ls].num + tr[tr[x].rs].num;
return x;
}
int Query(int L, int x, int x2){
// cout << L << ' ' << tr[x].mn << ' ' << tr[x].num - tr[x2].num << endl;
if(tr[x].mn >= L) return tr[x].num - tr[x2].num;
if(!tr[x].ls) return ;
int ret = ;
if(tr[tr[x].ls].mn >= L) ret = tr[tr[x].ls].num-tr[tr[x2].ls].num + Query(L,tr[x].rs,tr[x2].rs);
else ret = Query(L,tr[x].ls,tr[x2].ls);
return ret;
}
void Q(int p, int l, int r){
cout << l << " " << r << " " << tr[p].mn << ' ' << tr[p].num << endl;
if(l == r) return ;
int m = l+r >> ;
Q(tr[p].ls, l, m);
Q(tr[p].rs, m+, r);
}
int a[N];
int main(){
int n, m;
scanf("%d", &n);
rt[] = build(,n);
for(int i = ; i <= n; ++i){
scanf("%d", &a[i]);
if(a[i] <= n) rt[i] = Update(a[i], i, rt[i-], , n);
else rt[i] = rt[i-];
}
// Q(rt[4],1,n);
scanf("%d", &m);
int L, R;
for(int i = ; i <= m; ++i){
scanf("%d%d", &L, &R);
printf("%d\n", (R-L+)-Query(L, rt[R], rt[L-]));
}
return ;
}
牛课练习赛34 Flittle w and Discretization 主席树维护Mex的更多相关文章
- 牛客练习赛63 C 牛牛的揠苗助长 主席树 二分 中位数
LINK:牛牛的揠苗助长 题目很水 不过做法很多 想到一个近乎O(n)的做法 不过感觉假了 最后决定莽一个主席树 当然 平衡树也行. 容易想到 答案为ans天 那么一些点的有效增长项数为 ans%n. ...
- 牛客练习赛34 little w and Segment Coverage (差分区间)
链接:https://ac.nowcoder.com/acm/contest/297/C来源:牛客网 题目描述 小w有m条线段,编号为1到m. 用这些线段覆盖数轴上的n个点,编号为1到n. 第i条线段 ...
- 牛客NOIP提高组R1 C保护(主席树)
题意 题目链接 Sol Orz lyq 我们可以把一支军队(u, v)拆分为两个(u, lca)和(v, lca) 考虑一个点x,什么时候军队对它有贡献,肯定是u或v在他的子树内,且lca在他的子树外 ...
- 牛客练习赛34 D little w and Exchange(归纳)
题意: 给n个数,和m 问这组数是否可以构成[1, m]中的每一个数 思路: 先将a数组排序. 先算算构成前几个数需要什么,至少需要a[1]=1 需要a[2] = 1,2 在a[2] = 1的情况下a ...
- 牛客练习赛$48E$ 小$w$的矩阵前$k$大元素 堆
正解:堆 解题报告: 传送门$QwQ$ 考虑把$b$从大往小排序,然后把$a_1+b_1,a_2+b_1,...,a_n+b_1$丢到堆里,顺便记录下$b$的下标 然后每次拿出一个最大值,设为$mx= ...
- 牛客网 牛客练习赛4 A.Laptop-二维偏序+离散化+树状数组
A.Laptop 链接:https://ac.nowcoder.com/acm/contest/16/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其 ...
- 牛客练习赛47 E DongDong数颜色 (树状数组维护区间元素种类数)
链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...
- 牛客练习赛53 (E 老瞎眼 pk 小鲜肉) 线段树+离线
考试的时候切的,类似HH的项链~ code: #include <bits/stdc++.h> #define ll long long #define M 500003 #define ...
- 牛客多校第三次B——线段树维护线性基交
写线性基交函数时调试了半天.. #include<bits/stdc++.h> using namespace std; #define ll long long #define maxn ...
随机推荐
- 【转】解决eclipse连接不到genymotion的问题
(1)很多朋友在使用genymotion开发安卓应用程序的时候,会遇见完全正确的安装但是在运行的时候仍然找不到,genymotion上的设备,在打开的devices上找不到如下图所示: (2)解决的方 ...
- dubbo是如何控制并发数和限流的?
ExecuteLimitFilter ExecuteLimitFilter ,在服务提供者,通过 的 "executes" 统一配置项开启: 表示每服务的每方法最大可并行执行请求数 ...
- 从JavaScript到Python之异常
不少前端工程师看到这个标题可能会产生质问: 我js用得好好的,能后端能APP,为什么还要学习Python? 至少有下面两个理由: 学习曲线.ES6之后的JavaScript(TypeScript)的在 ...
- IdentityServer4笔记整理(更新中)
1 OAuth 2.0 1.1 OAuth 2.0协议流程图 1.2 授权码模式 1.3 简化模式 1.4 资源所有者密码模式 1.5 客户端凭证模式 2 OpenID Connect(OIDC) 2 ...
- codeforces1088D_Ehab and another another xor problem交互题
传送门 一道考验思维的交互题 大致思路就是从最高的二进制位向下询问 代入例子比如: 5 6 6 5 7 4 6 4 讨论一下 交互题的重点学会推理和归纳 #include <bits/stdc+ ...
- PDF.js 详情解说
pdf.js资源下载 点我下载 自定义默认加载的pdf资源 在web/view.js中我们可以通过DEFAULT_URL设置默认加载的pdf.通过上面代码我们也可以看出来可以通过后缀名来指定加载的pd ...
- 算法之《图》Java实现
数据结构之图 定义(百度百科) 图的术语表 无向图 深度优先搜索 广度优先遍历 有向图 路径问题 调度问题 强连通性 最小生成树(无向图) 最小生成树的贪心算法 加权无向图的数据结构 Kruskal算 ...
- Docker部署网站之后映射域名
Docker中部署tomcat相信大家也都知道,不知道的可以google 或者bing 一下.这里主要是为了记录在我们启动容器之后,tomcat需要直接定位到网站信息,而不是打开域名之后,还得加个bl ...
- 弃用 wget, 拥抱多线程下载 axel
0x00 事件 对于在 Linux 的下载工具而言,比较常用的就是 wget 或者 curl,吾也一直用 wget 的方式进行网络上的资源下载.偶然发现了 axel 这个支持多线程的下载工具,试用了几 ...
- HelloDjango 第 09 篇:让博客支持 Markdown 语法和代码高亮
作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 为了让博客文章具有良好的排版,显示更加丰富的格式,我们使用 Markdown 语法来书 ...