CRB and Queries

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 231    Accepted Submission(s): 41

Problem Description
There are N boys in CodeLand.
Boy i has his coding skill Ai.
CRB wants to know who has the suitable coding skill.
So you should treat the following two types of queries.
Query 1: 1 l v
The coding skill of Boy l has changed to v.
Query 2: 2 l r k
This is a report query which asks the k-th smallest value of coding skill between Boy l and Boy r(both inclusive).
 
Input
There are multiple test cases. 
The first line contains a single integer N.
Next line contains N space separated integers A1, A2, …, AN, where Ai denotes initial coding skill of Boy i.
Next line contains a single integer Q representing the number of queries.
Next Q lines contain queries which can be any of the two types.
1 ≤ N, Q ≤ 105
1 ≤ Ai, v ≤ 109
1 ≤ l ≤ r ≤ N
1 ≤ k ≤ r – l + 1

 
Output
For each query of type 2, output a single integer corresponding to the answer in a single line.
 
Sample Input
5
1 2 3 4 5
3
2 2 4 2
1 3 6
2 2 4 2
 
Sample Output
3
4
 
Author
KUT(DPRK)
 
Source
 
//动态空间第k大
#include <algorithm>
#include <cstdio>
#include <utility>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std; #define X first
#define Y second const int MX = ;
const int INF = ;
struct Node;
typedef pair <Node*, Node*> Pair;
Node *null; struct Node {
int val, size;
Node *left, *right; Node (int val, int size, Node *left, Node *right) :
val(val), size(size), left(left), right(right) {} Node *Update() {
size = left->size + + right->size;
return this;
} int askLess(const int &x) {
if (this == null) return ;
if (val < x) return left->size + + right->askLess(x);
else return left->askLess(x);
}
Pair Split(int n, int type) {
if (this == null)
return make_pair(null, null); if (type == && val == n) {
return make_pair(left, right);
} if (!(val < n)) {
Pair ret = left->Split(n, type);
left = ret.Y;
return make_pair(ret.X, this->Update());
} Pair ret = right->Split(n, type);
right = ret.X;
return make_pair(this->Update(), ret.Y);
}
}; struct BST {
Node *root; inline int ran() {
static int x = ;
x += (x << ) + ;
return x & ;
}
Node *Merge(Node *a, Node *b) {
if (a == null) return b;
if (b == null) return a; if (ran() % (a->size + b->size) < a->size) {
a->right = Merge(a->right, b);
return a->Update();
} b->left = Merge(a, b->left);
return b->Update();
} void add(int b) {
Pair ret = root->Split(b, );
root = Merge(ret.X, Merge(new Node(b, , null, null), ret.Y));
}
void del(int b) {
Pair ret = root->Split(b, );
root = Merge(ret.X, ret.Y);
}
int getLess(int b) {
return root->askLess(b);
} } tr[MX]; struct Query {
int t, l, r, k; void in() {
scanf("%d", &t);
if (t == ) scanf("%d%d", &l, &k);
else scanf("%d%d%d", &l, &r, &k);
} } q[MX]; int a[MX];
int p[MX], pn;
int L; void add(int p, int v) {
for (p++; p <= pn; p += p & -p) {
tr[p].add(v);
}
}
void del(int p, int v) {
for (p++; p <= pn; p += p & -p) {
tr[p].del(v);
}
} int calc(int l, int r, int k) {
int cur = ;
for (int i = L; i; i /= ) {
int u = cur + i;
if (u > pn) continue; int cnt = tr[u].getLess(r + ) - tr[u].getLess(l);
if (cnt >= k) continue;
cur = u, k -= cnt;
}
return cur + ;
} int main() {
int n, Q;
int i;
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(scanf("%d", &n)!=EOF){ for (i = ; i <= n; i++) {
scanf("%d", a + i);
p[pn++] = a[i];
} scanf("%d", &Q);
for (i = ; i <= Q; i++) {
q[i].in();
if (q[i].t == ) p[pn++] = q[i].k;
} sort(p, p + pn);
pn = unique(p, p + pn) - p;
for (L = ; L <= pn; L *= );
L /= ; null = new Node(, , NULL, NULL);
for (i = ; i <= pn; i++)
tr[i].root = null; for (i = ; i <= n; i++) {
a[i] = lower_bound(p, p + pn, a[i]) - p;
add(a[i], i);
} for (i = ; i <= Q; i++)
if (q[i].t == ) q[i].k = lower_bound(p, p + pn, q[i].k) - p; for (i = ; i <= Q; i++) {
if (q[i].t == ) {
int u = q[i].l;
del(a[u], u);
a[u] = q[i].k;
add(a[u], u); } else {
int u = calc(q[i].l, q[i].r, q[i].k);
printf("%d\n", p[u - ]);
}
}
}
return ;
}

hdu5412(动态区间第k大)的更多相关文章

  1. ZOJ 1112 Dynamic Rankings【动态区间第K大,整体二分】

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112 题意: 求动态区间第K大. 分析: 把修改操作看成删除与增加 ...

  2. ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)

    题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...

  3. ZOJ2112--Dynamic Rankings (动态区间第k大)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  4. 整体二分求动态区间第k大

    比树状数组套主席树不知道高到哪里去了,solve(l,r,L,R)就是对于L,R的操作区间的答案都在l,r区间里,然后递归下去 复杂度O(nlognlogn),每个操作会执行logn次就是o(nlog ...

  5. Dynamic_Rankings(动态区间第k大)

    ZOJ - 2112 \[ \ \] (那些说这道题是树状数组套主席树的人一定对主席树有误解!) 这里我们用树状数组套线段树来解决来写 首先 , 我们需要有n棵线段树(不是\(n^2\)空间,别慌) ...

  6. 整体二分(模板二)动态区间第K大

    这才是更一般的二分写法--HDU5412 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio>// ...

  7. 动态区间第K大

    整体二分. 主要需要注意的一点是,对于每个删除操作,若删除操作被算入贡献,则最开始的插入操作也一定会被算入,所以不必担心删除删错. #include<cstdio> #include< ...

  8. 静态区间第k大(主席树)

    POJ 2104为例(主席树入门题) 思想: 可持久化线段树,也叫作函数式线段树,也叫主席树(高大上). 可持久化数据结构(Persistent data structure):利用函数式编程的思想使 ...

  9. 【ZOJ2112】【整体二分+树状数组】带修改区间第k大

    The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...

随机推荐

  1. GIMP学习笔记

    参考文献: ① http://www.gimp.org/docs/userfaq.html GIMP是GNU Image Manipulation Program的缩写,sired by Spence ...

  2. Zookeeper相关知识

    一.Zookeeper是什么? Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务. ...

  3. Android_Nexus4_屏幕截图

    1. 一般都是 音量-键 + 电源键,同时按一秒以上 2. 3.

  4. HDU5785 manacher+差分数组

    用manacher算法O(n)求出所有的回文半径.有了回文半径后,就可以求出L[i]表示以i结尾的回文串的起始位置的和R[i]表示以i起始的回文串的结尾位置的和,然后就可以求出答案了,这里要注意奇偶长 ...

  5. linux学习笔记2-命令总结4

    帮助命令 help - 帮助命令 man - 获取帮助信息 用户管理命令 useradd - 添加新用户 passwd - 设置用户密码 who - 显示所有用户 w - 查看更详细的用户信息 use ...

  6. Java中的数学运算BigDecimal

    Math类 package ch7; /** * Created by Jiqing on 2016/11/24. */ public class MathDemo { public static v ...

  7. numpy库的常用知识

    为什么有numpy这个库呢?准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1,2, ...

  8. Java编程思想笔记

    打好java基础 后续会增加相应基础笔试题 目录如下 1 对象导论2 一切都是对象3 操作符4 控制执行流程5 初始化与清理6 访问控制权限7 复用类8 多态9 接口10 内部类11 持有对象12 通 ...

  9. activiti jbpm相关资源

    Activiti 5.16 用户手册 http://www.mossle.com/docs/activiti/index.html jBPM 4.4开发指南 http://www.mossle.com ...

  10. find和xargs

    find 命令的一般形式为 find pathname -options [-print -exec -ok] 让我们来看看该命令的参数: pathname find命令所查找的目录路径.例如用.来表 ...