/**
problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966
裸板
**/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
using namespace std; const int MAXN = ; template <typename T>
class SegmentTree {
private:
struct Node {
int left, right;
T sum, lazy;
} node[MAXN << ];
T data[MAXN];
void pushUp(int root) {
node[root].sum = node[root << ].sum + node[root << | ].sum;
}
void pushDown(int root) {
if(node[root].left == node[root].right) return;
int lson = root << ;
int rson = root << | ;
node[lson].sum += node[root].lazy * (node[lson].right - node[lson].left + );
node[rson].sum += node[root].lazy * (node[rson].right - node[rson].left + );
node[lson].lazy += node[root].lazy;
node[rson].lazy += node[root].lazy;
node[root].lazy = ;
}
public:
void build(int left, int right, int root = ) {
node[root].left = left;
node[root].right = right;
node[root].lazy = ;
if(left == right) {
node[root].sum = data[left];
} else {
int mid = (left + right) >> ;
build(left, mid, root << );
build(mid + , right, root << | );
pushUp(root);
}
}
void update(int left, int right, T value, int root = ) {
int lson = root << ;
int rson = root << | ;
node[root].sum += value * (right - left + );
if(node[root].left == left && node[root].right == right) {
node[root].lazy += value;
return ;
}
if(left >= node[rson].left) {
update(left, right, value, rson);
} else if(right <= node[lson].right) {
update(left, right, value, lson);
} else {
update(left, node[lson].right, value, lson);
update(node[rson].left, right, value, rson);
}
}
T query(int left, int right, int root = ) {
int lson = root << ;
int rson = root << | ;
if(node[root].lazy) pushDown(root);
if(node[root].left == left && node[root].right == right) {
return node[root].sum;
}
if(left >= node[rson].left) {
return query(left, right, rson);
} else if(right <= node[lson].right) {
return query(left, right, lson);
} else {
return query(left, node[lson].right, lson) + query(node[rson].left, right, rson);
}
}
void clear(int n, const vector<int> &d) {
for(int i = ; i <= n; i ++) {
this->data[i] = d[i];
}
build(, n);
}
}; template <typename T>
class TreeToLink {
private:
struct Point {
int size, son, depth, father, top, newId;
T data;
} point[MAXN];
struct Edge {
int to, next;
} edge[MAXN << ];
int oldId[MAXN], first[MAXN], sign, sumOfPoint, cnt;
SegmentTree<T> st;
void dfs1(int u, int father = , int depth = ) {
point[u].depth = depth;
point[u].father = father;
point[u].size = ;
int maxson = -;
for(int i = first[u]; i != -; i = edge[i].next) {
int to = edge[i].to;
if(to == father) continue;
dfs1(to, u, depth + );
point[u].size += point[to].size;
if(point[to].size > maxson) {
point[u].son = to;
maxson = point[to].size;
}
}
}
void dfs2(int u, int top) {
point[u].newId = ++cnt;
oldId[cnt] = u;
point[u].top = top;
if(point[u].son == -) {
return ;
}
dfs2(point[u].son, top);
for(int i = first[u]; i != -; i = edge[i].next) {
int to = edge[i].to;
if(to == point[u].son || to == point[u].father) continue;
dfs2(to, to);
}
}
public:
void clear(int n) {
sumOfPoint = n;
sign = ;
cnt = ;
for(int i = ; i <= n; i ++) {
first[i] = -;
point[i].son = -;
scanf("%d", &point[i].data);
}
}
void addEdgeOneWay(int u, int v) {
edge[sign].to = v;
edge[sign].next = first[u];
first[u] = sign ++;
}
void addEdgeTwoWay(int u, int v) {
addEdgeOneWay(u, v);
addEdgeOneWay(v, u);
}
void preWork(int x = ) {
dfs1(x);
dfs2(x, x);
vector<int> data(sumOfPoint + );
for(int i = ; i <= sumOfPoint; i ++) {
data[i] = point[oldId[i]].data;
}
st.clear(sumOfPoint, data);
}
void updatePath(int x, int y, T z){
while(point[x].top != point[y].top){
if(point[point[x].top].depth < point[point[y].top].depth)
swap(x, y);
st.update(point[point[x].top].newId, point[x].newId, z);
x = point[point[x].top].father;
}
if(point[x].depth > point[y].depth)
swap(x, y);
st.update(point[x].newId, point[y].newId, z);
}
T queryPath(int x, int y){
T ans = ;
while(point[x].top != point[y].top){
if(point[point[x].top].depth < point[point[y].top].depth)
swap(x, y);
ans += st.query(point[point[x].top].newId, point[x].newId);
x = point[point[x].top].father;
}
if(point[x].depth > point[y].depth)
swap(x, y);
ans += st.query(point[x].newId, point[y].newId);
return ans;
}
void updateSon(int x, T z){
st.update(point[x].newId, point[x].newId + point[x].size - , z);
}
T querySon(int x){
return st.query(point[x].newId, point[x].newId + point[x].size - );
}
T queryPoint(int x) {
return queryPath(x, x);
}
void updatePoint(int x, T z) {
updatePath(x, x, z);
}
}; class Solution {
private:
int n, m, p;
TreeToLink<int> ttl;
public:
void solve() {
while(~scanf("%d%d%d", &n, &m, &p)){
ttl.clear(n);
for(int i = , a, b; i < m; i ++) {
scanf("%d%d", &a, &b);
ttl.addEdgeTwoWay(a, b);
}
ttl.preWork();
while(p --) {
char opt;
int a, b, c;
scanf(" %c%d", &opt, &a);
if(opt == 'I') {
scanf("%d%d", &b, &c);
ttl.updatePath(a, b, c);
} else if(opt == 'D') {
scanf("%d%d", &b, &c);
ttl.updatePath(a, b, -c);
} else if(opt == 'Q') {
printf("%d\n", ttl.queryPoint(a));
}
}
}
}
} DarkScoCu; int main() {
DarkScoCu.solve();
return ;
}

hdu 3966 Aragorn's Story : 树链剖分 O(nlogn)建树 O((logn)²)修改与查询的更多相关文章

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

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

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

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

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

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

  4. HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问

    Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord ...

  5. HDU 3966 Aragorn's Story 树链剖分

    Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题注意要手动扩栈. 这题我交g++无限RE,即使手动扩栈了,但交C++就过了. #pragm ...

  6. hdu 3966 Aragorn's Story 树链剖分 按点

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

  7. HDU 3966 Aragorn's Story (树链剖分入门题)

    树上路径区间更新,单点查询. 线段树和树状数组都可以用于本题的维护. 线段树: #include<cstdio> #include<iostream> #include< ...

  8. HDU 3966 Aragorn's Story (树链点权剖分,成段修改单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...

  9. HDU 3966 Aragorn's Story 树链拋分

    一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...

随机推荐

  1. 清除canvas画布内容--点擦除+线擦除

    清空canvas画布内容 1.重置宽或高 由于canvas每当高度或宽度被重设时,画布内容就会被清空,因此可以用以下方法清空:(此方法仅限需要清除全部内容的情况) var c=document.get ...

  2. Android 自定义圆形图表

    <com...SignChartView android:id="@+id/signChart" android:layout_width="265dp" ...

  3. ubuntu 18 环境下使用 @vue-cli 3.2 新建 vue 项目

    ubuntu 18 环境下使用 @vue-cli 3.2 新建 vue 项目 标签(空格分隔): Vue 首先安装全局@vue-cli工具: npm install -g @vue/cli 然后创建项 ...

  4. Linux --防火墙(一)

    基本组成 表: filter:用来对数据包进行过滤,根据具体的规则要求决定如何处理一个数据包.表内包含三个链,即INOUT.FORWARD.OUTPUT nat表:主要用来修改数据包的IP地址.端口号 ...

  5. 转贴:天然VC的迷局

    天然VC的迷局作者:棱子 http://www.jkzgr.net/jiankangguanli/176.html 维生素C对人类来说是一种必不可少的维生素.我们可以通过正常饮食获取所需的VC.市场上 ...

  6. 长大DeepMind第一次团队作业

    1.队名 长大DeepMind 2.队员风采 学号 姓名 擅长的技术 编程的兴趣点 希望承担的角色 一句话宣言 B20150304508 晏司举 JAVA,ssm框架,MySQL数据库 JAVA后台服 ...

  7. flume-ng 自定义sink消费flume source

    如何从一个已经存在的Flume source消费数据 1.下载flume wget http://www.apache.org/dist/flume/stable/apache-flume-1.5.2 ...

  8. 【转载】#438 - Benefits of Using Interfaces

    You might wonder why you'd define an interface, have a class implement that interface and then acces ...

  9. 【洛谷5283】[十二省联考2019] 异或粽子(可持久化Trie树+堆)

    点此看题面 大致题意: 求前\(k\)大的区间异或和之和. 可持久化\(Trie\)树 之前做过一些可持久化\(Trie\)树题,结果说到底还是主席树. 终于,碰到一道真·可持久化\(Trie\)树的 ...

  10. 【[COCI2011-2012#5] POPLOCAVANJE】

    据说这道题卡空间? 不存在的,拿\(AC\)自动机去存\(5000\times5000\)的串肯定是要M的 我们可以考虑对长度为\(n\)的串建一个\(SAM\),这样空间就只需要两倍的\(3e5\) ...