ZOJ 2112 Dynamic Rankings(树状数组+主席树)
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.
<b< dd="">
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.
<b< dd="">
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
题解:
树状数组的每个节点都是一颗线段树,但这棵线段树不再保存每个前缀的信息了,而是由树状数组的sum函数计算出这个前缀的信息,那么显而易见这棵线段树保存的是辅助数组S的值,即S=A[i-lowbit+1]+...+A[i],其中A[i]表示值为i的元素出现的次数。
那么对于每次修改,我们要修改树状数组上的logn棵树,对于每棵树,我们要修改logn个结点,所以时空复杂度为
O((n+q)*logn*logn),由于这道题n比较大,查询次数q比较小,所以我们可以初始时建立一颗静态的主席树,树状数组只保存每次修改的信息,那么时空复杂度降为了O(n*logn+q*logn*logn)
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) (x&-x)
typedef long long ll;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
const int maxn=6e4+;
struct Tree{
int ls,rs;
int sum;
} node[maxn*]; struct Qnode{
bool flag;
int l,r,k;
} qn[maxn]; int T,n,q,a[maxn],b[maxn],num,rt[maxn];
char str[];
int ul[maxn],ur[maxn],ca[maxn],s[maxn],cnt; void Build(int rt,int l,int r)
{
rt=++cnt;
node[rt].sum=;
if(l==r) return ;
int mid=l+r>>;
Build(node[rt].ls,l,mid);
Build(node[rt].rs,mid+,r);
} void Update(int y,int &x,int l,int r,int pos,int val)
{
node[++cnt]=node[y];node[cnt].sum+=val;x=cnt;
if(l==r) return ;
int mid=l+r>>;
if(pos<=mid) Update(node[y].ls,node[x].ls,l,mid,pos,val);
else Update(node[y].rs,node[x].rs,mid+,r,pos,val);
} void Add(int x,int val)
{
int res=lower_bound(b+,b++num,a[x])-b;
while(x<=n)
{
Update(s[x],s[x],,num,res,val);
x+=lowbit(x);
}
} int Sum(int x,bool temp)
{
int res=;
while(x>)
{
if(temp) res+=node[node[ur[x]].ls].sum;
else res+=node[node[ul[x]].ls].sum;
x-=lowbit(x);
}
return res;
} int Query(int L,int R,int x,int y,int l,int r,int k)
{
if(l==r) return l;
int mid=l+r>>;
int res=Sum(R,true)-Sum(L,false)+node[node[y].ls].sum-node[node[x].ls].sum;
if(k<=res)
{
for(int i=R;i;i-=lowbit(i)) ur[i]=node[ur[i]].ls;
for(int i=L;i;i-=lowbit(i)) ul[i]=node[ul[i]].ls;
return Query(L,R,node[x].ls,node[y].ls,l,mid,k);
}
else
{
for(int i=R;i;i-=lowbit(i)) ur[i]=node[ur[i]].rs;
for(int i=L;i;i-=lowbit(i)) ul[i]=node[ul[i]].rs;
return Query(L,R,node[x].rs,node[y].rs,mid+,r,k-res);
}
} int main()
{
T=read();
while(T--)
{
n=read();q=read(); cnt=num=;
memset(rt,,sizeof(rt));
for(int i=;i<=n;++i) a[i]=read(),b[++num]=a[i];
for(int i=;i<=q;++i)
{
scanf("%s",str);
if(str[]=='Q')
{
qn[i].flag=true;
qn[i].l=read();qn[i].r=read();qn[i].k=read();
}
else
{
qn[i].flag=false;
qn[i].l=read();qn[i].r=read();b[++num]=qn[i].r;
}
}
sort(b+,b++num);
int tot=unique(b+,b++num)-b-;
num=tot;
for(int i=;i<=n;++i) ca[i]=lower_bound(b+,b++num,a[i])-b;
Build(rt[],,num);
for(int i=;i<=n;++i) Update(rt[i-],rt[i],,num,ca[i],);
for(int i=;i<=n;++i) s[i]=rt[];
for(int i=;i<=q;++i)
{
if(qn[i].flag)
{
for(int j=qn[i].r;j;j-=lowbit(j)) ur[j]=s[j];
for(int j=qn[i].l-;j;j-=lowbit(j)) ul[j]=s[j];
printf("%d\n",b[Query(qn[i].l-,qn[i].r,rt[qn[i].l-],rt[qn[i].r],,num,qn[i].k)]);
}
else
{
Add(qn[i].l,-);
a[qn[i].l]=qn[i].r;
Add(qn[i].l,);
}
}
} return ;
}
ZOJ 2112 Dynamic Rankings(树状数组+主席树)的更多相关文章
- ZOJ - 2112 Dynamic Rankings(BIT套主席树)
纠结了好久的一道题,以前是用线段树套平衡树二分做的,感觉时间复杂度和分块差不多了... 终于用BIT套函数式线段树了过了,120ms就是快,此题主要是卡内存. 假设离散后有ns个不同的值,递归层数是l ...
- BZOJ_1901_Zju2112 Dynamic Rankings_树状数组+主席树
BZOJ_1901_Zju2112 Dynamic Rankings_树状数组+主席树 题意: 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i, ...
- zoj2112 树状数组+主席树 区间动第k大
Dynamic Rankings Time Limit: 10000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Subm ...
- 【bzoj1146】[CTSC2008]网络管理Network 倍增LCA+dfs序+树状数组+主席树
题目描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构由N个路由器和N-1条高 ...
- 【bzoj3744】Gty的妹子序列 分块+树状数组+主席树
题目描述 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzoj3720) 上掉落下来了许多妹子,他发现 她们排成 ...
- BZOJ_2120_数颜色_Set+树状数组+主席树
BZOJ_2120_数颜色_Set+树状数组+主席树 Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L ...
- P1972 [SDOI2009]HH的项链[离线+树状数组/主席树/分块/模拟]
题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...
- ZOJ 2112 Dynamic Rankings(树状数组+主席树)
题意 \(n\) 个数,\(m\) 个操作,每次操作修改某个数,或者询问某个区间的第 \(K\) 小值. \(1 \leq n \leq 50000\) \(1 \leq m \leq 10000\) ...
- [luogu2617][bzoj1901][Zju2112]Dynamic Rankings【树套树+树状数组+主席树】
题目网址 [传送门] 题目大意 请你设计一个数据结构,支持单点修改,区间查询排名k. 感想(以下省略脏话inf个字) 真的强力吹爆洛谷数据,一般的树套树还给我T了一般的点,加强的待修主席树还给我卡了几 ...
随机推荐
- HTML——基础知识点1
- Junit使用方法
Junit使用方法 步骤: 定义 一个测试类(测试用例) 建议 测试类名:被测试类名+Test 包名:xxx.xxx.xxx.test 测试方法 建议: 方法名:test测试的方法名 返回值: voi ...
- sql性能分析(explain关键字)
explain关键字结果 列名所代表的的含义: Id:MySQL QueryOptimizer 选定的执行计划中查询的序列号.表示查询中执行 select 子句或操作表的顺序,id 值越大优先级越高, ...
- redhat、centos7系列破解密码
redhat或者centos7,破解密码: 1.开机出现引导菜单时按下e键 2.找到linux16行,在其后追加 rd.break 参数 console=tty0 3.启动到特定的模式,由于更改密码需 ...
- [LC]88题 Merge Sorted Array (合并两个有序数组 )
①英文题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. N ...
- 在 ASP.NET Core 项目中使用 MediatR 实现中介者模式
一.前言 最近有在看 DDD 的相关资料以及微软的 eShopOnContainers 这个项目中基于 DDD 的架构设计,在 Ordering 这个示例服务中,可以看到各层之间的代码调用与我们之前 ...
- hdu 1863 畅通工程 (prim)
畅通工程Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- nyoj 277-车牌号 (map, pair, iterator)
277-车牌号 内存限制:64MB 时间限制:3000ms 特判: No 通过数:9 提交数:13 难度:1 题目描述: 茵茵很喜欢研究车牌号码,从车牌号码上可以看出号码注册的早晚,据研究发现,车牌号 ...
- 编写 Dockerfile 最佳实践
官方仓库虽然有数十万计的免费镜像,但大多数无法直接满足公司业务需求,这就需要我们自己去定制镜像了. Docker通过Dockerfile自动构建镜像,Dockerfile是一个包含用于组建镜像的文本文 ...
- 【前端知识体系-CSS相关】Bootstrap相关知识
1.Bootstrap 的优缺点? 优点:CSS代码结构合理,现成的代码可以直接使用(响应式布局) 缺点:定制流程较为繁琐,体积大 2.如何实现响应式布局? 原理:通过media query设置不同分 ...