lca的做法还是非常明显的。简单粗暴,

只是不是正解。假设树是长链就会跪,直接变成O(n)、、

最后跑的也挺快,出题人还是挺阳光的。。

动态树的解法也是听别人说能ac的。预计就是放在splay上剖分一下,做法还是比較复杂的。,,

来一发lca:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=20010;
int head[maxn],tol,dp[maxn],fa[maxn][20],dep[maxn],weight[maxn];
struct Edge{
int next,to;
Edge(int _next=0,int _to=0){
next=_next;to=_to;
}
}edge[10*maxn];
void addedge(int u,int v){
edge[tol]=Edge(head[u],v);
head[u]=tol++;
}
void bfs(int s){
queue<int> q;
dep[s]=0,fa[s][0]=s;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=1;i<20;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa[u][0])continue;
fa[v][0]=u;
dep[v]=dep[u]+1;
q.push(v);
}
}
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=19;i>=0;i--)if((1<<i)&(dep[x]-dep[y]))x=fa[x][i];
if(x==y)return x;
for(int i=19;i>=0;i--)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
void dfs(int u,int pre){
dp[u]=weight[u];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==pre)continue;
dfs(v,u);
dp[u]+=dp[v];
}
}
int move(int x,int d){
for(int i=19;i>=0;i--)
if(d&(1<<i))x=fa[x][i];
return x;
}
int main()
{
int T;
cin>>T;
for(int t=1;t<=T;t++){
int n;
scanf("%d",&n);
memset(head,-1,sizeof(head));tol=0;
for(int i=1;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
bfs(1);
for(int i=1;i<=n;i++)scanf("%d",&weight[i]);
dfs(1,-1);
printf("Case #%d:\n",t);
int Q;
scanf("%d",&Q);
int root=1;
while(Q--){
char op[10];
int x,y;
scanf("%s",op);
if(op[0]=='R'){
scanf("%d",&x);
root=x;
}
else if(op[0]=='C'){
scanf("%d%d",&x,&y);
int dd=x;
while(1){
dp[dd]+=y-weight[x];
if(dd==1)break;
dd=fa[dd][0];
}
weight[x] = y;
}
else {
scanf("%d",&x);
if(x==root)printf("%d\n",dp[1]);
else {
int lca=LCA(x,root);
if(lca==x){
int p=move(root,dep[root]-dep[x]-1);
//cout<<"han "<<x<<" "<<root<<" "<<dep[root]-dep[x]-1<<endl;cout<<"p="<<p<<endl;
printf("%d\n",dp[1]-dp[p]);
}
else printf("%d\n",dp[x]);
}
}
}
}
return 0;
}

正解应该是树状数组维护欧拉序列,,

bit的神牛教的。,

详见:点击打开链接

HDU 4836 The Query on the Tree lca || 欧拉序列 || 动态树的更多相关文章

  1. hdu 4836 The Query on the Tree(线段树or树状数组)

    The Query on the Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. lca 欧拉序+rmq(st) 欧拉序+rmq(线段树) 离线dfs 倍增

    https://www.luogu.org/problemnew/show/P3379 1.欧拉序+rmq(st) /* 在这里,对于一个数,选择最左边的 选择任意一个都可以,[left_index, ...

  3. HDU 2814 斐波那契循环节 欧拉降幂

    一看就是欧拉降幂,问题是怎么求$fib(a^b)$,C给的那么小显然还是要找循环节.数据范围出的很那啥..unsigned long long注意用防爆的乘法 /** @Date : 2017-09- ...

  4. HDU 2586(LCA欧拉序和st表)

    什么是欧拉序,可以去这个大佬的博客(https://www.cnblogs.com/stxy-ferryman/p/7741970.html)巨详细 因为欧拉序中的两点之间,就是两点遍历的过程,所以只 ...

  5. HDU 4676 Sum Of Gcd 【莫队 + 欧拉】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=4676 Sum Of Gcd Time Limit: 10000/5000 MS (Java/Others ...

  6. Apple Tree POJ - 3321 dfs序列构造树状数组(好题)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

  7. 【HDU 6191】Query on A Tree 【可持久化字典树】

    题目 给出一棵有n个结点的树,树根是1,每个结点给出一个value.然后给出q个询问,每个询问给出两个整数u和x,你要在以u结点为根的子树中找出一个结点v,使得val[v] xor x最大, 并输出这 ...

  8. HDU 1116 Play on Words(有向欧拉判断)

    题目链接 题意:给出一些单词,问全部单词能否首尾相连 直接 将每一个单词第一个和最后一个字母建立一条有向边,保证除了首尾两个出入度不相等,其他的要保证相等.还有一个条件就是 首尾两个出入度差为1 同时 ...

  9. hdu 1286:找新朋友(数论,欧拉函数)

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 解决servlet乱码问题

    1) request中的中文乱码 a) POST方式提交 在获得提交表单信息之前调用request.setCharactersEncoding("UTF-8"); b) GET方式 ...

  2. poj 3176 Cow Bowling(区间dp)

    题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...

  3. 第四种:GCD

    GCD 1> 概述 Grand Central Dispatch (GCD)是Apple开发的一种多核编程技术.主要用于优化应用程序以支持多核处理器以及其他对称多处理系统. GCD提供函数实现多 ...

  4. PHPCMS V9.3.2用户注册模板中的一个低级Bug

    当我们下载了目前最新的PHPCMS的时候,如果我们修改了用户注册的模板,那么模板缓存就会更新,这时候就会如下 的问题: Parse error: syntax error, unexpected T_ ...

  5. [Swust OJ 610]--吉祥数

    题目链接:http://acm.swust.edu.cn/problem/610/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  6. Python实现 zip解压缩到指定目录

    #!/bin/env python #-*- coding:utf-8 -*- import zipfile,os import platform,sys,os from zipfile import ...

  7. Java 重入锁 ReentrantLock

    本篇博客是转过来的. 但是略有改动感谢 http://my.oschina.net/noahxiao/blog/101558 摘要 从使用场景的角度出发来介绍对ReentrantLock的使用,相对来 ...

  8. C语言深度剖析--volatile(转载)

    volatile关键字和const一样是一种类型修饰符,用它修饰的变量表示可以被某些编译器未知的因素更改,比如操作系统,硬件或者其他线程等等.遇到这个关键字声明的变量,编译器对访问该变量的代码就不再进 ...

  9. Codeforces Round #253 (Div. 2), problem: (B)【字符串匹配】

    简易字符串匹配,题意不难 #include <stdio.h> #include <string.h> #include <math.h> #include < ...

  10. 模拟Struts2的AOP实现

    在Struts2中有拦截器的概念,通过它的拦截器可以拦截Action.Struts2的拦截器是通过AOP来实现的,在Spring也有类似的概念.下面的我们先来比较一下Struts2和Spring中AO ...