Problem Description
There are N boys in CodeLand.
Boy i has his coding skill Ai.
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 A1, A2, …, AN, where Ai 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 ≤ N, Q ≤ 105
1 ≤ Ai, v ≤ 109
1 ≤ l ≤ r ≤ N
1 ≤ k ≤ 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 <cmath>
using namespace std;
const int N=;
int gi(){
int str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>='' && ch<='')str=(str<<)+(str<<)+ch-,ch=getchar();
return str;
}
int n,m,a[N],top=,ans[N];
struct node{
int x,y,cnt,k,id;
}t[N*],q1[N*],q2[N*];
int Tree[N*];
void add(int sta,int ad){
for(int i=sta;i<=n;i+=(i&(-i)))Tree[i]+=ad;
}
int getsum(int sta){
int sum=;
for(int i=sta;i>=;i-=(i&(-i)))sum+=Tree[i];
return sum;
}
int l1=,l2=;
void count(int ll,int rr,int dl,int dr)
{
for(int i=ll;i<=rr;i++)
{
if(t[i].k)
t[i].cnt=getsum(t[i].y)-getsum(t[i].x-);
else if(t[i].y<=dr)
add(t[i].x,t[i].cnt);
}
for(int i=ll;i<=rr;i++)
if(!t[i].k && t[i].y<=dr)
add(t[i].x,-t[i].cnt);
l1=;l2=;
for(int i=ll;i<=rr;i++)
{
if(t[i].k)
{
if(t[i].cnt>=t[i].k)
q1[++l1]=t[i];
else
t[i].k-=t[i].cnt,q2[++l2]=t[i];
}
else
{
if(t[i].y<=dr)
q1[++l1]=t[i];
else
q2[++l2]=t[i];
}
}
int now=ll-;
for(int i=;i<=l1;i++)
t[++now]=q1[i];
for(int i=;i<=l2;i++)
t[++now]=q2[i];
}
void div(int ll,int rr,int dl,int dr)
{
if(dl==dr)
{
for(int i=ll;i<=rr;i++)
if(t[i].k)ans[t[i].id]=dl;
return ;
}
int mid=(dl+dr)>>;
count(ll,rr,dl,mid);
int to=l1;
if(to)
div(ll,ll+to-,dl,mid);
if(to<=rr-ll)
div(ll+to,rr,mid+,dr);
}
void Clear(){
top=;
memset(ans,,sizeof(ans));
memset(t,,sizeof(t));
}
void work()
{
Clear();
for(int i=;i<=n;i++)
{
a[i]=gi();
t[++top]=((node){i,a[i],,,});
}
m=gi();
int flag,x,y,z;
for(int i=;i<=m;i++)
{
flag=gi();
if(flag==)
{
x=gi();y=gi();
t[++top]=((node){x,a[x],-,,});
t[++top]=((node){x,y,,,});
a[x]=y;
}
else
{
x=gi();y=gi();z=gi();
t[++top]=(node){x,y,,z,i};
}
}
div(,top,,1e9);
for(int i=;i<=m;i++)if(ans[i])printf("%d\n",ans[i]);
}
int main()
{
while(~scanf("%d",&n))
work();
return ;
}

还有主席树维护的树状数组,但是MLE,这种题目如果数据小可以用

 #include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N=;
int n,m,a[N],b[N<<];
struct Ques{
int flag,x,y,k;
}q[N];
int num=,bel[N];
int getrank(int x){
int l=,r=num,mid;
while(l<=r)
{
mid=(l+r)>>;
if(b[mid]==x)return mid;
if(x>b[mid])l=mid+;
else r=mid-;
}
}
struct Tree{
int ls,rs,cnt;
}t[N*];
int root[N],tot=;
void add(int &rt,int l,int r,int val,int ad)
{
t[++tot]=t[rt];rt=tot;t[rt].cnt+=ad;
if(l==r)return ;
int mid=(l+r)>>;
if(val<=mid)add(t[rt].ls,l,mid,val,ad);
else add(t[rt].rs,mid+,r,val,ad);
}
void updata(int sta,int val,int ad)
{
for(int i=sta;i<=n;i+=(i&(-i)))add(root[i],,num,val,ad);
}
int nl[N],nr[N];
int query(int ll,int rr,int rank)
{
int l=,r=num,mid;
int lenl=,lenr=,sl,sr;
for(int i=ll;i>=;i-=(i&(-i)))nl[++lenl]=root[i];
for(int i=rr;i>=;i-=(i&(-i)))nr[++lenr]=root[i];
while(l<r)
{
mid=(l+r)>>;
sl=sr=;
for(int i=;i<=lenl;i++)sl+=t[t[nl[i]].ls].cnt;
for(int i=;i<=lenr;i++)sr+=t[t[nr[i]].ls].cnt;
if(sr-sl>=rank)
{
for(int i=;i<=lenl;i++)nl[i]=t[nl[i]].ls;
for(int i=;i<=lenr;i++)nr[i]=t[nr[i]].ls;
r=mid;
}
else
{
for(int i=;i<=lenl;i++)nl[i]=t[nl[i]].rs;
for(int i=;i<=lenr;i++)nr[i]=t[nr[i]].rs;
l=mid+;rank-=sr-sl;
}
}
return b[r];
}
void Clear()
{
tot=;num=;
memset(root,,sizeof(root));
memset(t,,sizeof(t));
}
void work()
{
Clear();
for(int i=;i<=n;i++)scanf("%d",&a[i]),b[++num]=a[i];
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d",&q[i].flag);
if(q[i].flag==){
scanf("%d%d%d",&q[i].x,&q[i].y,&q[i].k);
}
else{
scanf("%d%d",&q[i].x,&q[i].y);
b[++num]=q[i].y;
}
}
sort(b+,b+num+);
for(int i=;i<=n;i++)bel[i]=getrank(a[i]);
for(int i=;i<=n;i++)updata(i,bel[i],);
for(int i=;i<=m;i++)
{
if(q[i].flag==)
{
updata(q[i].x,bel[q[i].x],-);
updata(q[i].x,q[i].y=getrank(q[i].y),);
bel[q[i].x]=q[i].y;
}
else
printf("%d\n",query(q[i].x-,q[i].y,q[i].k));
}
}
int main()
{
//freopen("pp.in","r",stdin);
//freopen("pp.out","w",stdout);
while(~scanf("%d",&n))
work();
return ;
}

HDU 5412 CRB and Queries 动态整体二分的更多相关文章

  1. hdu 5412 CRB and Queries

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5412 CRB and Queries Description There are $N$ boys i ...

  2. HDU - 5412 CRB and Queries (整体二分)

    题目链接 动态区间第k小,但是这道题的话用主席树+树状数组套线段树的空间复杂度是O(nlog2n)会爆掉. 另一种替代的方法是用树状数组套平衡树,空间复杂度降到了O(nlogn),但我感觉平衡树是个挺 ...

  3. hdu 5412 CRB and Queries(整体二分)

    题意 动态区间第k大 (n<=100000,m<=100000) 题解 整体二分的应用. 与静态相比差别不是很大.(和CDQ还有点像)所以直接上代码. #include<iostre ...

  4. HDU 5412——CRB and Queries——————【线段树套Treap(并没有AC)】

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  5. 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 ...

  6. HDU 5412 CRB and Queries(区间第K大 树套树 按值建树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5412 Problem Description There are N boys in CodeLan ...

  7. Hdu CRB and Queries(整体二分)

    CRB and Queries Time Limit: 6000 MS Memory Limit: 131072 K Problem Description There are N boys in C ...

  8. HDU5412 CRB and Queries 整体二分

    传送门 刚觉得最近写代码比较顺畅没什么Bug,cdq分治真是我的一个噩梦.. 整体二分模板题,带修改的区间第k小. vjudge不知抽什么风,用不了,hdu忘了密码了一直在那里各种试,难受.. 写得比 ...

  9. ZOJ 1112 Dynamic Rankings【动态区间第K大,整体二分】

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112 题意: 求动态区间第K大. 分析: 把修改操作看成删除与增加 ...

随机推荐

  1. python API的安全认证

    我们根据pid加客户端的时间戳进行加密md5(pid|时间戳)得到的单向加密串,与时间戳,或者其它字段的串的url给服务端. 服务端接收到请求的url进行分析 客户端时间与服务端的时间戳之差如果大于规 ...

  2. 视频聊天插件:AnyChat使用攻略之iOS开发指南

    AnyChat使用攻略之iOS开发指南 这套攻略主要指导刚开始使用AnyChat SDK For iOS的同学,快速搭建SDK环境,和实现音视频开发流程. (需要工程案例文件可联系我们) 在iOS平台 ...

  3. 视频聊天 Demo

    ESFramework Demo -- 入门Demo,简单的即时通讯系统(附源码) 是基于ESFramework实现的一个简单的文字聊天demo,现在,我们将在这个demo的基础上,使用OMCS为其增 ...

  4. 《高级软件测试》11.16.Jira使用说明的撰写和操作视频的录制

    今日任务完成情况如下: 小王:完成了测试管理工具jira的使用手册中,基本情况介绍.下载安装部分的撰写工作:小高:参考官方手册,结合自己的实际使用体会,对jira的基本组成及其工作流程进行了介绍:小陈 ...

  5. php中函数和方法的区别

    php的方法就是定义在类里面的方法,一般不建议在方法内部定义方法,但是这种也可以这种叫做内部方法,一般只能本方法调用. 如果定义在同一个类中的方法,在同类的其他方法中调用是$this->方法名就 ...

  6. windows安装tensorflow简单直接的方法(win10+pycharm+tensorflow-gpu1.7+cuda9.1+cudnn7.1)

    安装tensorflow-gpu环境需要:python环境,tensorflow-gpu包,cuda,cudnn 一,安装python,pip3直接到官网下载就好了,下载并安装你喜欢的版本 https ...

  7. JavaScript查找数组中最大的值

    // 查找一个数组中最大的数 // 定义一个方法 searchMax function searchMax(arr) { // 声明一个变量MaxNumber假设为数组中最大的值arr[0]; var ...

  8. emqtt 试用(三)mqtt 知识

    一.概念 MQTT 协议客户端库: https://github.com/mqtt/mqtt.github.io/wiki/libraries 例如,mosquitto_sub/pub 命令行发布订阅 ...

  9. hadoop2.6.0实践:002 检查伪分布式环境搭建

    1.检查网络配置[root@hadoop-master ~]# cat /etc/sysconfig/networkNETWORKING=yesHOSTNAME=hadoop-masterGATEWA ...

  10. Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类

    本文目录 1.  前沿 2.CacheHelper基于Microsoft.Extensions.Caching.Memory封装 3.XmlHelper快速操作xml文档 4.Serializatio ...