Time Limit: 10000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

Submit Status

Description

The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They have developed a more powerful system such that for N numbers a[1], a[2], ..., a[N], you can ask it like: what is the k-th smallest number of a[i], a[i+1], ..., a[j]? (For some i<=j, 0<k<=j+1-i that you have given to it). More powerful, you can even change the value of some a[i], and continue to query, all the same.

Your task is to write a program for this computer, which

- Reads N numbers from the input (1 <= N <= 50,000)

- Processes M instructions of the input (1 <= M <= 10,000). These instructions include querying the k-th smallest number of a[i], a[i+1], ..., a[j] and change some a[i] to t.

Input

The first line of the input is a single number X (0 < X <= 4), the number of the test cases of the input. Then X blocks each represent a single test case.

The first line of each block contains two integers N and M, representing N numbers and M instruction. It is followed by N lines. The (i+1)-th line represents the number a[i]. Then M lines that is in the following format

Q i j k or
C i t

It represents to query the k-th number of a[i], a[i+1], ..., a[j] and change some a[i] to t, respectively. It is guaranteed that at any time of the operation. Any number a[i] is a non-negative integer that is less than 1,000,000,000.

There're NO breakline between two continuous test cases.

Output

For each querying operation, output one integer to represent the result. (i.e. the k-th smallest number of a[i], a[i+1],..., a[j])

There're NO breakline between two continuous test cases.

Sample Input

2
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3

Sample Output

3
6
3
6

/*
study
zoj2112 树状数组+主席树 区间动第k大
开始想了一下,如果只用主席树在修改的时候差不多要修改所有的树
然后看了下树状数组套主席树,
相当于我们先照静态第k大建好树,然后把修改操作通过树状数组另外建一个树
于是在查询的时候只要通过树状数组得出增减情况再加上最初的
hhh-2016-02-19 15:03:33
*/ #include <functional>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std; const int maxn = 60010;
int n,m;
int a[maxn],t[maxn];
int T[maxn],val[maxn*40],lson[maxn*40],rson[maxn*40];
int tot; void ini_hash(int num) //排序去重
{
sort(t,t+num);
m = unique(t,t+num)-t;
} int Hash(int x) //获得x在排序去重后的位置
{
return lower_bound(t,t+m,x) - t;
} int build(int l,int r)
{
int root = tot++;
val[root] = 0;
if(l != r)
{
int mid = (l+r)>>1;
lson[root] = build(l,mid);
rson[root] = build(mid+1,r);
}
return root;
} //如果那里发生改变则兴建一个节点而非像平常修改那个节点的值
int update(int root,int pos,int va)
{
int newroot = tot++;
int tmp = newroot;
val[newroot] = val[root] + va;
int l = 0,r = m-1;
while(l < r)
{
int mid = (l+r)>>1;
if(pos <= mid)
{
lson[newroot] = tot++;
rson[newroot] = rson[root];
newroot = lson[newroot];
root = lson[root];
r = mid;
}
else
{
lson[newroot] = lson[root];
rson[newroot] = tot++;
newroot = rson[newroot];
root = rson[root];
l = mid+1;
}
val[newroot] = val[root] + va;
}
return tmp;
} int s[maxn];
int cur[maxn]; int lowbit(int x)
{
return x&(-x);
} //void add(int x,int pos,int va)
//{
// while(x < n)
// {
// s[x] = update(s[x],pos,va);
// x += lowbit(x);
// }
//} int sum(int x)
{
int cn = 0;
while(x>0)
{
cn += val[lson[cur[x]]];
x -= lowbit(x);
}
return cn;
} int query(int lt,int rt,int k)
{ int l = 0, r = m-1;
int lroot = T[lt-1];
int rroot = T[rt];
for(int i = lt-1; i; i-=lowbit(i)) cur[i] = s[i];
for(int i = rt; i; i-=lowbit(i)) cur[i] = s[i];
while(l < r)
{
int mid = (l+r)>>1;
int tmp = sum(rt)-sum(lt-1)+val[lson[rroot]]-val[lson[lroot]];
if(tmp >= k)
{
for(int i = lt-1; i; i-=lowbit(i)) cur[i] = lson[cur[i]];
for(int i = rt; i; i-=lowbit(i)) cur[i] = lson[cur[i]];
lroot = lson[lroot];
rroot = lson[rroot];
r = mid;
}
else
{
k -= tmp;
for(int i = lt-1; i; i-=lowbit(i)) cur[i] = rson[cur[i]];
for(int i = rt; i; i-=lowbit(i)) cur[i] = rson[cur[i]];
lroot = rson[lroot];
rroot = rson[rroot];
l = mid+1;
}
}
return l;
} void change(int root,int pos,int va)
{
while(root <= n)
{
s[root] = update(s[root],pos,va);
root += lowbit(root);
}
} struct node
{
int l,r,kind,k;
} qry[10010]; int main()
{
int q,cas;
scanf("%d",&cas);
while(cas--)
{
m = 0;
tot = 0;
scanf("%d%d",&n,&q) ;
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
t[m++] = a[i];
} char ask[10];
int l,r,k;
for(int i = 1; i <= q; i++)
{
scanf("%s",ask); if(ask[0] == 'Q')
{
scanf("%d%d%d",&l,&r,&k);
qry[i].l = l;
qry[i].r = r;
qry[i].k = k;
qry[i].kind = 0;
}
else
{
scanf("%d%d",&l,&r);//将l位置的数改为r
qry[i].l = l;
qry[i].r = r;
qry[i].kind = 1;
t[m++] = qry[i].r;
}
} ini_hash(m);
T[0] = build(0,m-1);
for(int i = 1; i <= n; i++)
T[i] = update(T[i-1],Hash(a[i]),1); for(int i =1; i <= n; i++)
{
s[i] = T[0];
}
for(int i =1; i <= q; i++)
{
if(qry[i].kind == 0)
{
printf("%d\n",t[query(qry[i].l,qry[i].r,qry[i].k)]);
}
else
{
change(qry[i].l,Hash(a[qry[i].l]),-1);
change(qry[i].l,Hash(qry[i].r),1);
a[qry[i].l] = qry[i].r;
}
}
}
return 0;
}

  

zoj2112 树状数组+主席树 区间动第k大的更多相关文章

  1. BZOJ_1901_Zju2112 Dynamic Rankings_树状数组+主席树

    BZOJ_1901_Zju2112 Dynamic Rankings_树状数组+主席树 题意: 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i, ...

  2. 【bzoj3744】Gty的妹子序列 分块+树状数组+主席树

    题目描述 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzoj3720) 上掉落下来了许多妹子,他发现 她们排成 ...

  3. BZOJ_2120_数颜色_Set+树状数组+主席树

    BZOJ_2120_数颜色_Set+树状数组+主席树 Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L ...

  4. P1972 [SDOI2009]HH的项链[离线+树状数组/主席树/分块/模拟]

    题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...

  5. 【bzoj1146】[CTSC2008]网络管理Network 倍增LCA+dfs序+树状数组+主席树

    题目描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构由N个路由器和N-1条高 ...

  6. 【BZOJ】1146: [CTSC2008]网络管理Network(树链剖分+线段树套平衡树+二分 / dfs序+树状数组+主席树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1146 第一种做法(时间太感人): 第二种做法(rank5,好开心) ================ ...

  7. BZOJ 2743: [HEOI2012]采花 [树状数组 | 主席树]

    题意: 查询区间中出现次数$>2$的颜色个数 一眼主席树,区间中$l \le last[i] \le r$的个数减去$l \le last[last[i]] \le r$的个数,搞两颗主席树来做 ...

  8. ZOJ 2112 Dynamic Rankings(树状数组+主席树)

    题意 \(n\) 个数,\(m\) 个操作,每次操作修改某个数,或者询问某个区间的第 \(K\) 小值. \(1 \leq n \leq 50000\) \(1 \leq m \leq 10000\) ...

  9. HDU 3333 Turing Tree(树状数组/主席树)

    题意 给定一个长度为 \(n​\) 的序列,\(m​\) 个查询,每次查询区间 \([L,R]​\) 范围内不同元素的和. \(1\leq T \leq 10\) \(1 \leq n\leq 300 ...

随机推荐

  1. 山西某公司NetApp存储不小心删除文件数据恢复成功案例

    故障情况简介: 需要进行数据恢复的设备是一台NetApp存储,共有24块磁盘组成.由于管理员删除文件夹,且时间比较久,删除有几个月时间. 可恢复性判断:由于NetApp中的文件系统的特性,WAFL是& ...

  2. php函数var_dump() 、print_r()、echo()

    var_dump() 能打印出类型 print_r() 只能打出值 echo() 是正常输出... 需要精确调试的时候用 var_dump(); 一般查看的时候用 print_r() 另外 , ech ...

  3. python小练习之三---购物车程序

    购物车购物的例子 严格来讲,这个例子相对大一些 功能也稍完备一些,具有用户登录,商品上架,用户购物,放入购物车,展示每个用户的购物车里的商品的数量,用户账户余额,支持用户账户充值等 下面展示的代码有些 ...

  4. Python爬虫之urllib模块1

    Python爬虫之urllib模块1 本文来自网友投稿.作者PG,一个待毕业待就业二流大学生.玄魂工作室未对该文章内容做任何改变. 因为本人一直对推理悬疑比较感兴趣,所以这次爬取的网站也是平时看一些悬 ...

  5. 离线Chrome插件安装文件(crx)的安装方法

    离线Chrome插件安装文件(crx)的安装方法 一.正常安装方法 1.开发谷歌浏览器,设置->扩展程序 在打开的谷歌浏览器的扩展管理器中用户可以看到一些已经安装程序的Chrome插件,或者一个 ...

  6. mysql(3)—— 内连接、外连接的区别

    先来看一下,内连接的语法: SELECT  XXX FROM XXX INNER JOIN XXX ON XXX; 这里 INNER 可以省略,在上一篇博客中我们对于笛卡尔积现象的研究中(http:/ ...

  7. 使用java 打印日历

    package hangshu; /* * 打印从1900年到2.year年的日历 */ import java.util.Scanner; public class Calender { publi ...

  8. mysql 练习题

    导出现有数据库数据: C:\Users\Administrator>mysqldump -u root db1>D:\agon\db1.sql -p  #结构+数据 mysqldump - ...

  9. SpringBoot(一):使用eclipse/idea创建springboot helloword工程

    eclipse如何创建spring boot工程: 第一步:首先打开eclipse,找到图中的下图的中“下三角”符号,选中"working sets"(表示将会把eclipse中的 ...

  10. html标记语言 --表单

    html标记语言 --表单 七.表单 1.表单标记 1.1表单中的内容 <form></form>定义表单的开始位置和结束位置,表单提交时的内容就是<form>表单 ...