[bzoj3720]Gty的妹子树
来源:互联网 发布:程序员离职交接文档 编辑:程序博客网 时间:2024/05/01 07:21
题目大意
一颗树,根节点为1,要求在线兹瓷添加一个结点、修改一个结点权值和询问子树内权值大于x的点的个数(x每次询问不同)
树上分块
我们可以树上分块,详见我博客中杂文下的根号算法题库
然后每个块维护降序,对于整一块在子树内的就可以二分,其余部分暴力。
#include<cstdio>#include<algorithm>#include<cmath>#include<ctime>#define fo(i,a,b) for(i=a;i<=b;i++)using namespace std;const int maxn=60000+10,maxc=10000;int h[maxn],next[maxn*2],go[maxn*2],h2[maxn],n2[maxn],g2[maxn];int belong[maxn],a[maxn],d[maxn],size[maxn],dl[maxn],ld[maxn];int b[maxn][200];int i,j,k,l,r,t,n,m,tot,top,cnt,t2,ans,c,wdc;void add(int x,int y){ go[++tot]=y; next[tot]=h[x]; h[x]=tot;}void add2(int x,int y){ g2[++t2]=y; n2[t2]=h2[x]; h2[x]=t2;}void insert(int x,int y){ b[x][++size[x]]=y; int i=size[x]; while (i>1&&a[y]>a[b[x][i-1]]){ swap(b[x][i-1],b[x][i]); i--; }}void dfs(int x,int y){ d[x]=d[y]+1; if (!y||size[belong[y]]==c){ belong[x]=++top; insert(top,x); if (y) add2(belong[y],top); } else belong[x]=belong[y],insert(belong[y],x); int t=h[x]; while (t){ if (go[t]!=y) dfs(go[t],x); t=next[t]; }}void find(int x,int y){ int l=0,r=size[x],mid; while (l<r){ mid=(l+r+1)/2; if (a[b[x][mid]]>y) l=mid;else r=mid-1; } ans+=l;}int main(){ freopen("3720.in","r",stdin);freopen("3720.out","w",stdout); scanf("%d",&n); c=floor(sqrt(n)+1); fo(i,1,n-1){ scanf("%d%d",&j,&k); add(j,k);add(k,j); } fo(i,1,n) scanf("%d",&a[i]); dfs(1,0); cnt=n; scanf("%d",&m); ans=0; while (m--){ scanf("%d",&t); if (t==0){ scanf("%d%d",&j,&k); j^=ans;k^=ans; ans=wdc=0; dl[l=r=1]=j; while (l<=r){ if (a[dl[l]]>k) ans++; t=h[dl[l]]; while (t){ if (d[go[t]]>d[dl[l]]&&belong[go[t]]==belong[dl[l]]){ dl[++r]=go[t]; } else if (d[go[t]]>d[dl[l]]) ld[++wdc]=belong[go[t]]; t=next[t]; } l++; } fo(i,1,wdc) dl[i]=ld[i]; l=1;r=wdc; while (l<=r){ find(dl[l],k); t=h2[dl[l]]; while (t){ dl[++r]=g2[t]; t=n2[t]; } l++; } printf("%d\n",ans); } else if (t==1){ scanf("%d%d",&j,&k); j^=ans;k^=ans; fo(i,1,size[belong[j]]) if (b[belong[j]][i]==j) break; if (k<a[j]){ while (i<size[belong[j]]&&k<a[b[belong[j]][i+1]]){ swap(b[belong[j]][i+1],b[belong[j]][i]); i++; } } else{ while (i>1&&k>a[b[belong[j]][i-1]]){ swap(b[belong[j]][i-1],b[belong[j]][i]); i--; } } a[j]=k; } else{ scanf("%d%d",&j,&k); j^=ans;k^=ans; a[++cnt]=k; d[cnt]=d[j]+1; add(j,cnt); if (size[belong[j]]<c){ belong[cnt]=belong[j]; insert(belong[j],cnt); } else{ belong[cnt]=++top; insert(top,cnt); add2(belong[j],top); } } }}
0 0
- BZOJ3720 Gty的妹子树
- bzoj3720: Gty的妹子树
- [bzoj3720]Gty的妹子树
- [bzoj3720]Gty的妹子树
- BZOJ3720: Gty的妹子树
- Bzoj3720:Gty的妹子树:树分块
- 【bzoj3720】Gty的妹子树 块状树
- BZOJ3720 Gty的妹子树Solution
- [bzoj3720]Gty的妹子树 解题报告
- [bzoj3720][非树分块算法]Gty的妹子树
- 3720: Gty的妹子树
- 3720: Gty的妹子树 块状树
- BZOJ 3720 gty的妹子树
- BZOJ 3720 Gty的妹子树
- bzoj 3720: Gty的妹子树
- [bzoj3744]Gty的妹子序列
- bzoj3744 Gty的妹子序列
- bzoj3744: Gty的妹子序列
- 用java实现输入日期后得到这个日期是这一年的第多少天
- 3月24日学习笔记(调用隐式Intent启动浏览器)
- 各种基本算法实现小结(四)—— 图及其遍历
- 操作系统读写文件涉及的存储部件
- 专题一 Problem Q
- [bzoj3720]Gty的妹子树
- 圆角效果实现,圆角效果对性能的影响及优化
- 采用C++的ACE库实现的一个通用的C/S架构通信程序(最终版)
- 编码规范
- org.springframework.beans.Beanutils copyProperties 原理
- H5 - canvas
- 解决tableview下滑隐藏HeaderInSection的问题
- x509和pkcs12以及pkcs7的关系和区别
- 树形选择框(jsp页面样板)