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个点的无根树,边上均有权值,每个点上有 ...
随机推荐
- Linux运维相关目录
- 快速传输大数据(tar+lz4+pv)
快速传输大数据(tar+lz4+pv) 如果用传统SCP远程拷贝,速度是比较慢的.现在采用lz4压缩传输.LZ4是一个非常快的无损压缩算法,压缩速度在单核300MB/S,可扩展支持多核CPU.它还 ...
- ORM框架是什么
ORM框架是什么 对象关系映射,目前数据库是关系型数据库 ORM 主要是把数据库中的关系数据映射称为程序中的对象 目前集中常见的ORM框架1 Nhibernate原因:用的比较多,资料也比较好找. ...
- Adobe Flash Player 因过期而遭到阻止 更新插件 运行一次 解决方法
老机器运行 10.3.183.90 比较流畅 可是 Chrome 浏览器提示 Adobe Flash Player 因过期而遭到阻止 更新插件 运行一次 每次单击 运行一次 才运行,这样每次提醒很烦人 ...
- 20.python笔记之装饰器
装饰器 装饰器是函数,只不过该函数可以具有特殊的含义,装饰器用来装饰函数或类,使用装饰器可以在函数执行前和执行后添加相应操作. 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插 ...
- Greedy:Stripes(POJ 1826)
新生物 题目大意:给你一堆数,两两结合,答案为2*sqrt(x1*x2),问组合成一个数时,最小的量? 超级无敌大水题,排序或者用堆都可以,反正就是优先组合大的,让根号一直把大数开根降低整体的大小 # ...
- Extjs的数据读取器store和后台返回类型简单解析
工作中用到了Extjs,从后台获取数据的时候,用到了extjs自己的Ext.data.store方法,然后封装了ExtGridReturn方法, 目的:前台用到Ext.data.store读取从后台传 ...
- mybatis There is no getter for property named 'xx' in 'class java.lang.String
转载自://http://www.cnblogs.com/anee/p/3324140.html 用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 There is no getter ...
- [Git] Git 文件归档, include submodule
git archive命令,可以对任意提交对应的目录树建立归档. $ git archive -o latest.zip HEAD 基于最新提交建立归档文件latest.zip $ git ar ...
- QQ图片名字
ﻩ并亲了你一下ﻩ大兔子҉҉大兔子҉҉҉҉҉҉҉҉