达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026


2016-02-23:真的是模板题诶,主席树模板水过。今天新校网不好,没有评测,但我立下flag这个代码一定能A。我的同学在自习课上考语文,然而机房党都跑到机房来避难了\(^o^)/~

2016-02-24:果然A乐~~~,我们机房党又躲过啦数学考试。良心不安啊

#include<cstdio>
#include<cstring>
#include<algorithm>
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
#define for2(i,a,n) for(int i=(a);i<(n);i++)
#define for3(i,a,n) for(int i=(a);i>=(n);i--)
#define for4(i,a,n) for(int i=(a);i>(n);i--)
#define CC(i,a) memset(i,a,sizeof(i))
#define MID (l+r)>>1
#define read(x) x=getint()
using namespace std;
const int N=100010;
inline int max(const int &a,const int &b){return a>b?a:b;}
inline int min(const int &a,const int &b){return a<b?a:b;}
inline void swapp(int &a,int &b){int c=a;a=b;b=c;}
inline int getint(){char c;int ret=0,k=1;for(c=getchar();c<'0'||c>'9';c=getchar())if(c=='-')k=-1;for(;c>='0'&&c<='9';c=getchar())ret=ret*10+c-'0';return ret*k;}
int key,a[N],id[N],b[N],n,m,root[N],cnt=0;
struct node{int l,r,s;}T[N*50];
inline bool cmp(int x,int y){return a[x]<a[y];}
inline void update(int l,int r,int &pos){
T[++cnt]=T[pos]; pos=cnt; T[pos].s++;
if (l==r) return;
int m=MID;
if (key<=m) update(l,m,T[pos].l); else update(m+1,r,T[pos].r);
}
inline int query(int l,int r,int x,int y,int k){
if (l==r) return l;
int m=MID,s=T[T[y].l].s-T[T[x].l].s;
if (k<=s) return query(l,m,T[x].l,T[y].l,k); else return query(m+1,r,T[x].r,T[y].r,k-s);
}
int main(){
read(n); read(m);
for1(i,1,n) read(a[i]),id[i]=i;
sort(id+1,id+n+1,cmp);
for1(i,1,n) b[id[i]]=i;
for1(i,1,n){
root[i]=root[i-1]; key=b[i];
update(1,n,root[i]);
}int x,y;
while (m--){
read(x); read(y); read(key);
printf("%d\n",a[id[query(1,n,root[x-1],root[y],key)]]);
}return 0;
}

这样就可以了,主席树水过了,然而这是DaD3zZ几年前就虐的东西~~(╯﹏╰)b,本蒟蒻还得努力啊。

【POJ 2104】 K-th Number 主席树模板题的更多相关文章

  1. 主席树:POJ2104 K-th Number (主席树模板题)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44952   Accepted: 14951 Ca ...

  2. SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]

    题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...

  3. POJ 2104 K-th Number(主席树模板题)

    http://poj.org/problem?id=2104 题意:求区间$[l,r]$的第k小. 思路:主席树不好理解啊,简单叙述一下吧. 主席树就是由多棵线段树组成的,对于数组$a[1,2...n ...

  4. 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题

    达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...

  5. poj2104 主席树模板题

    题意 给出n个数字组成的数字序列,有m组询问.每次询问包含三个数字l,r,k.对于每个询问输出序列区间[l,r]中第k大的数字. 分析 这是主席树的模板题,套板子就可以 #include <cs ...

  6. hdu2665(主席树模板题)

    hdu2665 题意 求区间第 k 小. 分析 参考 这类题目做法挺多的,例如 划分树. 这里使用主席树再写一发,不得不说主席树相比而言要好写的多,比起普通线段树,主席树就是复用了线段树共有的信息. ...

  7. POJ-2104-K-th Number(区间第K大+主席树模板题)

    Description You are working for Macrohard company in data structures department. After failing your ...

  8. POJ 2104 主席树模板题

    #include <iostream> #include <cstdio> #include <algorithm> int const maxn = 200010 ...

  9. POJ2104 K-th Number 划分树 模板题啊

    /*Source Code Problem: 2104 User: 96655 Memory: 14808K Time: 1282MS Language: G++ Result: Accepted S ...

随机推荐

  1. Could not open INSTALL.LOG file

    今天卸载MailEnable时弹出这个提示,用360这样的工具卸载问题依旧,折腾了大半天没解决 UNWISE Could not open INSTALL.LOG file 本人有强迫症,虽然这个软件 ...

  2. [反编译U3D]Decompile Unity Resources

    工具说明 反编译unity project资源文件,包括ios,android,pc等平台资源,仅供学习使用! 以下工具任选其一,未有特殊说明是可以同时提取unity3.x,unity4.x,unit ...

  3. iOS开发如何提高(from 唐巧的博客)

    http://blog.devtang.com/blog/2014/07/27/ios-levelup-tips/

  4. webBroser获取cookie

    //取当前webBrowser登录后的Cookie值 [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError ...

  5. Hill密码

    希尔密码(Hill Password)是运用基本矩阵论原理的替换密码,由Lester S. Hill在1929年发明.每个字母当作26进制数字:A=, B=, C=... 一串字母当成n维向量,跟一个 ...

  6. HTML---总结

    (1) margin负值能让没有宽度的盒子变大. 对于有宽度的盒子,没有浮动,只有margin-top 和margin-left 有效:如果浮动,margin-浮动方向 有效,-top有效,margi ...

  7. Go cron定时任务的用法

    cron是什么 cron的意思就是:计划任务,说白了就是定时任务.我和系统约个时间,你在几点几分几秒或者每隔几分钟跑一个任务(job),就那么简单. cron表达式 cron表达式是一个好东西,这个东 ...

  8. C语言 百炼成钢9

    //题目25:求1+2!+3!+...+20!的和 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib ...

  9. JS 之BOM

    BOM的核心对象是window,表示浏览器的一个实例. 使用框架时,window.top对象指顶层框架,也就是浏览器窗口.window.parent对象指包含当前窗口的框架,也就是父框架.window ...

  10. 在opencv3中利用SVM进行图像目标检测和分类

    采用鼠标事件,手动选择样本点,包括目标样本和背景样本.组成训练数据进行训练 1.主函数 #include "stdafx.h" #include "opencv2/ope ...