BZOJ4538 HNOI2016网络(树链剖分+线段树+堆/整体二分+树上差分)

时间:2022-06-01 15:08:01

  某两个点间的请求只对不在这条路径上的询问有影响。那么容易想到每次修改除该路径上的所有点的答案。对每个点建个两个堆,其中一个用来删除,线段树维护即可。由于一条路径在树剖后的dfs序中是log个区间,所以其补集也是log个区间。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 100010
int n,m,p[N],t,cnt;
int id[N],tag[N],top[N],fa[N],deep[N],size[N],son[N];
int L[N<<],R[N<<];
struct data{int to,nxt;
}edge[N<<];
struct data2{int x,y,z;
}q[N<<];
struct data3
{
int l,r;
bool operator <(const data3&a) const
{
return r<a.r;
}
}a[N];
struct heap
{
priority_queue<int> a,b;
void check(){while (!b.empty()&&!a.empty()&&a.top()==b.top()) a.pop(),b.pop();}
void ins(int x){a.push(x);check();}
void del(int x){b.push(x);check();}
}tree[N<<];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
void dfs1(int k)
{
size[k]=;
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=fa[k])
{
deep[edge[i].to]=deep[k]+;
fa[edge[i].to]=k;
dfs1(edge[i].to);
size[k]+=size[edge[i].to];
if (size[son[k]]<size[edge[i].to]) son[k]=edge[i].to;
}
}
void dfs2(int k,int from)
{
top[k]=from;
id[k]=++cnt;tag[cnt]=k;
if (son[k]) dfs2(son[k],from);
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=fa[k]&&edge[i].to!=son[k])
dfs2(edge[i].to,edge[i].to);
}
void build(int k,int l,int r)
{
L[k]=l,R[k]=r;tree[k].ins(-);
if (l==r) return;
int mid=l+r>>;
build(k<<,l,mid);
build(k<<|,mid+,r);
}
void down(int k)
{
while (!tree[k].a.empty())
{
int x=tree[k].a.top();tree[k].a.pop();
tree[k<<].ins(x);tree[k<<|].ins(x);
tree[k].check();
}
while (!tree[k].b.empty())
{
int x=tree[k].b.top();tree[k].b.pop();
tree[k<<].del(x);tree[k<<|].del(x);
}
}
void ins(int k,int l,int r,int x)
{
if (L[k]==l&&R[k]==r) {tree[k].ins(x);return;}
down(k);
int mid=L[k]+R[k]>>;
if (r<=mid) ins(k<<,l,r,x);
else if (l>mid) ins(k<<|,l,r,x);
else ins(k<<,l,mid,x),ins(k<<|,mid+,r,x);
}
void del(int k,int l,int r,int x)
{
if (L[k]==l&&R[k]==r) {tree[k].del(x);return;}
down(k);
int mid=L[k]+R[k]>>;
if (r<=mid) del(k<<,l,r,x);
else if (l>mid) del(k<<|,l,r,x);
else del(k<<,l,mid,x),del(k<<|,mid+,r,x);
}
int query(int k,int x)
{
if (L[k]==R[k]) return tree[k].a.top();
down(k);
int mid=L[k]+R[k]>>;
if (x<=mid) return query(k<<,x);
else return query(k<<|,x);
}
void modify(int x,int y,int z)
{
int m=;
while (top[x]!=top[y])
{
if (deep[top[x]]<deep[top[y]]) swap(x,y);
m++,a[m].l=id[top[x]],a[m].r=id[x];
x=fa[top[x]];
}
if (deep[x]<deep[y]) swap(x,y);
m++,a[m].l=id[y],a[m].r=id[x];
sort(a+,a+m+);
a[].l=a[].r=,a[m+].l=a[m+].r=n+;
for (int i=;i<=m;i++)
if (a[i].r+<=a[i+].l-)
if (z>) ins(,a[i].r+,a[i+].l-,z);
else del(,a[i].r+,a[i+].l-,-z);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj4538.in","r",stdin);
freopen("bzoj4538.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read();
for (int i=;i<n;i++)
{
int x=read(),y=read();
addedge(x,y),addedge(y,x);
}
dfs1();
dfs2(,);
build(,,n);
for (int i=;i<=m;i++)
{
int op=read();
switch(op)
{
case :
{
q[i].x=read(),q[i].y=read(),q[i].z=read();
modify(q[i].x,q[i].y,q[i].z);
break;
}
case :
{
int x=read();
modify(q[x].x,q[x].y,-q[x].z);
break;
}
case :
{
int x=read();
printf("%d\n",query(,id[x]));
}
}
}
return ;
}

  然而由于复杂度是O(nlog3n)的以及蒟蒻自带大常数,在luogu上T了两个点,bzoj甚至直接MLE了。于是考虑有没有更好的做法。

  感觉上修改时对于某一条路径之外的都要修改过于暴力,考虑是否能改成修改这条路径。如果只有一次询问,可以二分答案,统计出有多少个修改的权值不小于该答案,并统计上述种类的修改覆盖了查询点多少次,若两者相等则说明答案不可行,否则可行。修改路径可以通过树上差分维护子树和完成。

  现在有多组询问,可以想到整体二分。对整体二分一个答案,按时间顺序依次修改、查询,每次做完之后都缩小了每一个询问的答案范围,将询问按照答案是否大于mid、修改按照权值是否大于mid扔在两边即可。于是复杂度O(nlog2n)。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 100010
#define inf 1000000000
int n,m,p[N],t,tree[N],id[N],size[N],fa[N][],deep[N],cnt;
struct data{int to,nxt;
}edge[N<<];
struct data2{int op,x,y,z,l,i,ans;
}q[N<<],tmp[N<<];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
bool cmp(const data2&a,const data2&b)
{
return a.i<b.i;
}
void dfs(int k,int from)
{
id[k]=++cnt;size[k]=;
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=from)
{
deep[edge[i].to]=deep[k]+;
fa[edge[i].to][]=k;
dfs(edge[i].to,k);
size[k]+=size[edge[i].to];
}
}
int lca(int x,int y)
{
if (deep[x]<deep[y]) swap(x,y);
for (int j=;~j;j--) if (deep[fa[x][j]]>=deep[y]) x=fa[x][j];
if (x==y) return x;
for (int j=;~j;j--) if (fa[x][j]!=fa[y][j]) x=fa[x][j],y=fa[y][j];
return fa[x][];
}
void add(int k,int x){while (k<=n){tree[k]+=x;k+=k&-k;}}
int sum(int k){int s=;while (k){s+=tree[k];k-=k&-k;}return s;}
void solve(int l,int r,int low,int high)
{
if (l>r) return;
if (low==high)
{
for (int i=l;i<=r;i++)
if (q[i].op==) q[i].ans=low;
return;
}
int mid=low+high>>,s=;
for (int i=l;i<=r;i++)
if (q[i].op!=)
{
if (q[i].z>mid)
{
int t=q[i].op==?:-;
s+=t;
add(id[q[i].x],t),add(id[q[i].y],t);
add(id[q[i].l],-t);
if (q[i].l>) add(id[fa[q[i].l][]],-t);
}
}
else if (sum(id[q[i].x]+size[q[i].x]-)-sum(id[q[i].x]-)==s) q[i].ans=low;
else q[i].ans=high;
int cut=l;
for (int i=l;i<=r;i++)
if (q[i].op==&&q[i].ans<=mid||q[i].op!=&&q[i].z<=mid) tmp[cut++]=q[i];
int t=cut;
for (int i=l;i<=r;i++)
if (q[i].op==&&q[i].ans>mid||q[i].op!=&&q[i].z>mid) tmp[t++]=q[i];
for (int i=l;i<=r;i++) q[i]=tmp[i];
for (int i=cut;i<=r;i++)
if (q[i].op!=)
{
int t=q[i].op==?-:;
add(id[q[i].x],t),add(id[q[i].y],t);
add(id[q[i].l],-t);
if (q[i].l>) add(id[fa[q[i].l][]],-t);
}
solve(l,cut-,low,mid);
solve(cut,r,mid+,high);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj4538.in","r",stdin);
freopen("bzoj4538.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read();
for (int i=;i<n;i++)
{
int x=read(),y=read();
addedge(x,y),addedge(y,x);
}
fa[][]=;dfs(,);
for (int j=;j<;j++)
for (int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
for (int i=;i<=m;i++)
{
q[i].op=read();q[i].i=i;
switch(q[i].op)
{
case :
{
q[i].x=read(),q[i].y=read(),q[i].z=read(),q[i].l=lca(q[i].x,q[i].y);
break;
}
case :
{
int x=read();
q[i].x=q[x].x,q[i].y=q[x].y,q[i].z=q[x].z,q[i].l=q[x].l;
break;
}
case :q[i].x=read(),q[i].ans=-;
}
}
solve(,m,-,inf);
sort(q+,q+m+,cmp);
for (int i=;i<=m;i++)
if (q[i].op==) printf("%d\n",q[i].ans);
return ;
}