很模板的树链剖分题

注意什么时候用线段树上的标号,什么时候用点的标号。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector> using namespace std;
const int maxn = ;
int val_pre[maxn],val[maxn],siz[maxn],son[maxn],id[maxn],fa[maxn],top[maxn],dep[maxn];
int topw;
int M,N,P; vector<int> G[maxn]; void dfs_1(int u,int f,int d)
{
siz[u] = ;
fa[u] = f;
dep[u] = d;
son[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v == f) continue;
dfs_1(v,u,d+);
siz[u] += siz[v];
if(siz[son[u]] < siz[v])
son[u] = v;
}
} void dfs_2(int u,int tp)
{
top[u] = tp;
id[u] = ++topw;
if(son[u]) dfs_2(son[u],tp);
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v == fa[u]|| v==son[u]) continue;
dfs_2(v,v);
}
} void debug()
{
for(int i=;i<=N;i++)
{
printf("%d siz:%d son:%d dep:%d fa:%d ",i,siz[i],son[i],dep[i],fa[i]);
printf("top:%d id:%d\n",top[i],id[i]);
}
} ////////////////////////////////////////
//Segment Tree #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int add[maxn<<];
void push_down(int rt)
{
if(add[rt])
{
add[rt<<] += add[rt];
add[rt<<|] += add[rt];
add[rt] = ;
}
} void build(int l,int r,int rt)
{
add[rt] = ;
if(l == r)
{
add[rt] = val[r];
return ;
}
int m = (l+r)>>;
build(lson);
build(rson);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l && R>= r)
{
add[rt] += c;
return ;
}
push_down(rt);
int m = (l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
} int query(int x,int l,int r,int rt)
{
if(l == r)
{
return add[rt];
}
push_down(rt);
int m = (l+r)>>;
if(x <= m) query(x,lson);
else query(x,rson);
} void change(int u,int v,int add)
{
int fu = top[u],fv = top[v];
while(fu != fv)
{
//printf("fu:%d u:%d v:%d fv:%d \n",fu,u,v,fv);
if(dep[fu] < dep[fv])
{
swap(u,v);swap(fu,fv);
}
update(id[fu],id[u],add,,topw,);
u = fa[fu];
fu = top[u];
}
if(u == v)
{
update(id[u],id[v],add,,topw,);
return;
}
else{
if(dep[u] > dep[v]) swap(u,v);
update(id[u],id[v],add,,topw,);
return ;
}
} int main()
{
//freopen("input.in","r",stdin);
while(~scanf("%d%d%d",&N,&M,&P))
{
for(int i=;i<=N;i++) scanf("%d",&val_pre[i]);
for(int i=,a,b;i<=M;i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
topw = ;
dfs_1(,,);
dfs_2(,);
for(int i=;i<=N;i++)
{
val[id[i]] = val_pre[i];
}
build(,topw,);
//debug(); char op[];
int a,b,c; for(int i=;i<P;i++)
{
scanf("%s",op);
if(op[] == 'I')
{
scanf("%d%d%d",&a,&b,&c);
change(a,b,c);
}
else if(op[] == 'D')
{
scanf("%d%d%d",&a,&b,&c);
change(a,b,-c);
}
else if(op[] == 'Q')
{
scanf("%d",&a);
int ans = query(id[a],,topw,);
printf("%d\n",ans);
}
}
for(int i=;i<maxn;i++) G[i].clear();
}
}

HDU3966-Aragorn's Story-树链剖分-点权的更多相关文章

  1. HDU3669 Aragorn's Story 树链剖分 点权

    HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...

  2. hdu3966 Aragorn's Story 树链剖分

    题目传送门 题目大意: 有n个兵营形成一棵树,给出q次操作,每一次操作可以使两个兵营之间的所有兵营的人数增加或者减少同一个数目,每次查询输出某一个兵营的人数. 思路: 树链剖分模板题,讲一下树链剖分过 ...

  3. BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...

  4. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  5. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  6. POJ2763 Housewife Wind 树链剖分 边权

    POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...

  7. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  8. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  9. HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)

    题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...

随机推荐

  1. 程序员修仙之路- CXO让我做一个计算器!!

    菜菜呀,个税最近改革了,我得重新计算你的工资呀,我需要个计算器,你开发一个吧 CEO,CTO,CFO于一身的CXO X总,咱不会买一个吗? 菜菜 那不得花钱吗,一块钱也是钱呀··这个计算器支持加减乘除 ...

  2. LeetCode 595. Big Countries

    There is a table World +-----------------+------------+------------+--------------+---------------+ ...

  3. eclipse maven设置

    eclipse 4.4以上版本集成了maven,只需配置一下即可,如果你的eclipse 没有安装maven,可以参考这个文章.http://marketplace.eclipse.org/conte ...

  4. 几何学观止(Lie群部分)

    上承这个页面,这次把Lie群的部分写完了 几何学观止-微分几何部分(20181102).pdf 我觉得其他部分(尤其是代数几何部分)我目前没有把握写得令自己满意,总之希望在毕业前能写完吧. 这次调整了 ...

  5. javascript与php与python的函数写法区别与联系

    1.javascript函数写法种类: (一).第一种 function test(param){ return 111; } (二).第二种 var test = function(param){ ...

  6. ssm知识点总结

    项目名称:教育网—在线调查系统 项目总体流程图: 设计调查:调查-->包裹--->问题(增删改查) 1.调整包裹顺序 2.移动复制包裹 3.深度删除 创建调查流程分析: 主要生成surve ...

  7. windows 环境下 eclipse + maven + tomcat 的 hello world 创建和部署

    主要记录自己一个新手用 eclipse + maven + tomcat 搭建 hello world 的过程,以及遇到的问题.讲真都是自己通过百度和谷歌一步步搭建的项目,没问过高手,也没高手可问,由 ...

  8. 10 Comparisons with adjectvies and nouns

    1 比较级用来比较两个词条之间的关系,比较级是通过在形容词后加 er 或者在形容词之前加 more 构成. 它的反义句是通过在形容词前加 less 或者 not as构成. Perfume sales ...

  9. cmd远程连接oracle数据库

  10. mybatis源码分析(四)---------------代理对象的生成

    在mybatis两种开发方式这边文章中,我们提到了Mapper动态代理开发这种方式,现在抛出一个问题:通过sqlSession.getMapper(XXXMapper.class)来获取代理对象的过程 ...