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 2066 floyed优化算法

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  2. eclipse debug source not fount

    1.选择Edit Source Lookup Path 2.选择Add 3.选择Java Project 4.选择相应的Project 进行OK确定即可 注意:做完以上的操作,要清除一下原来的断点,然 ...

  3. python操作Excel读写--使用xlrd

    一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 import x ...

  4. (转)SQL Server 中的事务和锁(三)-Range S-U,X-X 以及死锁

    在上一篇中忘记了一个细节.Range T-K 到底代表了什么?Range T-K Lock 代表了在 SERIALIZABLE 隔离级别中,为了保护范围内的数据不被并发的事务影响而使用的一类锁模式(避 ...

  5. Python多进程(1)——subprocess与Popen()

    Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...

  6. mybatis的jdbcType类型

    在用mybatis的时候,如果传过来的参数有可能为空,那么就要指定jdbcType是什么了,否则会有异常,jdbcType有以下几种: BIT         FLOAT      CHAR      ...

  7. July 13th, Week 29th Wednesday, 2016

    Travel imparts new vigor to the mind. 旅行能给思想带来新的活力. Travel can give us opportunities to experience m ...

  8. vmware安装linux.iso

    安装方法 : .进入Fedora后,在虚拟机选项栏中选VM->install vmware tools 拷贝VMware Tools.tar.gz到指定文件夹,解压缩 进入超级终端:在-> ...

  9. 一、HTML和CSS基础--网页布局--实践--导航条菜单的制作

    案例一:导航菜单的制作 垂直菜单

  10. Perl中的正则表达式

    转自:http://c20031776.blog.163.com/blog/static/684716252013624383887/ Perl 程序中,正则表达式有三种存在形式 分别是 (1 模式匹 ...