【传送门:caioj1442


简要题意:

  给出n个点,每个点都有一个权值,m个操作,操作有两种:第一种是询问l到r的第k小的值,然后输出这个值,第二种是将第x个点的值改为k


题解:

  又是一道主席树的例题,不过简直比前两题(caioj1441,caioj1443)难不止一点点

  看到第一种操作,我们可以用主席树来搞,但是第二种操作的话,我们就要用树状数组来维护每个第二种操作所带来的影响,而且每次要求第一种操作的时候都要先在树状数组中找到相应的点,然后再处理主席树


参考代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
int a[];
struct node
{
int lc,rc,c;
}tr[];int cnt;
int rt[];int n;
int ust[];//树状数组
int lowbit(int x)//树状数组用来找上司或下属
{
return x&-x;
}
void Link(int &u,int l,int r,int p,int c)
{
if(u==) u=++cnt;
tr[u].c+=c;
if(l==r) return ;
int mid=(l+r)/;
if(p<=mid) Link(tr[u].lc,l,mid,p,c);
else Link(tr[u].rc,mid+,r,p,c);
}
void Merge(int &u1,int u2)
{
if(u1==){u1=u2;return ;}
if(u2==) return ;
tr[u1].c+=tr[u2].c;
Merge(tr[u1].lc,tr[u2].lc);
Merge(tr[u1].rc,tr[u2].rc);
}
void Turn(int u,int c)//树状数组的实时更新
//c==-1时树状数组记录每个点所在线段树的根
//c==0时记录左孩子
//c==1时记录右孩子
{
while(u>=n+)
{
if(c==-) ust[u]=rt[u];
else if(c==) ust[u]=tr[ust[u]].lc;
else if(c==) ust[u]=tr[ust[u]].rc;
u-=lowbit(u);
}
}
void Modefy(int u,int p,int c)//对于修改值就用树状数组来节约时间
{
while(u<=*n)
{
Link(rt[u],,,p,c);
u+=lowbit(u);
}
}
int Getsum(int u)//求出修改过后树状数组得到的值
{
int ret=;
while(u>=n+)
{
ret+=tr[tr[ust[u]].lc].c;
u-=lowbit(u);
}
return ret;
}
int Ask(int u1,int u2,int p1,int p2,int l,int r,int p)
{
if(l==r) return l;
int c=tr[tr[u2].lc].c-tr[tr[u1].lc].c+Getsum(p2+n)-Getsum(p1+n);
int mid=(l+r)/;
if(p<=c)
{
Turn(p1+n,);
Turn(p2+n,);
return Ask(tr[u1].lc,tr[u2].lc,p1,p2,l,mid,p);
}
else
{
Turn(p1+n,);
Turn(p2+n,);
return Ask(tr[u1].rc,tr[u2].rc,p1,p2,mid+,r,p-c);
}
}
int main()
{
int m;
scanf("%d%d",&n,&m);
cnt=;memset(rt,,sizeof(rt));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
Link(rt[i],,,a[i],);
Merge(rt[i],rt[i-]);
}
char st[];
for(int i=;i<=m;i++)
{
scanf("%s",st+);
if(st[]=='Q')
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
Turn(l+n-,-);
Turn(r+n,-);
printf("%d\n",Ask(rt[l-],rt[r],l-,r,,,k));
}
else
{
int p,c;
scanf("%d%d",&p,&c);
Modefy(p+n,a[p],-);
a[p]=c;
Modefy(p+n,a[p],);
}
}
return ;
}

caioj1442:第k小的数Ⅱ的更多相关文章

  1. *HDU2852 树状数组(求第K小的数)

    KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. 计算序列中第k小的数

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4046399.html 使用分治算法,首先选择随机选择轴值pivot,并使的序列中比pivot ...

  3. [LeetCode] Find K-th Smallest Pair Distance 找第K小的数对儿距离

    Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...

  4. #7 找出数组中第k小的数

    「HW面试题」 [题目] 给定一个整数数组,如何快速地求出该数组中第k小的数.假如数组为[4,0,1,0,2,3],那么第三小的元素是1 [题目分析] 这道题涉及整数列表排序问题,直接使用sort方法 ...

  5. 无序数组求第k大/第k小的数

    根据http://www.cnblogs.com/zhjp11/archive/2010/02/26/1674227.html 博客中所总结的7种解法,我挑了其中的解法3和解法6进行了实现. 解法3: ...

  6. 查找第K小的数 BFPRT算法

    出处 http://blog.csdn.net/adong76/article/details/10071297 BFPRT算法是解决从n个数中选择第k大或第k小的数这个经典问题的著名算法,但很多人并 ...

  7. 基于快速排序思想partition查找第K大的数或者第K小的数。

    快速排序 下面是之前实现过的快速排序的代码. function quickSort(a,left,right){ if(left==right)return; let key=partition(a, ...

  8. 算法---数组总结篇2——找丢失的数,找最大最小,前k大,第k小的数

    一.如何找出数组中丢失的数 题目描述:给定一个由n-1个整数组成的未排序的数组序列,其原始都是1到n中的不同的整数,请写出一个寻找数组序列中缺失整数的线性时间算法 方法1:累加求和 时间复杂度是O(N ...

  9. cogs930找第k小的数(k-th number)

    cogs930找第k小的数(k-th number) 原题链接 题解 好题... 终极版是bzoj3065(然而并不会) 先讲这个题... 维护\(n+1\)个值域线段树(用主席树),标号\(0\) ...

随机推荐

  1. Java中hashCode与equal方法详解

    转载自http://blog.csdn.net/jiangwei0910410003/article/details/22739953 Java中的equals方法和hashCode方法是Object ...

  2. 前端路由的两种模式:hash(#)模式和history模式(转)

    随着 ajax 的使用越来越广泛,前端的页面逻辑开始变得越来越复杂,特别是spa的兴起,前端路由系统随之开始流行. 从用户的角度看,前端路由主要实现了两个功能(使用ajax更新页面状态的情况下): 记 ...

  3. pytorch 2 variable 变量

    import torch from torch.autograd import Variable tensor = torch.FloatTensor([[1, 2], [3, 4]]) variab ...

  4. UI布局【转】

    转载自: https://www.cnblogs.com/wangdaijun/p/5519459.html https://www.jianshu.com/p/f781c40df57c Good U ...

  5. python学习--导入自己的包

    定义一个自己的方法包: def myFunc(x): if x > 10: return x else: return -x 在需要的地方导入包: # 导入自定义的方法包 from learn ...

  6. 浅析为什么 char 类型的范围是 : -128~+127

    在 C 语言中. signed char 类型的范围为 -128~127,每本教科书上也这么写.可是没有哪一本书上(包含老师)也不会给你为什么是 -128~127,这个问题貌似看起来也非常easyea ...

  7. ubuntu SDK 安装

    纯净sdk安装1.地址-http://gmirror.org/#android-sdk-tools-only(国内镜像)2.下载到本地目录 ~/下载3.进入下载,解压 tar -zxvf androi ...

  8. iOS开发-sqlite3使用

    SQLite3使用 SQLite简介 SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中. SQLite3 在XCode工程中,打开targets,在B ...

  9. spark 数据预处理 特征标准化 归一化模块

    #We will also standardise our data as we have done so far when performing distance-based clustering. ...

  10. 41.AngularJS 服务(Service)

    转自:https://www.cnblogs.com/best/tag/Angular/ 什么是服务? 在 AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用. A ...