达神主席树讲解传送门: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. MIT jos 6.828 Fall 2014 训练记录(lab 4)

    源代码参见我的github: https://github.com/YaoZengzeng/jos Part A: Multiprocessor Support and Cooperative Mul ...

  2. 数论+spfa算法 bzoj 2118 墨墨的等式

    2118: 墨墨的等式 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1283  Solved: 496 Description 墨墨突然对等式很感兴 ...

  3. 伪多项式时间算法Pseudo-polynomial Algorithms-----geeksforGeek 翻译

    原创翻译加学习笔记,方便国人学习算法知识! 原文链接http://www.geeksforgeeks.org/pseudo-polynomial-in-algorithms/ 什么是伪多项式? 当一个 ...

  4. make命令--基础

    一.Make的概念 Make这个词,英语的意思是"制作".Make命令直接用了这个意思,就是要做出某个文件.比如,要做出文件a.txt,就可以执行下面的命令. $ make a.t ...

  5. poj2387 Til the Cows Come Home 最短路径dijkstra算法

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  6. Codeforces Round #274 Div.1 C Riding in a Lift --DP

    题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...

  7. 2014 Super Training #7 B Continuous Login --二分

    原题:ZOJ 3768 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3768 一个结论:一个正整数总能用不超过三个前n项相 ...

  8. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  9. sqlzoo.net刷题3

    Find the continents where all countries have a population <= 25000000. Then find the names of the ...

  10. 【.NET进阶】函数调用--函数栈

    原文:http://www.cnblogs.com/rain-lei/p/3622057.html 函数调用大家都不陌生,调用者向被调用者传递一些参数,然后执行被调用者的代码,最后被调用者向调用者返回 ...