题目链接:

  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112

题目:

思路:

  树套树板子题。

代码实现如下:

 #include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-;
const int mod = ;
const int maxn = 5e4 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; char op[];
int n, q, x, y, tot1, tot2, cnt;
int a[maxn], q1[maxn], q2[maxn], root1[maxn], root2[maxn];
vector<int> v; struct que {
int op, l, r, x;
}ask[maxn]; struct node {
int l, r, sum;
}tree[maxn*]; int getid(int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin() + ;
} void update(int l, int r, int& x, int y, int pos, int val) {
tree[++cnt] = tree[y], tree[cnt].sum += val, x = cnt;
if(l == r) return;
int mid = (l + r) >> ;
if(pos <= mid) update(l, mid, tree[x].l, tree[y].l, pos, val);
else update(mid + , r, tree[x].r, tree[y].r, pos, val);
} int query(int l, int r, int x, int y, int k) {
if(l == r) return l;
int cnt = tree[tree[y].l].sum - tree[tree[x].l].sum;
for(int i = ; i < tot1; ++i) {
cnt -= tree[tree[q1[i]].l].sum;
}
for(int i = ; i < tot2; ++i) {
cnt += tree[tree[q2[i]].l].sum;
}
int mid = (l + r) >> ;
if(cnt >= k) {
for(int i = ; i < tot1; ++i) {
q1[i] = tree[q1[i]].l;
}
for(int i = ; i < tot2; ++i) {
q2[i] = tree[q2[i]].l;
}
return query(l, mid, tree[x].l, tree[y].l, k);
} else {
for(int i = ; i < tot1; ++i) {
q1[i] = tree[q1[i]].r;
}
for(int i = ; i < tot2; ++i) {
q2[i] = tree[q2[i]].r;
}
return query(mid + , r, tree[x].r, tree[y].r, k - cnt);
}
} int main(){
#ifndef ONLINE_JUDGE
FIN;
#endif
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &q);
cnt = tot1 = tot2 = ;
v.clear();
memset(root2, , sizeof(root2));
for(int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
v.push_back(a[i]);
}
for(int i = ; i <= q; ++i) {
scanf("%s", op);
if(op[] == 'C') ask[i].op = ;
else ask[i].op = ;
if(ask[i].op == ) {
scanf("%d%d", &ask[i].l, &ask[i].x);
v.push_back(ask[i].x);
} else {
scanf("%d%d%d", &ask[i].l, &ask[i].r, &ask[i].x);
}
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int sz = v.size();
for(int i = ; i <= n; ++i) {
a[i] = getid(a[i]);
update(, sz, root1[i], root1[i-], a[i], );
}
for(int i = ; i <= q; ++i) {
int op = ask[i].op;
if(op == ) {
int pos = ask[i].l, x = ask[i].x, y = a[pos];
x = getid(x);
a[pos] = x;
while(pos <= n) {
update(, sz, root2[pos], root2[pos], y, -);
update(, sz, root2[pos], root2[pos], x, );
pos += lowbit(pos);
}
} else {
int l = ask[i].l, r = ask[i].r, k = ask[i].x;
int x = l - ;
tot1 = tot2 = ;
while(x) {
q1[tot1++] = root2[x];
x -= lowbit(x);
}
x = r;
while(x) {
q2[tot2++] = root2[x];
x -= lowbit(x);
}
printf("%d\n", v[query(, sz, root1[l-], root1[r], k)-]);
}
}
}
return ;
}

Dynamic Rankings(动态第k大+树套树)的更多相关文章

  1. zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap

    Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...

  2. zoj2112 主席树动态第k大 (主席树&&树状数组)

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

  3. ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)

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

  4. P2617 Dynamic Rankings (动态开点权值线段树 + 树状数组)

    题意:带修求区间k小 题解:回忆在使用主席树求区间k小时 利用前缀和的思想 既然是前缀和 那么我们可以使用更擅长维护前缀和的树状数组 但是这里每一颗权值线段树就不是带版本的 而是维护数组里i号点的权值 ...

  5. ZOJ 2112 Dynamic Rankings (动态第k大,树状数组套主席树)

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

  6. 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题

    达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...

  7. 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树套树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1901 这题调了我相当长的时间,1wa1a,我是第一次写树套树,这个是树状数组套splay,在每个区间 ...

  8. ZOJ2112 BZOJ1901 Dynamic Rankings 树套树 带修改的区间第k小

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 树套树,线段树套splay或者主席树套树状数组,我抄了一下hzwer ...

  9. bzoj 1901: Zju2112 Dynamic Rankings(树套树)

    1901: Zju2112 Dynamic Rankings 经典的带改动求区间第k小值问题 树套树模板,我是用的线段树套splay实现的,并且用的数组模拟的,所以可能空间略大,bzoj过了,zoj过 ...

随机推荐

  1. 服务 在初始化安装时发生异常:System.IO.FileNotFoundException: 未能加载文件或******

    这个问题是在用installutil.exe安装服务时候碰到的 解决方法就是把installutil.exe文件直接复制到要安装的目录下 installutil.exe的所在位置 windows/mi ...

  2. php 的stdClass类的简单实用

    <?php $person = new stdClass(); $person->name = "yu"; $person->sex = "man&qu ...

  3. ubuntu 中安装memcache,并给出一个简单的实例·

    Memcache分为两部分,Memcache服务端和客户端.Memcache服务端是作为服务来运行的,所有数据缓存的建立,存储,删除实际上都是在这里完成的.客户端,在这里我们指的是PHP的可以调用的扩 ...

  4. 常见meta标签记录

    关于meta <meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标签位于文档的头部,不包含 ...

  5. Java并发编程中的设计模式解析(二)一个单例的七种写法

    Java单例模式是最常见的设计模式之一,广泛应用于各种框架.中间件和应用开发中.单例模式实现起来比较简单,基本是每个Java工程师都能信手拈来的,本文将结合多线程.类的加载等知识,系统地介绍一下单例模 ...

  6. Html5 web 本地存储 (localStorage、sessionStorage)

    HTML5 提供了两种在客户端存储数据的新方法localStorage,sessionStorage sessionStorage(临时存储) :为每一个数据源维持一个存储区域,在浏览器打开期间存在, ...

  7. [BZOJ2502]清理雪道 有上下界网络流(最小流)

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MB Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...

  8. Qt环境配置 + Qt使用教程

    官方下载链接有以下: http://download.qt.io/official_releases/qt/5.8/5.8.0/ http://download.qt.io/official_rele ...

  9. 洛谷P4233 射命丸文的笔记 【多项式求逆】

    题目链接 洛谷P4233 题解 我们只需求出总的哈密顿回路个数和总的强联通竞赛图个数 对于每条哈密顿回路,我们统计其贡献 一条哈密顿回路就是一个圆排列,有\(\frac{n!}{n}\)种,剩余边随便 ...

  10. 洛谷 P3990 [SHOI2013]超级跳马 解题报告

    P3990 [SHOI2013]超级跳马 题目描述 现有一个\(n\) 行 \(m\) 列的棋盘,一只马欲从棋盘的左上角跳到右下角.每一步它向右跳奇数列,且跳到本行或相邻行.跳越期间,马不能离开棋盘. ...