您的位置:首页 > 理论基础 > 数据结构算法

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

2015-07-23 12:36 477 查看
题解:哈哈哈我过了!!!主席树+树状数组写起来还真是hentai啊。。。

在这里必须分享我的一个沙茶错!!!看这段代码:

void get(int x,int d){
if(!d)for(lt[ltot=1]=root[x];x;x-=x&-x)if(c[x])lt[++ltot]=c[x];
else  for(rt[rtot=1]=root[x];x;x-=x&-x)if(c[x])rt[++rtot]=c[x];return;
}


似乎挺正常是吧?而且缩进也很优美对不对!!!

可是!!!!!!!!窝萌发现它是错的!!!!!!!!!!

因为下面那个else跟上面的if配对了!!!!!!所以这段程序其实是这样的:

void get(int x,int d){
if(!d)for(lt[ltot=1]=root[x];x;x-=x&-x){
if(c[x])lt[++ltot]=c[x];
else{
for(rt[rtot=1]=root[x];x;x-=x&-x)
if(c[x])rt[++rtot]=c[x];
}
}return;
}


我要疯了!!!!!!!!!!!!!!!!!!!

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
#define CH for(int d=0;d<2;d++)if(ch[d])
#define lson ls(x),y->ch[0],L,M
#define rson rs(x),y->ch[1],M+1,R
using namespace std;
const int maxn=100000+10,maxnode=20000000+10,inf=-1u>>1,vl=1,vr=100000;
struct node{
node*ch[2];int siz;node(){siz=0;}
}pol[maxnode],*nodecnt=pol,*root[maxn],*c[maxn],*lt[maxn],*rt[maxn];int ltot,rtot;
node*ls(node*x){return x?x->ch[0]:x;}
node*rs(node*x){return x?x->ch[1]:x;}
int sz(node*x){return x?x->siz:0;}
int n,m,A[maxn],cv;
void build(int v,int pos,node*x,node*&y,int L=vl,int R=vr){
y=nodecnt++;y->siz=sz(x)+v;if(L==R)return;int M=L+R>>1;
if(pos<=M)y->ch[1]=rs(x),build(v,pos,lson);else y->ch[0]=ls(x),build(v,pos,rson);return;
}
void update(int x,int v){
for(int w=x;w<=n;w+=w&-w)build(-1,A[x],c[w],c[w]);
for(int w=x;w<=n;w+=w&-w)build(1,A[x]=v,c[w],c[w]);return;
}
void get(int x,int d){
if(!d){for(lt[ltot=1]=root[x];x;x-=x&-x)if(c[x])lt[++ltot]=c[x];}
else  {for(rt[rtot=1]=root[x];x;x-=x&-x)if(c[x])rt[++rtot]=c[x];}return;
}
inline int read(){
int x=0,sig=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0;
for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';
return sig?x:-x;
}
inline void write(int x){
if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
}
void init(){
n=read();m=read();
for(int i=1;i<=n;i++)build(1,A[i]=read(),root[i-1],root[i]);
return;
}
void work(){
int x,y,v;
while(m--){
if(!read())x=read(),v=read(),update(x,v);
else{x=read();y=read();v=read();
get(x-1,0);get(y,1);int L=vl,R=vr,M,kth;
while(L<R){M=L+R>>1;kth=0;//printf("%d %d %d %d\n",L,R,ltot,rtot);
for(int i=1;i<=ltot;i++)kth-=sz(ls(lt[i]));
for(int i=1;i<=rtot;i++)kth+=sz(ls(rt[i]));
if(kth>=v){R=M;
for(int i=1;i<=ltot;i++)lt[i]=ls(lt[i]);
for(int i=1;i<=rtot;i++)rt[i]=ls(rt[i]);
}else{L=M+1;v-=kth;
for(int i=1;i<=ltot;i++)lt[i]=rs(lt[i]);
for(int i=1;i<=rtot;i++)rt[i]=rs(rt[i]);
}
}write(L);ENT;
}
}
return;
}
void print(){
return;
}
int main(){init();work();print();return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: