HDU5274
来源:互联网 发布:sql的delete语句 编辑:程序博客网 时间:2024/05/02 06:45
//我是无脑树链剖分的。。。过了快一个月了,终于靠自己把这道题A了..真是不容易呀。。。
//无脑树链剖分加线段树
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<string>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")const int maxn=100000+10;int nex[2*maxn];int to[2*maxn];int head[maxn];int val[maxn];int idx;int tot;void init(){ tot=0; idx=0; memset(head,-1,sizeof(head));}void addedge(int u,int v){ to[tot]=v; nex[tot]=head[u]; head[u]=tot++;}int siz[maxn];int dep[maxn];int fa[maxn];int son[maxn];void dfs1(int u,int d){ dep[u]=d; siz[u]=1; son[u]=0; for(int i=head[u];i!=-1;i=nex[i]) { int v=to[i]; if(v==fa[u]) continue; fa[v]=u; dfs1(v,d+1); siz[u]+=siz[v]; if(siz[son[u]]<siz[v]) son[u]=v; }}int id[maxn];int top[maxn];void dfs2(int u,int tp){ top[u]=tp; id[u]=++idx; if(son[u]) dfs2(son[u],tp); for(int i=head[u];i!=-1;i=nex[i]) { int v=to[i]; if(v==fa[u]||v==son[u]) continue; dfs2(v,v); }}struct tree{ int l; int r; int maxx;}node[4*maxn];void build(int x,int l,int r){ node[x].l=l; node[x].r=r; if(l==r) { node[x].maxx=val[l]; return ; } int mid=(l+r)>>1; build(x<<1,l,mid); build(x<<1|1,mid+1,r); node[x].maxx=node[x<<1].maxx^node[x<<1|1].maxx;}void update(int x,int k,int value){ if(node[x].l==node[x].r) { node[x].maxx=value; return ; } int mid=(node[x].l+node[x].r)>>1; if(k<=mid) update(x<<1,k,value); else update(x<<1|1,k,value); node[x].maxx=node[x<<1].maxx^node[x<<1|1].maxx;}int querry(int x,int l,int r){ if(node[x].l>=l&&node[x].r<=r) { return node[x].maxx; } int ans=0; int mid=(node[x].l+node[x].r)>>1; if(l<=mid) ans^=querry(x<<1,l,r); if(r>=mid+1) ans^=querry(x<<1|1,l,r); return ans;}void gao(int u,int v){ int top1=top[u]; int top2=top[v]; int ans=0; while(top1!=top2) { if(dep[top1]<dep[top2]) { swap(top1,top2); swap(u,v); } ans^=querry(1,id[top1],id[u]); u=fa[top1]; top1=top[u]; } if(dep[u]>dep[v]) swap(u,v); ans^=querry(1,id[u],id[v]); if(ans==0) printf("-1\n"); else printf("%d\n",ans-1);}int main(){ int t; scanf("%d",&t); while(t--) { init(); int n,m; scanf("%d%d",&n,&m); for(int i=1;i<n;i++) { int a,b; scanf("%d%d",&a,&b); addedge(a,b); addedge(b,a); } dfs1(1,1); dfs2(1,1); for(int i=1;i<=n;i++) { int a; scanf("%d",&a); val[id[i]]=a+1; } build(1,1,idx); for(int i=1;i<=m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(a==0) { update(1,id[b],c+1); } if(a==1) { gao(b,c); } } } return 0;}
0 0
- HDU5274
- hdu5274 Dylans loves tree
- HDU5274.Dylans loves tree
- hdu5274 - Dylans loves tree(树链剖分)
- hdu5274 Dylans loves tree LCA+线段树
- HDU5274 Dylans loves tree(树链剖分线段树)
- HDU5274 Dylans loves tree(树链剖分+异或)
- HDU5274 Dylans loves tree(树链剖分)很巧的点权更新
- 【爱笑话7.0版】笑话两万篇,免费阅读,绝无广告
- 测试
- 黑马程序员———反射机制
- Why Reflection is slowly?(Trail: The Reflection API)
- [LeetCode] 01矩阵中最大正方形 Maximal Square
- HDU5274
- LeetCode Fraction to Recurring Decimal
- Product of Array Except Self
- DT大数据梦工厂 温故而知新 之18讲
- Havok_2014-1-0_Pc_Xs_User_Guide(1.1.2-基础系统)
- C语言的格式化输入函数scanf
- HttpPost 中文乱码。
- Android中的Selector的用法
- 8.3知识总结