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的数据结构(负十四)的更多相关文章

  1. COJ 1010 WZJ的数据结构(十) 线段树区间操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1001 WZJ的数据结构(十) 难度级别:D: 运行时间限制:3000ms: ...

  2. COJ 0986 WZJ的数据结构(负十四) 区间动态k大

    题解:哈哈哈我过了!!!主席树+树状数组写起来还真是hentai啊... 在这里必须分享我的一个沙茶错!!!看这段代码: void get(int x,int d){ ]=root[x];x;x-=x ...

  3. COJ1012 WZJ的数据结构(十二)

    今天突然想写个树套树爽一爽(1810ms) 写的是树状数组套线段树(动态开节点) #include<cstdio> #include<cctype> #include<c ...

  4. Java数据结构(十四)—— 平衡二叉树(AVL树)

    平衡二叉树(AVL树) 二叉排序树问题分析 左子树全部为空,从形式上看更像一个单链表 插入速度没有影响 查询速度明显降低 解决方案:平衡二叉树 基本介绍 平衡二叉树也叫二叉搜索树,保证查询效率较高 它 ...

  5. COJ966 WZJ的数据结构(负三十四)

    WZJ的数据结构(负三十四) 难度级别:C: 运行时间限制:20000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给一棵n个节点的树,请对于形如"u  ...

  6. [COJ0985]WZJ的数据结构(负十五)

    [COJ0985]WZJ的数据结构(负十五) 试题描述 CHX有一个问题想问问大家.给你一个长度为N的数列A,请你找到两个位置L,R,使得A[L].A[L+1].…….A[R]中没有重复的数,输出R- ...

  7. [COJ0988]WZJ的数据结构(负十二)

    [COJ0988]WZJ的数据结构(负十二) 试题描述 输入 见题目,注意本题不能用文件输入输出 输出 见题目,注意本题不能用文件输入输出 输入示例 输出示例 数据规模及约定 1≤N≤1500,M≤N ...

  8. COJ970 WZJ的数据结构(负三十)

    WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...

  9. COJ968 WZJ的数据结构(负三十二)

    WZJ的数据结构(负三十二) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,边上均有权值,每个点上有 ...

随机推荐

  1. HDOJ 1272 并查集

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  2. Linux 的shell 字符串截取很有用。有八种方法。

    一 Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.linuxidc.com/123.htm 1  # 号截取,删除左边字符,保留右边字符. echo ${va ...

  3. 【云计算】Docker删除名称为none的Image镜像

    先上删除命令: docker images|grep none|awk '{print $3 }'|xargs docker rmi docker强制批量删除none的image镜像   真是有段时间 ...

  4. tcp ip detatils

    tcp ip detatils 8.关于TCP协议,下面哪种说法是错误的()A.TCP关闭连接过程中,两端的socket都会经过TIME_WAIT状态B.对一个Established状态的TCP连接, ...

  5. MFC添加鼠标相应事件

    Class View (类视图)窗口中选中 你要添加事件的类(比如C***View.CPP),切换到properties窗口. 点击上面的Message图标(在Event图标[一个闪电形状的图标] 右 ...

  6. linux tricks 之 roundup.

    转载:http://stackoverflow.com/questions/1010922/question-about-round-up-macro 以下内容转载自stackoverflow关于 r ...

  7. 5个让你的SaaS应用大卖的技巧

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 今天推荐的文章和具体的技术无关,但是对于创业的小伙伴应该有帮助. 去年底到今年,企业应用尤其 ...

  8. vb.net三层实现登录例子

    看三层已经很长时间了,中间有经过了期末考试.回家等等琐事,寒假开学的我已经回想不起什么事三层了,经过了三四天的重新复习,再加上查看各期师哥师姐的博客,终于,自己完成了C#视频中的登录小例子,下面就和大 ...

  9. aggregateByKey

    def seq(a:Int, b:Int) : Int ={ math.max(a,b) } def comb(a:Int, b:Int) : Int ={ a + b } val data = sc ...

  10. oracle的常用函数

    1. nvl NVL函数的格式如下:NVL(expr1,expr2) 含义是:如果oracle第一个参数expr1为空,那么显示第二个参数的值为expr2,如果第一个参数的值expr1不为空,则显示第 ...