同一份代码,为什么在 LOJ 上能过,在你谷上就过不去?是算法的问题吗?/kk
程序条理和码风都还行,求各位大佬帮个忙!
下面是代码
/*
luogu P3380
*/
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) (x&-x)
#define mid (l+r>>1)
int n,a[100005],q;
int m,lsh[200005];
struct quer{
int a,b,c,d;
}qq[100005];
int c[200005];
int cnt,root[200005];
struct hjtree{
int l,r,v;
}t[200005<<5];
int nl=0,nr=0,lt[200005],rt[200005],tmpl[200005],tmpr[200005];
void lsh_()
{
sort(lsh+1,lsh+1+m);
m = unique(lsh+1,lsh+1+m)-lsh-1;
}
void update(int o)
{
t[o].v = t[t[o].l].v+t[t[o].r].v;
}
void modify(int &o,int l,int r,int x,int v)
{
if(!o)
o = ++cnt;
if(l==x && r==x)
{
t[o].v += v;
return;
}
if(x<=mid)
modify(t[o].l,l,mid,x,v);
else
modify(t[o].r,mid+1,r,x,v);
update(o);
}
void fix(int x,int v)
{
for(int i=x;i<=n;i+=lowbit(i))
modify(root[i],1,m,a[x],v);
}
int queryrank(int l,int r,int x)
{
if(l==x && r==x)
return 0;
if(x<=mid)
{
for(int i=1;i<=nl;i++)
lt[i] = t[lt[i]].l;
for(int i=1;i<=nr;i++)
rt[i] = t[rt[i]].l;
return queryrank(l,mid,x);
}
else
{
int sum=0;
for(int i=1;i<=nr;i++)
{
sum += t[t[rt[i]].l].v;
rt[i] = t[rt[i]].r;
}
for(int i=1;i<=nl;i++)
{
sum -= t[t[lt[i]].l].v;
lt[i] = t[lt[i]].r;
}
return sum+queryrank(mid+1,r,x);
}
}
int askrank(int l,int r,int v)
{
nl = nr = 0;
if(l>1)
for(int i=l-1;i>=1;i-=lowbit(i))
lt[++nl] = root[i];
for(int i=r;i>=1;i-=lowbit(i))
rt[++nr] = root[i];
return queryrank(1,m,lower_bound(lsh+1,lsh+1+m,v)-lsh)+1;
}
int querykth(int l,int r,int k)
{
if(l==r)
return l;
int sum=0;
for(int i=1;i<=nr;i++)
sum += t[t[rt[i]].l].v;
for(int i=1;i<=nl;i++)
sum -= t[t[lt[i]].l].v;
if(k<=sum)
{
for(int i=1;i<=nl;i++)
lt[i] = t[lt[i]].l;
for(int i=1;i<=nr;i++)
rt[i] = t[rt[i]].l;
return querykth(l,mid,k);
}
else
{
for(int i=1;i<=nl;i++)
lt[i] = t[lt[i]].r;
for(int i=1;i<=nr;i++)
rt[i] = t[rt[i]].r;
return querykth(mid+1,r,k-sum);
}
}
int askkth(int l,int r,int v)
{
nl = nr = 0;
for(int i=l-1;i>=1;i-=lowbit(i))
lt[++nl] = root[i];
for(int i=r;i>=1;i-=lowbit(i))
rt[++nr] = root[i];
return querykth(1,m,v);
}
int askpre(int l,int r,int v)
{
int k=askrank(l,r,v)-1;
if(k==0)
return 0;
return askkth(l,r,k);
}
int asknext(int l,int r,int v)
{
if(lower_bound(lsh+1,lsh+1+m,v)-lsh==m)
return m+1;
int k=askrank(l,r,v+1);
if(k==r-l+1+1)
return m+1;
return askkth(l,r,k);
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n&&scanf("%d",a+i);i++)
lsh[++m] = a[i];
for(int i=1;i<=q;i++)
{
scanf("%d%d%d",&qq[i].a,&qq[i].b,&qq[i].c);
if(qq[i].a!=3)
scanf("%d",&qq[i].d);
if(qq[i].a==3)
lsh[++m] = qq[i].c;
else if(qq[i].a==4 || qq[i].a==5)
lsh[++m] = qq[i].d;
}
lsh_();
for(int i=1;i<=n;i++)
{
a[i] = lower_bound(lsh+1,lsh+1+m,a[i])-lsh;
fix(i,1);
}
lsh[0] = -2147483647, lsh[m+1] = 2147483647;
for(int i=1;i<=q;i++)
if(qq[i].a==3)
{
fix(qq[i].b,-1);
a[qq[i].b] = lower_bound(lsh+1,lsh+1+m,qq[i].c)-lsh;
fix(qq[i].b,1);
}
else if(qq[i].a==1)
{
printf("%d\n",askrank(qq[i].b,qq[i].c,qq[i].d));
}
else if(qq[i].a==2)
{
printf("%d\n",lsh[askkth(qq[i].b,qq[i].c,qq[i].d)]);
}
else if(qq[i].a==4)
{
printf("%d\n",lsh[askpre(qq[i].b,qq[i].c,qq[i].d)]);
}
else if(qq[i].a==5)
{
printf("%d\n",lsh[asknext(qq[i].b,qq[i].c,qq[i].d)]);
}
return 0;
}
/*
9 7
1 2 3 4 5 6 7 8 9
3 7 2
1 7 7 2
2 7 7 1
5 7 7 2
1 7 8 2
4 7 8 8
5 7 8 2
*/