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了一般的点,加强的待修主席树还给我卡了几 ...
随机推荐
- ES6入门十二:Module(模块化)
webpack4打包配置babel7转码ES6 Module语法与API的使用 import() Module加载实现原理 Commonjs规范的模块与ES6模块的差异 ES6模块与Nodejs模块相 ...
- Vue导入非模块化的第三方插件功能无效解决方案
一.问题: 最近在写vue项目时,想引入某些非模块化的第三方插件时,总是发现会有报错.且在与本地运行插件测试对比时发现插件根本没有注入到jQuery中(console.log($.fn)查看当前jq有 ...
- Java描述设计模式(23):访问者模式
本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.场景描述 电竞是游戏比赛达到"竞技"层面的体育项目.利用电子设备作为运动器械进行的.人与人之间的智力对抗 ...
- CentOS 7下配置ISO镜像文件为本地yum源
环境限制外网怎么办?离线环境怎么解决依赖?yum源配起来,可以解决大部分包的安装^_^ 环境: 虚拟机:VMware Workstation Pro 12.x Linux系统版本:CentOS-7-x ...
- webapi跨域使用session
在之前的项目中,我们设置跨域都是直接在web.config中设置的. 这样是可以实现跨域访问的.因为我们这边一般情况下一个webapi会有多个网站.小程序.微信公众号等访问,所以这样设置是没有问题的. ...
- 微信小程序使用 ECharts 实现数据可视化
微信小程序使用 ECharts 显示图表 首先创建微信小程序 这里就不再赘述 下载 GitHub 上的 ecomfe/echarts-for-weixin 下载后解压,打开文件夹,里面的 ec-can ...
- 【2018寒假集训 Day1】【位运算】桐桐的运输方案
桐桐的运输方案(transp) [问题描述] 桐桐有 N 件货物需要运送到目的地,它们的重量和价值分别记为: 重量:W1,W2,…,Wn: 价值:V1,V2,…,Vn: 已知某辆货车的最大载货量为 X ...
- 设计模式——代理模式(Proxy)
定义 为其他对象提供一种代理,以控制对这个对象的访问.代理对象在客户端和目标对象之间起到中介的作用.(结构型) 如果不知道代理模式,可能大家对代理服务器都不叫熟悉.代替服务器代替请求者去发一起对另一个 ...
- python分支循环
1.遍历循环 for i in range(5) for i in range (M,N,K) for c in s: for c in 'python' print(c,end="&quo ...
- Fortran流程控制与逻辑运算、循环--xdd
1.IF语句 1 if() then ... end if 2 if() then ... else ... end if 3 if() then ... else if() then ... els ...