Hdu CRB and Queries(整体二分)
CRB and Queries
Time Limit: 6000 MS Memory Limit: 131072 K
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
Author
KUT(DPRK)
Source
2015 Multi-University Training Contest 10
/*
动态区间第K小.
把操作离线.
修改操作视为一个删除操作和一个添加操作.
然后和静态处理方式相同.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 400001
#define INF 1e9
using namespace std;
struct data{int x,y,k,s,o,cut;}
q[MAXN],tmp1[MAXN],tmp2[MAXN];
int n,m,tot,cut,s[MAXN],ans[MAXN],a[MAXN],tmp[MAXN];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
void add(int x,int z)
{
while(x<=n) s[x]+=z,x+=x&-x;
return ;
}
int getsum(int x)
{
int sum=0;
while(x>0) sum+=s[x],x-=x&-x;
return sum;
}
void slove(int head,int tail,int l,int r)
{
if(head>tail) return ;
if(l==r)
{
for(int i=head;i<=tail;i++)
if(q[i].o==3) ans[q[i].s]=l;
return ;
}
int mid=(l+r)>>1;
for(int i=head;i<=tail;i++)//统计贡献.
{
if(q[i].o==1&&q[i].y<=mid) add(q[i].x,1);
else if(q[i].o==2&&q[i].y<=mid) add(q[i].x,-1);
else if(q[i].o==3) tmp[i]=getsum(q[i].y)-getsum(q[i].x-1);
}
for(int i=head;i<=tail;i++)
{
if(q[i].y<=mid)
{
if(q[i].o==1) add(q[i].x,-1);
else if(q[i].o==2) add(q[i].x,1);
}
}
int l1=0,l2=0;
for(int i=head;i<=tail;i++)
{
if(q[i].o==3)
{
if(q[i].cut+tmp[i]>q[i].k-1) tmp1[++l1]=q[i];
//对于该操作找一个所属答案位置.
else q[i].cut+=tmp[i],tmp2[++l2]=q[i];
}
else
{
if(q[i].y<=mid) tmp1[++l1]=q[i];
else tmp2[++l2]=q[i];
}
}
for(int i=1;i<=l1;i++) q[head+i-1]=tmp1[i];
for(int i=1;i<=l2;i++) q[head+l1+i-1]=tmp2[i];
slove(head,head+l1-1,l,mid),slove(head+l1,tail,mid+1,r);
return ;
}
void Clear()
{
memset(q,0,sizeof q);
//memset(s,0,sizeof s);
//memset(ans,0,sizeof ans);
tot=cut=0;return ;
}
int main()
{
int x,y,z,k;
while(scanf("%d",&n)!=EOF)
{
Clear();
for(int i=1;i<=n;i++)
{
a[i]=read();
q[++tot].x=i,q[tot].y=a[i];
q[tot].o=1,q[tot].s=0;
}
m=read();
while(m--)
{
z=read();
if(z==2)
{
x=read(),y=read(),k=read();
q[++tot].x=x,q[tot].y=y,q[tot].k=k;
q[tot].o=3;q[tot].s=++cut;
}
else
{
x=read(),y=read();
q[++tot].x=x,q[tot].y=a[x];
q[tot].o=2,q[tot].s=0;
q[++tot].x=x,q[tot].y=y;
q[tot].o=1,q[tot].s=0;
a[x]=y;
}
}
slove(1,tot,-INF,INF);
for(int i=1;i<=cut;i++) printf("%d\n",ans[i]);
}
return 0;
}
Hdu CRB and Queries(整体二分)的更多相关文章
- HDU - 5412 CRB and Queries (整体二分)
题目链接 动态区间第k小,但是这道题的话用主席树+树状数组套线段树的空间复杂度是O(nlog2n)会爆掉. 另一种替代的方法是用树状数组套平衡树,空间复杂度降到了O(nlogn),但我感觉平衡树是个挺 ...
- hdu 5412 CRB and Queries(整体二分)
题意 动态区间第k大 (n<=100000,m<=100000) 题解 整体二分的应用. 与静态相比差别不是很大.(和CDQ还有点像)所以直接上代码. #include<iostre ...
- HDU5412 CRB and Queries 整体二分
传送门 刚觉得最近写代码比较顺畅没什么Bug,cdq分治真是我的一个噩梦.. 整体二分模板题,带修改的区间第k小. vjudge不知抽什么风,用不了,hdu忘了密码了一直在那里各种试,难受.. 写得比 ...
- hdu 5412 CRB and Queries
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5412 CRB and Queries Description There are $N$ boys i ...
- HDU 5412——CRB and Queries——————【线段树套Treap(并没有AC)】
CRB and Queries Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- 整体二分 HDU - 5808
题目大意 有n个物品,排成一个序列,每个物品有一个di表示取到i要走的距离,vi表示i的价值. 给m组询问[l,r] ,c,sum,问由[l,r]的di<=c的物品能否凑出sum的价值(每个物品 ...
- 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 ...
- (困难) CF 484E Sign on Fence,整体二分+线段树
Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence o ...
- CQD(陈丹琦)分治 & 整体二分——专题小结
整体二分和CDQ分治 有一些问题很多时间都坑在斜率和凸壳上了么--感觉斜率和凸壳各种搞不懂-- 整体二分 整体二分的资料好像不是很多,我在网上找到了一篇不错的资料: 整体二分是个很神的东西 ...
随机推荐
- 【exgcd】卡片
卡片 题目描述 你有一叠标号为1到n的卡片.你有一种操作,可以重排列这些卡片,操作如下:1.将卡片分为前半部分和后半部分.2.依次从后半部分,前半部分中各取一张卡片,放到新的序列中.例如,对卡片序列( ...
- 三元组[01 Trie计数]
也许更好的阅读体验 \(\mathcal{Description}\) \(\mathcal{Solution}\) 有两种方法都可以拿到满分 \(Solution\ 1\) 考虑枚举\(y\) 建两 ...
- java 随机生成4位随机数
java 随机生成4位的随机数测试类 @org.junit.Testpublic void testRandom(){ String msg="您的注册码为%s,谢谢注册!"; S ...
- PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- hdu 2586 欧拉序+rmq 求lca
题意:求树上任意两点的距离 先说下欧拉序 对这颗树来说 欧拉序为 ABDBEGBACFHFCA 那欧拉序有啥用 这里先说第一个作用 求lca 对于一个欧拉序列,我们要求的两个点在欧拉序中的第一个位置之 ...
- C#插入时间
//获取日期+时间 DateTime.Now.ToString(); // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToString(); // 20 ...
- mvc伪静态
方法一:IIS配置伪静态 方法二:项目配置伪静态 网站配置文件Web.config <system.webServer> <handlers> <add name=&qu ...
- 【转载】IIS网站如何同时解析带www和不带www的域名
针对公网上线的网站系统,很多网站的域名会同时含有带www和不带www的域名解析记录,如果需要同时解析带www和不带www的域名信息,则需要在相应的域名解析平台(如阿里云域名解析平台.腾讯云域名解析平台 ...
- pdm文件打开方式
转自:https://blog.csdn.net/qq_36855191/article/details/79299216 pdm打开网站:http://www.dmanywhere.cn/
- 本地数据存储解决方案以及cookie的坑
本地数据存储解决方案以及cookie的坑 问题: cookie过长导致页面打开失败 背景: 在公司的项目中有一个需求是打开多个工单即在同一个页面中打开了多个tab(iframe),但是需要在刷新时只刷 ...