主席树——求静态区间第k大】的更多相关文章

例题:poj2104 http://poj.org/problem?id=2104 讲解:http://blog.sina.com.cn/s/blog_6022c4720102w03t.html http://seter.is-programmer.com/posts/31907.html 刚刚根据以上2篇博客看懂,还没到写讲解的水平 #include<cstdio> #include<algorithm> using namespace std; ; ],r_child[N*],…
前言 如果要求一些数中的第k大值,怎么做? 可以先就这些数离散化,用线段树记录每个数字出现了多少次. ... 那么考虑用类似的方法来求静态区间第k大. 原理 假设现在要有一些数 我们可以对于每个数都建一棵新的线段树,用来记录出现每个数字出现了多少次的前缀和. 那么假设要求区间[l,r]的第k大,将第r棵线段树减去第l-1棵线段树,像上面求所有数的第k大一样来求就可以了. 但是,对于每一个数都建一个线段树显然会爆空间. 现在考虑如何节约空间. 假设现在有四个数1 4 2 3,依次加入,可以这样处理…
主席树基本操作:静态区间第k大 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=2e5+,SIZE=MAXN*; int N,M,K; int tmp[MAXN],A[MAXN],rt[MAXN],order[MAXN]; int sz,lson[SIZE],rson[SIZE],sum[SIZE]; int init(int l,int r){ int cur=++sz; if(l<r){…
You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment. Th…
poj2104找了个板子..,但是各种IO还可以进行优化 /* 找区间[l,r]第k大的数 */ #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; #define maxn 100005 ]; int n,q,a[maxn],b[maxn],m,rt[maxn],size; int build(int l,int r){…
#include<stdio.h> #include<algorithm> #include<string.h> #define MAXN 100010 #define MAXM 5050 using namespace std; struct tr{ int l,r,sum; }; tr tree[MAXN*]; int root[MAXN]; int cnt; int jilu[MAXN],from[MAXN]; void updat(int s,int e,int…
POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数,否则递归的对子树进行求解并相加. 使用STL中的merge对子序列进行合并及排序. 时间复杂度O(nlogn+mlog3n) 代码(vector实现): #include<cstdio> #include<algorithm> #include<vector> #inclu…
K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 43315   Accepted: 14296 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse…
Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3047    Accepted Submission(s): 701 Problem Description You are given N positive integers, denoted as x0, x1 ... xN-1. Then give you…
题目背景 这是个非常经典的主席树入门题——静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示序列的长度和查询的个数. 第二行包含N个整数,表示这个序列各项的数字. 接下来M行每行包含三个整数l, r, kl,r,k , 表示查询区间[l, r][l,r]内的第k小值. 输出格式: 输出包含k行,每行1个整数,依次表示每一次查询的结果 输…