传送门

题目分析

标准主席树,按照位置插入每个数,对于询问l, r, 在l-1,r两树上按照线段树搜索次数大于(r - l + 1) / 2的数。

code

#include<bits/stdc++.h>
using namespace std; const int N = 500050, M = 500050;
int n, m;
int a[N]; struct node{
int lc, rc, cnt;
}tr[N * 25];
int pool, rt[N]; namespace IO{
inline int read(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch -'0');
return i * f;
} inline void wr(int x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO; inline void insert(int x, int &y, int l, int r, int v){
tr[y = ++pool] = tr[x];
tr[y].cnt++;
if(l == r) return;
int mid = l + r >> 1;
if(v <= mid) insert(tr[x].lc, tr[y].lc, l, mid, v);
else insert(tr[x].rc, tr[y].rc, mid + 1, r, v);
} inline int query(int nl, int nr, int l, int r, int x){
// if(tr[nr].cnt - tr[nl].cnt < x) return 0;
if(l == r) return l;
int mid = l + r >> 1;
if(tr[tr[nr].lc].cnt - tr[tr[nl].lc].cnt >= x) return query(tr[nl].lc, tr[nr].lc, l, mid, x);
else if(tr[tr[nr].rc].cnt - tr[tr[nl].rc].cnt >= x) return query(tr[nl].rc, tr[nr].rc, mid + 1, r, x);
else return 0;
} int main(){
freopen("h.in", "r", stdin);
n = read(), m = read();
for(int i = 1; i <= n; i++) a[i] = read(), insert(rt[i - 1], rt[i], 1, 500000, a[i]);
for(int i = 1; i <= m; i++){
int l = read(), r = read(), len = (r - l + 1) / 2;
int ans = query(rt[l - 1], rt[r], 1, 500000, len + 1);
wr(ans), putchar('\n');
}
return 0;
}

BZOJ 3524 - 主席树的更多相关文章

  1. BZOJ 3524主席树裸题 (雾)

    思路: 按权值建一棵主席树 (但是这好像不是正解 空间复杂度是不对的--.) //By SiriusRen #include <cstdio> #include <cstring&g ...

  2. bzoj 1901 主席树+树状数组

    修改+查询第k小值 单纯主席树修改会打乱所有,所以再套一个树状数组维护前缀和使得修改,查询都是log 对了,bzoj上不需要读入组数,蜜汁re.. #include<cstdio> #in ...

  3. BZOJ 2588 主席树

    思路: 主席树 做完BZOJ 3123 觉得这是道水啊-- 然后狂RE 狂MLE 要来数据 忘把deep[1]设成1了----------. 啊wocccccccccccccccc //By Siri ...

  4. bzoj 1818 主席树

    思路:主席树搞一搞. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #de ...

  5. BZOJ 4771 主席树+倍增+set

    思路: 因为有深度的限制,并且我们是在线段树上维护权值,所以我们把点按照dep排序,然后一个一个修改...主席树的下标就是dfs序,子树的查询就是区间查询... 但是发现这样怎么去维护LCA呢...因 ...

  6. BZOJ 3674/BZOJ 3673 主席树

    思路: 主席树维护可持久化数组 剩下的就是普通的并查集了- //By SiriusRen #include <cstdio> #include <cstring> #inclu ...

  7. BZOJ 3123 主席树 启发式合并

    思路: 主席树 搞树上的k大 x+y-lca(x,y)-fa(lca(x,y)) 按照size小树往大树上插 启发式合并 n*log^2n的 搞定~ //By SiriusRen #include & ...

  8. BZOJ 4448 主席树+树链剖分(在线)

    为什么题解都是离线的-- (抄都没法抄) 搞一棵主席树 1 操作 新树上的当前节点设成1 2 操作 查max(i-xx-1,0)那棵树上这条路径上有多少个点是1 让你找经过了多少个点 查的时候用dee ...

  9. BZOJ 3932 - 主席树

    传送门 题目分析 在只打会主席树模板的情况下做了这道题,也算是深有体会. 首先任务可以差分:一个任务是(s, e, p), 则在s处+1, 在e+1处-1,符合前缀.但是我们要查询指定时间的前k任务之 ...

随机推荐

  1. Win7下IE11点击无反应的解决方法

    平台:win7 sp1 32bit 问题:点击Internet Explorer在开始菜单.快捷栏的图标和安装目录下的程序均没有反应,鼠标在变成漏斗后恢复原状再无反应.但搜狗浏览器和360浏览器下使用 ...

  2. GO语言学习(十四)Go 语言数组

    Go 语言数组 Go 语言提供了数组类型的数据结构. 数组是具有相同唯一类型的一组已编号且长度固定的数据项序列,这种类型可以是任意的原始类型例如整形.字符串或者自定义类型. 相对于去声明number0 ...

  3. cocos2d-x中六种持续性动作

    持续性动作: (一) 位置变化动作 Move by to Jump by to (二) 属性变化动作 Scale by to Rotate by to Fade in out to Tint to b ...

  4. amazeui学习笔记--css(常用组件3)--按钮组Button-group

    amazeui学习笔记--css(常用组件3)--按钮组Button-group 一.总结 1.按钮组用法:把一系列要使用的 .am-btn 按钮放入 .am-btn-group . 2.按钮工具栏: ...

  5. 边缘独立(marginal independent)的理解及举例

    1. 定义 ∀xi∈dom(X),yj∈dom(Y),yk∈dom(Y),如果满足, P(X=xi|Y=yj)==P(X=xi|Y=yk)P(X=Xi) 则称随机变量 X 边缘独立于随机变量 Y. 理 ...

  6. POJ 3187 Backward Digit Sums 枚举水~

    POJ 3187  Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3   1   2   4  他可以相邻 ...

  7. POJ 1466 Girls and Boys (ZOJ 1137 )最大独立点集

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=137 http://poj.org/problem?id=1466 题目大意: ...

  8. 【例题 6-17 UVa 10562】Undraw the Trees

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟+递归 [代码] #include <bits/stdc++.h> using namespace std; con ...

  9. UML学习总结(3)——StarUML指导手册

    StarUML使用说明-指导手册 原著:Stephen Wong            翻译:火猴 StarUML是一种生成类图和其他类型的统一建模语言(UML)图表的工具.这是一个用Java语言描述 ...

  10. 异步载入JS

      平时最常使用的就是这样的同步载入形式:    <script src="http://yourdomain.com/script.js"></script&g ...