HDU5274 Dylans loves tree(树链剖分+异或)
来源:互联网 发布:最好的建筑设计软件 编辑:程序博客网 时间:2024/04/30 01:10
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5274
题意很简单,输入以后,查询的时候
0 x y,是把x点改成y。
1 x y,是查询[x,y]中,数字出现的次数是否都是偶数,出现奇数的数字<=1。
都是偶数的话,输出-1,有奇数的话,输出奇数的权值。
思路:这题也是一个裸题,但是这题的线段树维护的比较巧妙,因为一个数异或自己偶数次就是0,所以求一个区间异或和就行了。但是有个地方要注意,就是权值为0的话,异或自己,奇数次也是0。所以可以把所有的权值+1,输出的时候再-1就好了。如果+1的话,在修改权值的时候也不要忘记+1。
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <cctype>#include <string>#include <iostream>#include <vector>#include <map>#include <queue>#include <ctime>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef pair<int,int> pii;#define PB push_back#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define calm (l+r)>>1const int INF=(int)1e9+7;const int maxn=100010;struct EE{ int to,next; EE(){} EE(int to,int next):to(to),next(next){}}edge[maxn*2];int n,q,Ecnt,head[maxn],tot,val[maxn];int top[maxn],fa[maxn],num[maxn],son[maxn],id[maxn],rev[maxn],deep[maxn];inline void addedge(int a,int b){ edge[Ecnt]=EE(b,head[a]); head[a]=Ecnt++;}void dfs1(int s,int pre,int d){ fa[s]=pre;deep[s]=d;num[s]=1;son[s]=-1; for(int i=head[s];~i;i=edge[i].next){ int t=edge[i].to; if(t==pre)continue; dfs1(t,s,d+1);num[s]+=num[t]; if(son[s]==-1||num[t]>num[son[s]]){ son[s]=t; } }}void dfs2(int s,int rt){ top[s]=rt;id[s]=++tot;rev[tot]=s; if(son[s]==-1)return; dfs2(son[s],rt); for(int i=head[s];~i;i=edge[i].next){ int t=edge[i].to; if(t==fa[s]||t==son[s])continue; dfs2(t,t); }}//SegmentTreeint sum[maxn<<2];inline void pushup(int rt){ sum[rt]=sum[rt<<1]^sum[rt<<1|1];}void build(int l,int r,int rt){ if(l==r){ sum[rt]=val[rev[l]]; return; } int m=calm; build(lson);build(rson); pushup(rt);}int query(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R){ return sum[rt]; } int m=calm,ans=0; if(L<=m)ans^=query(L,R,lson); if(R>m)ans^=query(L,R,rson); return ans;}void update(int x,int v,int l,int r,int rt){ if(l==r){ sum[rt]=v; return; } int m=calm; if(x<=m)update(x,v,lson); else update(x,v,rson); pushup(rt);}int findsum(int x,int y){ int f1=top[x],f2=top[y],ans=0; while(f1!=f2){ if(deep[f1]<deep[f2]){ swap(f1,f2);swap(x,y); } ans^=query(id[f1],id[x],1,n,1); x=fa[f1];f1=top[x]; } if(deep[x]>deep[y])swap(x,y); ans^=query(id[x],id[y],1,n,1); return ans;}int main(){ //freopen("/home/xt/code/acm/input.txt","r",stdin); int T;scanf("%d",&T); while(T--){ scanf("%d%d",&n,&q); memset(head,-1,sizeof head); Ecnt=0;tot=0; for(int i=1;i<n;i++){ int a,b;scanf("%d%d",&a,&b); addedge(a,b);addedge(b,a); } for(int i=1;i<=n;i++){ scanf("%d",&val[i]); val[i]++; } dfs1(1,0,1);dfs2(1,1); build(1,n,1); while(q--){ int op;scanf("%d",&op); int a,b;scanf("%d%d",&a,&b); if(op==0){ update(id[a],b+1,1,n,1);//更新的也要+1 } else{ printf("%d\n",findsum(a,b)-1);//输出-1 } } } //printf("[Run in %.1fs]\n",(double)clock()/CLOCKS_PER_SEC); return 0;}
1 0
- HDU5274 Dylans loves tree(树链剖分+异或)
- hdu5274 - Dylans loves tree(树链剖分)
- hdu5274 Dylans loves tree
- HDU5274.Dylans loves tree
- HDU5274 Dylans loves tree(树链剖分线段树)
- hdu5274 Dylans loves tree LCA+线段树
- HDU5274 Dylans loves tree(树链剖分)很巧的点权更新
- HDU 5274 Dylans loves tree 树链剖分(水)
- HDOJ 5274 Dylans loves tree
- BestCoder #45 1003 Dylans loves tree
- 【线段树】 HDOJ 5274 Dylans loves tree
- hdu 5274 Dylans loves tree 树剖
- hdu 5274 Dylans loves tree && BestCoder Round #45
- HDU 5274 Dylans loves tree(LCA+dfs时间戳+成段更新 OR 树链剖分+单点更新)
- Dylans loves numbers
- Dylans loves sequence
- HDU5273-Dylans loves sequence
- HDU5273--Dylans loves sequence
- SPOJ Number of Palindromes(回文树)
- ipyhon 与 ipython notebook
- c语言内嵌汇编语法
- java反射机制详解 及 Method.invoke解释 getMethod
- 后缀数组学习
- HDU5274 Dylans loves tree(树链剖分+异或)
- centos6.7 搭建 redis集群
- Linux驱动LCD driver学习总结
- linux命令大全(4)
- cmd命令中adb报错error: cannot parse version string: kg01
- JSON与fastjson
- JavaScript中this的理解
- 【BestCoder Round #81 (div.2)】HDU5670Machine
- (二)认识标签(1)