hdu 5412 CRB and Queries
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5412
CRB and Queries
Description
There are $N$ boys in CodeLand.
Boy i has his coding skill $A_{i}$.
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 $A_{1}, A_{2}, …, A_{N}$, where $A_{i}$ 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 \leq N, Q \leq 10^{5} $
$1 \leq A_{i}, v \leq 10^{9}$
$1 \leq l \leq r \leq N$
$1 \leq k \leq 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
树套树裸题。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::map;
using std::min;
using std::find;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
#define lc root<<1
#define rc root<<1|1
const int N = 100010;
const int INF = 0x3f3f3f3f;
struct Node {
int v, s, c;
Node *ch[2];
inline void push_up() {
s = ch[0]->s + ch[1]->s + c;
}
inline void setc(int _v, int _s, Node *p) {
v = _v, s = c = _s, ch[0] = ch[1] = p;
}
inline int cmp(int x) const {
if (x == v) return -1;
return x > v;
}
};
int sum, arr[N];
struct SBT {
int top;
Node *null, *tail, *pool[N], stack[N << 5], *ptr[N << 2];
inline void init(int n) {
top = 0;
tail = &stack[0];
null = tail++;
null->setc(0, 0, NULL);
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
seg_built(1, 1, n);
}
inline Node *newNode(int v) {
Node *p = !top ? tail++ : pool[--top];
p->setc(v, 1, null);
return p;
}
inline void rotate(Node *&x, int d) {
Node *k = x->ch[!d]; x->ch[!d] = k->ch[d]; k->ch[d] = x;
k->s = x->s; x->push_up(); x = k;
}
inline void Maintain(Node *&x, int d) {
if (!x->ch[d]->s) return;
if (x->ch[d]->ch[d]->s > x->ch[!d]->s) rotate(x, !d);
else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s) rotate(x->ch[d], d), rotate(x, !d);
else return;
Maintain(x, 0), Maintain(x, 1);
}
inline void insert(Node* &x, int key) {
if (!x->s) { x = newNode(key); return; }
int d = x->cmp(key);
x->s++;
if (-1 == d) { x->c++; return; }
insert(x->ch[d], key);
x->push_up();
Maintain(x, d);
}
inline void erase(Node* &x, int key){
if (!x->s) return;
int d = x->cmp(key);
x->s--;
if (-1 == d) {
if (x->c > 1) {
x->c--;
} else if (!x->ch[0]->s || !x->ch[1]->s) {
pool[top++] = x;
x = x->ch[0]->s ? x->ch[0] : x->ch[1];
} else {
Node *ret = x->ch[1];
for (; ret->ch[0]->s; ret = ret->ch[0]);
erase(x->ch[1], x->v = ret->v);
}
} else {
erase(x->ch[d], key);
}
if (x->s) x->push_up();
}
inline int sbt_rank(Node *x, int key) {
int t, cur = 0;
for (; x->s;) {
t = x->ch[0]->s;
if (key < x->v) x = x->ch[0];
else if (key >= x->v) cur += x->c + t, x = x->ch[1];
}
return cur;
}
inline void seg_built(int root, int l, int r) {
ptr[root] = null;
for (int i = l; i <= r; i++) insert(ptr[root], arr[i]);
if (l == r) return;
int mid = (l + r) >> 1;
seg_built(lc, l, mid);
seg_built(rc, mid + 1, r);
}
inline void seg_query(int root, int l, int r, int x, int y, int val) {
if (x > r || y < l) return;
if (x <= l && y >= r) {
sum += sbt_rank(ptr[root], val);
return;
}
int mid = (l + r) >> 1;
seg_query(lc, l, mid, x, y, val);
seg_query(rc, mid + 1, r, x, y, val);
}
inline void seg_modify(int root, int l, int r, int pos, int val) {
if (pos > r || pos < l) return;
erase(ptr[root], arr[pos]);
insert(ptr[root], val);
if (l == r) return;
int mid = (l + r) >> 1;
seg_modify(lc, l, mid, pos, val);
seg_modify(rc, mid + 1, r, pos, val);
}
inline void kth(int n, int a, int b, int k) {
int l = 0, r = INF;
while (l < r) {
sum = 0;
int mid = (l + r) >> 1;
seg_query(1, 1, n, a, b, mid);
if (sum < k) l = mid + 1;
else r = mid;
}
printf("%d\n", l);
}
inline void solve(int n) {
init(n);
int m, a, b, c, k;
scanf("%d", &m);
while (m--) {
scanf("%d", &a);
if (2 == a) {
scanf("%d %d %d", &b, &c, &k);
kth(n, b, c, k);
} else {
scanf("%d %d", &b, &c);
seg_modify(1, 1, n, b, c);
arr[b] = c;
}
}
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
while (~scanf("%d", &n)) {
go.solve(n);
}
return 0;
}
hdu 5412 CRB and Queries的更多相关文章
- HDU 5412——CRB and Queries——————【线段树套Treap(并没有AC)】
CRB and Queries Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries
CRB and Queries Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5412 CRB and Queries(区间第K大 树套树 按值建树)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5412 Problem Description There are N boys in CodeLan ...
- HDU 5412 CRB and Queries 动态整体二分
Problem Description There are N boys in CodeLand.Boy i has his coding skill Ai.CRB wants to know who ...
- HDU - 5412 CRB and Queries (整体二分)
题目链接 动态区间第k小,但是这道题的话用主席树+树状数组套线段树的空间复杂度是O(nlog2n)会爆掉. 另一种替代的方法是用树状数组套平衡树,空间复杂度降到了O(nlogn),但我感觉平衡树是个挺 ...
- hdu 5412 CRB and Queries(整体二分)
题意 动态区间第k大 (n<=100000,m<=100000) 题解 整体二分的应用. 与静态相比差别不是很大.(和CDQ还有点像)所以直接上代码. #include<iostre ...
- Hdu CRB and Queries(整体二分)
CRB and Queries Time Limit: 6000 MS Memory Limit: 131072 K Problem Description There are N boys in C ...
- [la P5031&hdu P3726] Graph and Queries
[la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: ...
- HDU 3726 Graph and Queries treap树
题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...
随机推荐
- 慕课网-安卓工程师初养成-2-6 Java中的数据类型
来源:http://www.imooc.com/code/1230 通常情况下,为了方便物品的存储,我们会规定每个盒子可以存放的物品种类,就好比在“放臭袜子的盒子”里我们是不会放“面包”的!同理,变量 ...
- 配置Tomcat的JVM的大小解决Tomcat内存溢出的问题
操作步骤如下图所示(图中也有对应的文字说明已比较详细,不再用文字做过多的解释): 1: 2: 3: 4: 5: 下面是配置的参数的说明: -Xms256m JV ...
- 设置Eclipse的字体风格方式
1:Eclipse版本号:3.7.0(汉化版) 假设eclipse已经打开 窗口--->首选项--->常规--->外观--->颜色和字体--->基本--->文字字体 ...
- 洛谷P1461 海明码 Hamming Codes
P1461 海明码 Hamming Codes 98通过 120提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 给出 N,B 和 ...
- Java基础——序列化
Java的“对象序列化”能将一个实现了Serialiable接口(标记接口,没有任何方法)的对象转化为一组byte,这样日后要用到这个对象的时候,就能把这些byte数据恢复出来,并据此重新构建那个对象 ...
- windows内核对象可以等待
内核对象有两种状态 触发 与未触发. 是可以等待的.
- 【Framework】HTTP运行期与页面执行模型
HTTP运行期 HTTP运行期处理客户端应用程序(例如Web浏览器)进入的一个Web请求,通过处理它的应用程序的适当组件路由请求,然后产生响应并发回提出请求的客户端应用程序. 进入的HTTP Web请 ...
- CentOS下通过rdesktop连接Windows远程桌面
众所周知,微软的Windows提供了一种远程桌面系统(Remote Desktop),该服务的默认端口是3389,可使用户远程登录进行系统管理或作为终端服务器运行各种应用软件. 而要连接Windows ...
- js怎样改变div的宽度
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- (图 BFS)走迷宫
题目: 给一个迷宫,求出从起点到终点的路径.迷宫 src.txt 文件内容如下,第一行是迷宫的行列数,后面行是迷宫,1表示可行走,0表示不可以通过,起点是最左上角,终点是最右下角: 解析: 其实就是图 ...