bzoj 3065 带插入区间k小值

我去,sb爆了。。。。二分的时候没考虑重复,tle了一天没找出来,又是这种神奇的小错误,555555。。。。

/**************************************************************
    Problem: 3065
    User: Clare
    Language: C++
    Result: Accepted
    Time:47624 ms
    Memory:247744 kb
****************************************************************/
 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
 
#define N 100010
const double a=0.75;
 
int n,m,tot,S_tot,Root,Tot,Ans,Last_val,sheep;
struct Node{
    int L,R,sum;
}t[N*200];
int fa[N],root[N],c[N][2],v[N],id[N],size[N],A[N];
queue Q;
 
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
 
void Rec_seg(int &k)     //Correct
{
    if(!k)return;
    if(t[k].L)Rec_seg(t[k].L);
    if(t[k].R)Rec_seg(t[k].R);
    Q.push(k);t[k].sum=0;k=0;
}
 
int New_node()     //Correct
{
    if(!Q.empty())
    {
        int now=Q.front();Q.pop();
        return now;
    }
    else return ++tot;
}
 
void Insert(int &i,int l,int r,int pos,int val)   //Correct
{
    if(!i)i=New_node();
    if(l==r)
    {
        t[i].sum+=val;
        return;
    }
    int mid=(l+r)/2;
    if(pos<=mid)
        Insert(t[i].L,l,mid,pos,val);
    else Insert(t[i].R,mid+1,r,pos,val);
    t[i].sum=t[t[i].L].sum+t[t[i].R].sum;
    if(!t[i].sum)Rec_seg(i);
}
 
int Query_lessthan(int i,int l,int r,int pos)   //Correct
{
    if(!i)return 0;
    if(l==r)
        return t[i].sum;
    int mid=(l+r)/2;
    if(pos<=mid)
        return Query_lessthan(t[i].L,l,mid,pos);
    else return Query_lessthan(t[i].R,mid+1,r,pos)+t[t[i].L].sum;
}
 
bool Balance(int k)   //Correct
{
    return a*size[k]+5>=max(size[c[k][0]],size[c[k][1]]);
}
 
void Rec(int k)      //Correct
{
    if(c[k][0])Rec(c[k][0]);
    id[++S_tot]=k;
    Rec_seg(root[k]);
    if(c[k][1])Rec(c[k][1]);
}
 
int Build_balace(int last,int l,int r)    //Correct
{
    if(l>r)return 0;
    int mid=(l+r)/2,now=id[mid];
    fa[now]=last;
    c[now][0]=Build_balace(now,l,mid-1);
    c[now][1]=Build_balace(now,mid+1,r);
    size[now]=size[c[now][0]]+size[c[now][1]]+1;
    for(int i=l;i<=r;i++)
        Insert(root[now],0,70000,v[id[i]],1);
    return now;
}
 
void Rebuild(int x)   //Correct
{
    int f=fa[x],son=(c[f][1]==x)?1:0;
    S_tot=0;Rec(x);
    int qwer=Build_balace(f,1,S_tot);
    c[f][son]=qwer;
    c[0][0]=c[0][1]=fa[0]=v[0]=root[0]=size[0]=0;
    if(x==Root)Root=qwer;
}
 
void My_insert(int &k,int last,int rank,int x)   //Correct proved
{
    if(!k)
    {
        k=++Tot;v[k]=x;size[k]=1;fa[k]=last;
        Insert(root[k],0,70000,x,1);return;
    }
    size[k]++;Insert(root[k],0,70000,x,1);
    if(size[c[k][0]]>=rank)
        My_insert(c[k][0],k,rank,x);
    else My_insert(c[k][1],k,rank-size[c[k][0]]-1,x);
    if(!Balance(k))sheep=k;
}
 
void Change(int k,int pos,int Now_val)   //Correct
{
    if(size[c[k][0]]+1==pos)
    {
        Last_val=v[k];v[k]=Now_val;
        Insert(root[k],0,70000,Last_val,-1);
        Insert(root[k],0,70000,Now_val,1);
        return;
    }
    else if(size[c[k][0]]>=pos)
        Change(c[k][0],pos,Now_val);
    else Change(c[k][1],pos-size[c[k][0]]-1,Now_val);
    Insert(root[k],0,70000,Last_val,-1);
    Insert(root[k],0,70000,Now_val,1);
}
 
int My_lessthan(int k,int pos,int val)   //Correct
{
    if(!k)return 0;
    if(size[c[k][0]]>=pos)
        return My_lessthan(c[k][0],pos,val);
    else if(size[c[k][0]]+1==pos)
        return (v[k]<=val?1:0)+Query_lessthan(root[c[k][0]],0,70000,val);
    else
        return (v[k]<=val?1:0)+Query_lessthan(root[c[k][0]],0,70000,val)+My_lessthan(c[k][1],pos-size[c[k][0]]-1,val);
}
 
int My_query(int l,int r,int rank)    //Incorrect  a few hours wasted...
{
    int L=0,R=70010;
    while(L


你可能感兴趣的:(树套树)