POJ2104Kth Number
整体二分模板题, 有些细节需要注意
#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
#include<cstring>
using namespace std;
inline int read()
{
int x = 0, flag = 1;
char c;
while(! isgraph(c = getchar()))
if(c == '-')
flag *= - 1;
while(isgraph(c))
x = x * 10 + c - '0', c = getchar();
return x * flag;
}
void println(int x)
{
if(x < 0)
putchar('-'), x *= - 1;
if(x == 0)
putchar('0');
int ans[10 + (1 << 4)], top = 0;
while(x)
ans[top ++] = x % 10, x /= 10;
for(; top; top --)
putchar(ans[top - 1] + '0');
putchar('\n');
}
const int MAXN = (int)1e5 + (1 << 5), MAXM = (int)5e4 + (1 << 4);
const int oo = INT_MAX;
int a[MAXN];
struct query
{
int L, R, k, ID;
query(){}
query(int L, int R, int k, int ID): L(L), R(R), k(k), ID(ID){}
}Q[MAXN];
int res[MAXN], sum[MAXN];
int nxt[MAXN][2], pre[MAXN][2];
int _a[MAXN], map[MAXN];
query _Q[MAXM];
int ans[MAXM];
void solve(int mn, int mx, int L, int R, int QL, int QR)
{
if(mn >= mx)
{
for(int i = QL; i <=QR; i ++)
ans[Q[i].ID] = mn;
return;
}
int cur = (mn + mx) >> 1;
for(int i = L; i <= R; i ++)
res[i] = (a[i] <= cur);
sum[L] = res[L];
for(int i = L + 1; i <= R; i ++)
sum[i] = sum[i - 1] + res[i];
pre[L][res[L]] = L, pre[L][res[L] ^ 1] = - oo;
for(int i = L + 1; i <= R; i ++)
pre[i][res[i]] = i, pre[i][res[i] ^ 1] = pre[i - 1][res[i] ^ 1];
nxt[R][res[R]] = R, nxt[R][res[R] ^ 1] = oo;
for(int i = R - 1; i >= L; i --)
nxt[i][res[i]] = i, nxt[i][res[i] ^ 1] = nxt[i + 1][res[i] ^ 1];
int Ltop = L, Rtop = R;
for(int i = L; i <= R; i ++)
{
if(res[i])
_a[Rtop] = a[i], map[i] = Rtop --;
else
_a[Ltop] = a[i], map[i] = Ltop ++;
}
int mid = Rtop;
for(int i = L; i <= R; i ++)
a[i] = _a[i];
Ltop = QL, Rtop = QR;
for(int i = QL; i <= QR; i ++)
{
int cnt = sum[Q[i].R] - sum[Q[i].L - 1];
if(cnt < Q[i].k)
{
Q[i].L = map[nxt[Q[i].L][0]];
Q[i].R = map[pre[Q[i].R][0]];
Q[i].k -= cnt; //当 cnt < k 时要在k中减去cnt
if(Q[i].L > Q[i].R)
swap(Q[i].L, Q[i].R); //记得要判断是否需要调换顺序
_Q[Ltop ++] = Q[i];
}
else if (cnt >= Q[i].k) //当 cnt >= k时则不需要减去cnt
{
Q[i].L = map[nxt[Q[i].L][1]];
Q[i].R = map[pre[Q[i].R][1]];
if(Q[i].L > Q[i].R)
swap(Q[i].L, Q[i].R);
_Q[Rtop --] = Q[i];
}
}
for(int i = QL; i <= QR; i ++)
Q[i] = _Q[i];
for(int i = L; i <= R; i ++)
sum[i] = 0;
solve(cur + 1, mx, L, mid, QL, Rtop);
solve(mn, cur, mid + 1, R, Ltop, QR);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("POJ2104.in", "r", stdin);
freopen("POJ2104.out", "w", stdout);
#endif
int n = read();
int m = read();
int mx = - oo, mn = oo;
for(int i = 1; i <= n; i ++)
a[i] = read(), mx = max(mx, a[i]), mn = min(mn, a[i]);
for(int i = 1; i <= m; i ++)
{
int L = read(), R = read(), k = read();
Q[i] = query(L, R, k, i);
}
memset(sum, 0, sizeof(sum));
solve(mn, mx, 1, n, 1, m);
for(int i = 1; i <= m; i ++)
println(ans[i]);
}
POJ2104Kth Number的更多相关文章
- 平方分割poj2104K-th Number
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 59798 Accepted: 20879 Ca ...
- POJ-2104-K-th Number(区间第K大+主席树模板题)
Description You are working for Macrohard company in data structures department. After failing your ...
- POJ2104-- K-th Number(主席树静态区间第k大)
[转载]一篇还算可以的文章,关于可持久化线段树http://finaltheory.info/?p=249 无修改的区间第K大 我们先考虑简化的问题:我们要询问整个区间内的第K大.这样我们对值域建线段 ...
- 【静态主席树】POJ2104-K-th Number
求区间第k大.裸线段树. 莫队版本:☆ #include<iostream> #include<cstdio> #include<cstring> #include ...
- 【序列莫队+二分答案+树状数组】POJ2104-K-th Number
[题目大意] 给出一个长度为n的序列和m组查询(i,j,k),输出[i,j]中的第k大数. [思路] 先离散化然后莫队分块.用树状数组来维护当前每个值的个数,然后对于每次询问二分答案即可. 又一次实力 ...
- POJ-2104-Kth Number(主席树)
链接: https://vjudge.net/problem/POJ-2104#author=malic 题意: 给定一个数组 a[1...n],数组元素各不相同,你的程序要对每次查询Q(i,j,k) ...
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
随机推荐
- The 2018 ACM-ICPC China JiangSu Provincial Programming Contest J. Set
Let's consider some math problems. JSZKC has a set A=A={1,2,...,N}. He defines a subset of A as 'Meo ...
- Linux学习-什么是 daemon 与服务 (service)
『常驻在记体体中的程序,且可以提供 一些系统或网络功能,那就是服务』.而服务一般的英文说法是『 service 』. 那么 daemon 与 service 有关啰?否则为什么都能够提供 某些系统或网 ...
- 03011_HttpServletRequest
1.HttpServletRequest概述 (1)我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代 ...
- Django Model two
Django_model: eg: class XXXX(models.Model): nid = models.AutoField(primary_Key=True) name = models.C ...
- Scala学习-02-方法
算数和操作符重载 所有的操作符都是方法. a + b 是一种缩写形式 : a .+ b “+”是方法名(操作符重载) ++和—— Scala中并没有“++”和“——”.需要使用“+=”和“-=” ...
- ios开发讲解之anchorPoint和position详解
引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置? CALayer的position点是哪一点呢? anchorPoint与posi ...
- python + selenium - 自动化环境搭建
1. 安装python (1)下载地址:https://www.python.org/downloads/windows/ (2)安装方式:默认安装即可 (3)环境变量配置:打开[系统属性]-[环境变 ...
- Frequent values(ST)
描述 You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to ...
- 2017"百度之星"程序设计大赛 - 资格赛
度度熊与邪恶大魔王 Accepts: 3666 Submissions: 22474 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- 论蛋疼的调戏matrix67的首页
唔,初学js,然后拿matrix67的首页玩玩23333 console 里输入以下这句话 for(var i = 1; i <= 400; i++){var td = document.get ...