https://www.luogu.org/problemnew/show/T28848#sub

#include <iostream>
#include <cstdio> using namespace std;
const int N = 1e5 + ; int top[N], fa[N], deep[N], size[N], son[N], tree[N], bef[N];
int head[N], W[N << ], F[N << ], Size[N << ];
int n, Ty, tim, now = ;
struct Node{int v, nxt;} G[N << ];
int Answer; #define gc getchar() inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline void Add(int u, int v) {G[now].v = v; G[now].nxt = head[u]; head[u] = now ++;} void Dfs_1(int u, int dep, int f_) {
deep[u] = dep; fa[u] = f_;
size[u] = ;
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != f_) {
Dfs_1(v, dep + , u);
size[u] += size[v];
if(size[v] > size[son[u]]) son[u] = v;
}
}
} void Dfs_2(int u, int tp) {
top[u] = tp;
tree[u] = ++ tim;
if(!son[u]) return ;
Dfs_2(son[u], tp);
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != fa[u] && v != son[u]) Dfs_2(v, v);
}
} #define lson jd << 1
#define rson jd << 1 | 1 void Build_tree(int l, int r, int jd) {
Size[jd] = r - l + ;
if(l == r) {
W[jd] = ;
return ;
}
int mid = (l + r) >> ;
Build_tree(l, mid, lson);
Build_tree(mid + , r, rson);
W[jd] = W[lson] + W[rson];
} void Down(int jd) {
int f = F[jd];
W[lson] += Size[lson] * f; W[rson] += Size[rson] * f;
F[lson] += f; F[rson] += f;
F[jd] = ;
} void Sec_G(int l, int r, int jd, int x, int y, int yj) {
if(x <= l && r <= y) {W[jd] += (r - l + ) * yj; F[jd] += yj; return ;}
if(F[jd]) Down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y, yj);
if(y > mid) Sec_G(mid + , r, rson, x, y, yj);
W[jd] = W[lson] + W[rson];
} inline void Sec_G_imp(int x, int y) {
int tp1 = top[x], tp2 = top[y];
while(tp1 != tp2) {
if(deep[tp1] < deep[tp2]) swap(x, y), swap(tp1, tp2);
Sec_G(, n, , tree[tp1], tree[x], -);
x = fa[tp1];
tp1 = top[x];
}
if(x == y) return ;
if(deep[x] < deep[y]) swap(x, y);
Sec_G(, n, , tree[y] + , tree[x], -);
} void Sec_A(int l, int r, int jd, int x, int y) {
if(x <= l && r <= y) {
Answer += W[jd]; return ;
}
if(F[jd]) Down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_A(l, mid, lson, x, y);
if(y > mid) Sec_A(mid + , r, rson, x, y);
} inline int Sec_A_imp(int x) {
int tp1 = top[x], ret = ;
while(tp1 != ) {
Answer = ;
Sec_A(, n, , tree[tp1], tree[x]);
ret += Answer;
x = fa[tp1];
tp1 = top[x];
}
if(x == ) return ret;
Answer = ;
Sec_A(, n, , tree[] + , tree[x]);
ret += Answer;
return ret;
} int main() {
n = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++) {
int u = read(), v = read();
Add(u, v); Add(v, u);
}
Dfs_1(, , );
Dfs_2(, );
Build_tree(, n, );
Ty = read();
Ty = Ty + n - ;
while(Ty --) {
string opt;
cin >> opt;
if(opt[] == 'A') {
int x = read(), y = read();
Sec_G_imp(x, y);
}
else {
int x = read();
cout << Sec_A_imp(x) << "\n";
}
} return ;
}
/*
5
1 2
1 3
1 4
4 5
4
W 5
A 1 4
W 5
A 4 5
W 5
W 2
A 1 2
A 1 3
*/

[Luogu] trip的更多相关文章

  1. Lesson 4 An existing trip

    Text I have just received a letter from my brother,Tim. He is in Australia. He has been there for si ...

  2. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  3. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  4. dp or 贪心 --- hdu : Road Trip

    Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...

  5. 【poj1041】 John's trip

    http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...

  6. 1301. The Trip

    A number of students are members of a club that travels annually to exotic locations. Their destinat ...

  7. 三分 --- POJ 3301 Texas Trip

    Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...

  8. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)

    Problem C: The Trip Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 19  Solved: 3[Submit][Status][Web ...

  9. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

随机推荐

  1. C++ 继承 - 在派生类中对基类初始化

    构造函数与基类的其他成员不同,不能被派生类继承,因此为了初始化基类中的成员变量,需要在派生类中调用基类的构造函数(即显式调用),如果派送类没有调用则默认调用基类的无参构造函数(即隐式调用). 显式调用 ...

  2. 超级简单的requests模块教程

    在web后台开发过程中,会遇到需要向第三方发送http请求的场景,python中的requests库可以很好的满足这一要求,这里简要记录一下requests模块的使用! 说明: 这里主要记录一下req ...

  3. memcached基本操作指令

    item执行命令: 第一行:Key Flags ExpirationTime BytesKey:Key 用于查找缓存值Flags:一个32位的标志值,客户机使用它存储关于键值对的额外信息Expirat ...

  4. MediaAPIController

    using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.D ...

  5. 如何爬取icourse163 中国慕课上课程信息(上),

    中国大学MOOC网上有着特别完善的课程信息,我觉得这是一份可以让我们充分利用的资源 那么,接下来的问题就是我们该如何爬取这里的资源 选择其中的计算机课程进行尝试 import requests fro ...

  6. 安装笔记, caffe 、 opencv等

    1. 1.1 opencv static linux mkdir build & cd build cmake .. -LH  这句话用来查看编译选项  如果不知道编译啥  可以用这个查看一下 ...

  7. mysql管理工具之pt

    之前我一直用Seconds_behind_master来衡量主从的延迟,今天看到文档,才觉得多么不可靠!以下是官方文档的描述: In essence, this field measures the ...

  8. String,StringBuffer,StringBuilder 的区别是什么

    Java中用于处理字符串常用的有三个类: 1.java.lang.String 2.java.lang.StringBuffer 3.java.lang.StrungBuilder 一.Java St ...

  9. maskrcnn-benchmark错误:ImportError: cannot import name rnn_compat

    错误: from apex import amp File "build/bdist.linux-x86_64/egg/apex/__init__.py", line 5, in ...

  10. 【异常】hbase启动后hdfs文件权限目录不一致,导致Phoenix无法删除表结构

    1 异常信息 Received error when attempting to archive files ([class org.apache.hadoop.hbase.backup.HFileA ...