POJ 2104 && POJ 2761 (静态区间第k大,主席树)
查询区间第K大,而且没有修改。
使用划分树是可以做的。
作为主席树的入门题,感觉太神奇了,Orz
- /* ***********************************************
- Author :kuangbin
- Created Time :2013-9-4 20:13:20
- File Name :POJ2104.cpp
- ************************************************ */
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <queue>
- #include <set>
- #include <map>
- #include <string>
- #include <math.h>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- const int MAXN = ;
- const int M = MAXN * ;
- int n,q,m,tot;
- int a[MAXN], t[MAXN];
- int T[M], lson[M], rson[M], c[M];
- void Init_hash()
- {
- for(int i = ; i <= n;i++)
- t[i] = a[i];
- sort(t+,t++n);
- m = unique(t+,t++n)-t-;
- }
- int build(int l,int r)
- {
- int root = tot++;
- c[root] = ;
- if(l != r)
- {
- int mid = (l+r)>>;
- lson[root] = build(l,mid);
- rson[root] = build(mid+,r);
- }
- return root;
- }
- int hash(int x)
- {
- return lower_bound(t+,t++m,x) - t;
- }
- int update(int root,int pos,int val)
- {
- int newroot = tot++, tmp = newroot;
- c[newroot] = c[root] + val;
- int l = , r = m;
- while(l < r)
- {
- int mid = (l+r)>>;
- if(pos <= mid)
- {
- lson[newroot] = tot++; rson[newroot] = rson[root];
- newroot = lson[newroot]; root = lson[root];
- r = mid;
- }
- else
- {
- rson[newroot] = tot++; lson[newroot] = lson[root];
- newroot = rson[newroot]; root = rson[root];
- l = mid+;
- }
- c[newroot] = c[root] + val;
- }
- return tmp;
- }
- int query(int left_root,int right_root,int k)
- {
- int l = , r = m;
- while( l < r)
- {
- int mid = (l+r)>>;
- if(c[lson[left_root]]-c[lson[right_root]] >= k )
- {
- r = mid;
- left_root = lson[left_root];
- right_root = lson[right_root];
- }
- else
- {
- l = mid + ;
- k -= c[lson[left_root]] - c[lson[right_root]];
- left_root = rson[left_root];
- right_root = rson[right_root];
- }
- }
- return l;
- }
- int main()
- {
- //freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- while(scanf("%d%d",&n,&q) == )
- {
- tot = ;
- for(int i = ;i <= n;i++)
- scanf("%d",&a[i]);
- Init_hash();
- T[n+] = build(,m);
- for(int i = n;i ;i--)
- {
- int pos = hash(a[i]);
- T[i] = update(T[i+],pos,);
- }
- while(q--)
- {
- int l,r,k;
- scanf("%d%d%d",&l,&r,&k);
- printf("%d\n",t[query(T[l],T[r+],k)]);
- }
- }
- return ;
- }
POJ 2104 && POJ 2761 (静态区间第k大,主席树)的更多相关文章
- POJ 2104 K-th Number (区间第k大)
题意:给定一个序列A,接下来又m个询问,每个询问输出A[L,R]中的第K大.(保证第k大存在) 思路: 我想拿来练习“可持久化线段树”的,搜到这个比较巧的算法也可以解决这个问题,叫“归并树?.大概的思 ...
- HDU2665 求区间第K大 主席树
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665 代码: //#include<bits/stdc++.h> #include< ...
- POJ-2104-K-th Number(区间第K大+主席树模板题)
Description You are working for Macrohard company in data structures department. After failing your ...
- 静态区间第k大(归并树)
POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...
- HDU3473--Minimum Sum(静态区间第k大)
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 43315 Accepted: 14296 Ca ...
- 主席树(静态区间第k大)
前言 如果要求一些数中的第k大值,怎么做? 可以先就这些数离散化,用线段树记录每个数字出现了多少次. ... 那么考虑用类似的方法来求静态区间第k大. 原理 假设现在要有一些数 我们可以对于每个数都建 ...
- 可持久化线段树(主席树)——静态区间第k大
主席树基本操作:静态区间第k大 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=2e5+, ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
随机推荐
- 字体格式类型(.eot/.otf/.woff/.svg)
@font-face语句是css中的一个功能模块,用于实现网页字体多样性的模块(设计者可随意指定字体,不需要考虑浏览者电脑上是否安装). @font-face文件 而由于网页中使用的字体类型,也是各浏 ...
- python进阶学习之高阶函数
高阶函数就是把函数当做参数传递的一种函数, 例如: 执行结果: 1.map()函数 map()接收一个函数 f 和一个list, 并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 l ...
- 【读书笔记】Android的Ashmem机制学习
Ashmem是安卓在linux基础上添加的驱动模块,就是说安卓有linux没有的功能. Ashmem模块在内核层面上实现,在运行时库和应用程序框架层提供了访问接口.在运行时库层提供的是C++接口,在应 ...
- Struts 2 - Hello World Example
As you learnt from the Struts 2 architecture, when you click on a hyperlink or submit an HTML form i ...
- hdu 5475 模拟计算器乘除 (2015上海网赛H题 线段树)
给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第i个结点的值 替换成 ...
- day6面向对象
面向对象介绍(http://www.cnblogs.com/alex3714/articles/5188179.htm) 世界万物,皆可分类 世界万物,皆为对象 只要是对象,就 ...
- python日期格式转换小记
utc格林时间==>东八区北京时间 原始日期格式: utctime = ‘2016-07-26 10:08:29’ localtime = (datetime.datetime.fromtime ...
- 易普优APS混流排序算法助力汽车整车厂的均衡生产
一.汽车整车厂生产排序的难点 “ 冲压-焊接-涂装-总装”是汽车整车生产的四大工艺类型,它们存在上下游关联关系,每个车间都有自己的优化排序目标,汽车混流生产模式使得生产过程更加复杂,从而生产管控的难度 ...
- python日常总结
1. post请求中是否可以在url中携带请求体信息? 可以.Get请求时,请求体放在URL中; POST请求,请求体既可以是Form表单中的数据 也可以在请求的URL地址中放请求体信息. 如: &l ...
- nginx、php-fpm、swoole HTTP/TCP压测对比
本次测试是在win7下docker环境中进行压测,共创建一个nginx容器.一个php-fpm容器和一个swoole容器,客户端请求nginx服务器,nginx接收用户访问请求并转发给php-fpm, ...