【POJ】2104 K-th Number 主席树
来源:互联网 发布:python 画时间轴 分钟 编辑:程序博客网 时间:2024/05/16 09:01
题目传送门
主席树什么真的好迷啊……
写一下自己对主席树的理解吧,好像就是一个线段树的前缀和。
首先我们先建出一棵空树,作为之后线段树的基础。
对每一个节点都建一棵线段树,但是也不是完全建出整棵线段树,而是对于不需要修改的节点就直接调用当前节点的对应节点作为儿子,对于需要修改的节点就建出新的节点作为儿子,并对新建出来节点进行修改。
然后发现这样的数据结构就可以有求区间第k大等骚操作了。
博主对于主席树还不是很熟练,现在还只会没有修改的求区间第k大,如果之后学到了更多的骚操作再来填坑吧。
回到这题,就是一个求区间第k大的裸题,直接主席树上就行了。
p.s.主席树要用到动态开点的知识,需要用到的节点为2*n+m*log2(n),“但是最好把内存开的大一些,防止爆炸。”——来自ZZK的教诲
附上AC代码:
#include <cstdio>#include <cctype>#include <algorithm>#define mid (l+r>>1)using namespace std;const int N=1e5+10;struct tree{ int lt,rt,sum;}t[2000010];int n,m,a[N],b[N],root[N],size,x,y,w;inline char nc(void){ static char ch[100010],*p1=ch,*p2=ch; return p1==p2&&(p2=(p1=ch)+fread(ch,1,100010,stdin),p1==p2)?EOF:*p1++;}inline void read(int &a){ static char c=nc();int f=1; for (;!isdigit(c);c=nc()) if (c=='-') f=-1; for (a=0;isdigit(c);a=(a<<3)+(a<<1)+c-'0',c=nc()); a*=f;return;}inline void ist(int pre,int &k,int l,int r,int w){ t[k=++size]=t[pre],++t[k].sum; if (l==r) return; if (mid>=w) ist(t[pre].lt,t[k].lt,l,mid,w); else ist(t[pre].rt,t[k].rt,mid+1,r,w); return;}inline int query(int pre,int k,int l,int r,int w){ if (l==r) return b[l]; int c=t[t[k].lt].sum-t[t[pre].lt].sum; if (c>=w) return query(t[pre].lt,t[k].lt,l,mid,w); else return query(t[pre].rt,t[k].rt,mid+1,r,w-c);}int main(void){ read(n),read(m); for (int i=1; i<=n; ++i) read(a[i]),b[i]=a[i]; sort(b+1,b+1+n); for (int i=1; i<=n; ++i) ist(root[i-1],root[i],1,n,lower_bound(b+1,b+1+n,a[i])-b); while (m--) read(x),read(y),read(w),printf("%d\n",query(root[x-1],root[y],1,n,w)); return 0;}
阅读全文
0 0
- [POJ 2104]K-th Number 主席树
- poj 2104 K-th Number (主席树)
- poj 2104 K-th Number【主席树】
- POJ 2104 K-th Number [主席树]
- POJ 2104 K-th Number 主席树
- 主席树 poj 2104 K-th Number
- POJ 2104 K-th Number 主席树
- POJ 2104 K-th Number(主席树)
- 【POJ 2104 K-th Number】+ 主席树
- 【POJ】2104 K-th Number 主席树
- POJ-2104:K-th Number(主席树)
- 【主席树】POJ 2014 K-th Number
- poj 2104 K-th Number(划分树 or 主席树)
- POJ 2104 K-th Number (主席树 || 划分树)
- POJ 2104 K-th Number (划分树 / 主席树)
- poj 2104 K-th Number(划分树裸题&主席树)
- POJ 2104 K-th Number 静态主席树(裸
- POJ 2104 K-th Number 经典主席树
- Java入门笔记第四课(类)
- tomcat-maven-plugin的使用
- 单词树(最优二叉树)
- 分层图的一些人生经验
- Coursera吴恩达《优化深度神经网络》课程笔记(1)-- 深度学习的实用层面
- 【POJ】2104 K-th Number 主席树
- 用函数对数组进行初始化,逆置,清空
- codeforces 583B B. Once Again...(LIS)
- 数据存储
- Poj
- linux 下 top 命令的含义
- [codevs1647]炮兵阵地
- CodeForces 868C Qualification Rounds
- caffe中Python层的使用