COJ986 WZJ的数据结构(负十四)
WZJ的数据结构(负十四) |
难度级别:D; 运行时间限制:6000ms; 运行空间限制:262144KB; 代码长度限制:2000000B |
试题描述
|
请你设计一个数据结构,完成以下功能: 给定一个大小为N的整数组A,M次操作,操作分两种: 1.1 i j k 每次询问给你i,j,k三个参数,求Ai至Aj中第k小的数。 2.0 x v 每次操作给你x,v两个参数,将A[x]改成v。 |
输入
|
第一行为两个正整数N,M。
第二行为N个正整数Ai。 接下来M行为操作。 |
输出
|
对于每个询问输出答案(保证k合法)。
|
输入示例
|
6 8
1 3 2 2 5 3 1 1 3 2 1 1 4 2 1 1 4 3 1 1 4 4 0 4 3 1 1 4 3 0 2 5 1 1 4 4 |
输出示例
|
2
2 2 3 3 5 |
其他说明
|
1<=N,M,Ai,v<=100000
1<=i<=j<=N 1<=k<=j-i+1 1<=x<=N |
写一个二分+树状数组+Treap,挺爽的
#include<cstdio>
#include<cctype>
#include<queue>
#include<ctime>
#include<cstring>
#include<algorithm>
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
const int maxnode=;
struct Node {
Node* ch[];
int r,s,v;
void maintain() {s=ch[]->s+ch[]->s+;}
}nodes[maxnode],*null=&nodes[];
queue<Node*> del;
int n,A[maxn],ToT;
Node* newnode(int v) {
Node* o;
if(!del.empty()) o=del.front(),del.pop();
else o=&nodes[++ToT];
o->ch[]=o->ch[]=null;
o->s=;o->v=v;o->r=rand();
return o;
}
void remove(Node* &o) {
del.push(o);o=null;
}
void rotate(Node* &o,int d) {
Node* k=o->ch[d^];o->ch[d^]=k->ch[d];k->ch[d]=o;
o->maintain();k->maintain();o=k;
}
void insert(Node* &o,int v) {
if(o==null) o=newnode(v);
else {
int d=v>o->v;insert(o->ch[d],v);
if(o->ch[d]->r>o->r) rotate(o,d^);
else o->maintain();
}
}
void remove(Node* &o,int v) {
if(o->v==v) {
Node* t=o;
if(o->ch[]==null) o=o->ch[],remove(t);
else if(o->ch[]==null) o=o->ch[],remove(t);
else {
int d=o->ch[]->r>o->ch[]->r;
rotate(o,d);remove(o->ch[d],v);
}
}
else remove(o->ch[v>o->v],v);
if(o!=null) o->maintain();
}
void print(Node* o) {
if(o==null) return;
print(o->ch[]);
printf("%d ",o->v);
print(o->ch[]);
}
int query(Node* &o,int v) {
if(o==null) return ;
if(v<=o->v) return query(o->ch[],v);
return query(o->ch[],v)+o->ch[]->s+;
}
Node* root[maxn];
void insert(int x,int v) {for(;x<=n;x+=x&-x) insert(root[x],v);}
void update(int x,int v) {for(;x<=n;x+=x&-x) remove(root[x],v);}
int query(int x,int v) {int ret=;for(;x;x-=x&-x) ret+=query(root[x],v+);return ret;}
int main() {
null->s=;srand(time());
n=read();int m=read();
rep(,n) root[i]=null;
rep(,n) insert(i,A[i]=read());
while(m--) {
if(read()) {
int l=read(),r=read(),k=read();
int L=,R=,M;
while(L<R) if(query(r,M=L+R>>)-query(l-,M=L+R>>)>=k) R=M; else L=M+;
printf("%d\n",L);
}
else {
int x=read();update(x,A[x]);
insert(x,A[x]=read());
}
}
return ;
}
复习一下树状数组+可持久化线段树,写WA了一发真不爽
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
const int maxnode=;
int ls[maxnode],rs[maxnode],s[maxnode],ToT;
int n,m,A[maxn],root[maxn],c[maxn];
void update(int& y,int x,int l,int r,int pos,int v) {
s[y=++ToT]=s[x]+v;if(l==r) return;
int mid=l+r>>;ls[y]=ls[x];rs[y]=rs[x];
if(pos<=mid) update(ls[y],ls[x],l,mid,pos,v);
else update(rs[y],rs[x],mid+,r,pos,v);
}
void update(int x,int v) {
for(int i=x;i<=n;i+=i&-i) update(c[i],c[i],,,A[x],-);
for(int i=x;i<=n;i+=i&-i) update(c[i],c[i],,,A[x]=v,);
}
int lt[maxn],rt[maxn],ltot,rtot;
void get(int x,int tp) {
if(!tp) {lt[ltot=]=root[x];for(;x;x-=x&-x) if(c[x]) lt[++ltot]=c[x];}
else {rt[rtot=]=root[x];for(;x;x-=x&-x) if(c[x]) rt[++rtot]=c[x];}
}
int main() {
n=read();m=read();
rep(,n) update(root[i],root[i-],,,A[i]=read(),);
while(m--) {
if(!read()) {
int x=read(),v=read();
update(x,v);
}
else {
int ql=read(),qr=read(),k=read();
get(ql-,);get(qr,);int l=,r=;
while(l<r) {
int mid=l+r>>,tot=;
rep(,ltot) tot-=s[ls[lt[i]]];
rep(,rtot) tot+=s[ls[rt[i]]];
if(tot>=k) {
r=mid;
rep(,ltot) lt[i]=ls[lt[i]];
rep(,rtot) rt[i]=ls[rt[i]];
}
else {
l=mid+;k-=tot;
rep(,ltot) lt[i]=rs[lt[i]];
rep(,rtot) rt[i]=rs[rt[i]];
}
}
printf("%d\n",l);
}
}
return ;
}
COJ986 WZJ的数据结构(负十四)的更多相关文章
- COJ 1010 WZJ的数据结构(十) 线段树区间操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1001 WZJ的数据结构(十) 难度级别:D: 运行时间限制:3000ms: ...
- COJ 0986 WZJ的数据结构(负十四) 区间动态k大
题解:哈哈哈我过了!!!主席树+树状数组写起来还真是hentai啊... 在这里必须分享我的一个沙茶错!!!看这段代码: void get(int x,int d){ ]=root[x];x;x-=x ...
- COJ1012 WZJ的数据结构(十二)
今天突然想写个树套树爽一爽(1810ms) 写的是树状数组套线段树(动态开节点) #include<cstdio> #include<cctype> #include<c ...
- Java数据结构(十四)—— 平衡二叉树(AVL树)
平衡二叉树(AVL树) 二叉排序树问题分析 左子树全部为空,从形式上看更像一个单链表 插入速度没有影响 查询速度明显降低 解决方案:平衡二叉树 基本介绍 平衡二叉树也叫二叉搜索树,保证查询效率较高 它 ...
- COJ966 WZJ的数据结构(负三十四)
WZJ的数据结构(负三十四) 难度级别:C: 运行时间限制:20000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给一棵n个节点的树,请对于形如"u ...
- [COJ0985]WZJ的数据结构(负十五)
[COJ0985]WZJ的数据结构(负十五) 试题描述 CHX有一个问题想问问大家.给你一个长度为N的数列A,请你找到两个位置L,R,使得A[L].A[L+1].…….A[R]中没有重复的数,输出R- ...
- [COJ0988]WZJ的数据结构(负十二)
[COJ0988]WZJ的数据结构(负十二) 试题描述 输入 见题目,注意本题不能用文件输入输出 输出 见题目,注意本题不能用文件输入输出 输入示例 输出示例 数据规模及约定 1≤N≤1500,M≤N ...
- COJ970 WZJ的数据结构(负三十)
WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...
- COJ968 WZJ的数据结构(负三十二)
WZJ的数据结构(负三十二) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,边上均有权值,每个点上有 ...
随机推荐
- 如何给spine骨骼动画挂载粒子特效
目的是要把粒子挂载到骨骼动画的某个一个部件上,其实最主要是找对位置. 预览效果,左手红火,右手蓝火,很炫吧:) //init bool HelloWorld::init() { /////////// ...
- 正则表达式里"-"中划线的使用注意
今天要匹配正则表达式,把非法的字符找出来,开始的写法是这个 [^A-Za-z0-9_.*-+%!],我的目的是把_.*-+%!这7个字符算合法字符,但是发现有许多其他字符也合法了,原来是中划线的位置不 ...
- iOS7 中的statusbar的隐藏和样式更改
ios7以前,如果想要隐藏statusbar,需要用到[UIApplicationsharedApplication].statusBarHidden = YES; 或者在plist文件中设定Stat ...
- ARM 处理器的几个相关术语
生产ARM的厂商很多,自然ARM处理器的名字就五花八门.但是,它们有些共同点,那就是:架构和核心. 架构这个概念太宽不太懂,一般不同的架构会有不同的指令集,在不同的架构下面还可以有多种核心.核心就是指 ...
- Emacs 从入门到精通
1 前言 不想再说废话了,既然你会阅读这篇文档,说明你多少对Emacs有 些兴趣,或者已 经非常熟悉Emacs的基础操作了,并且希望有所提高.因此我不需要再把"编辑器 之神,还是神的编辑器& ...
- 读取STL模型
读取二进制格式的STL模型文件 std::ifstream fin;fin.open(stlFilePath, std::ios::in | std::ios::binary);bool isBina ...
- java单例,懒汉&饿汉
* 单例模式Singleton * 应用场合:有些对象只需要一个就足够了,如皇帝 * 作用: 保证整个应用程序中某个实例有且只有一个 * 区别: 饿汉模式的特点是加载类时比较慢,但运行是比较快 ...
- 禅道bug安装报错
[root@lnmp src]# grep "session.save" /etc/php.ini ; http://php.net/session.save-handler se ...
- Sublime Text 插件 & 使用技巧
20 个强大的 Sublime Text 插件http://blog.jobbole.com/58725/ 12个不可不知的Sublime Text应用技巧和诀窍http://segmentfault ...
- 5个让你的SaaS应用大卖的技巧
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 今天推荐的文章和具体的技术无关,但是对于创业的小伙伴应该有帮助. 去年底到今年,企业应用尤其 ...