[Luogu] trip
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的更多相关文章
- 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 ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- dp or 贪心 --- hdu : Road Trip
Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...
- 【poj1041】 John's trip
http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...
- 1301. The Trip
A number of students are members of a club that travels annually to exotic locations. Their destinat ...
- 三分 --- POJ 3301 Texas Trip
Texas Trip Problem's Link: http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)
Problem C: The Trip Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 19 Solved: 3[Submit][Status][Web ...
- hdu 3018 Ant Trip 欧拉回路+并查集
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
随机推荐
- vue—组件基础了解
什么是组件? 组件是vue中的一个可复用实例,所以new Vue()是vue中最大的那个组件,根组件,有名字,使用的时候以单标签或双标签使用 vm = newVue() 是最大的组件,具有很多实用性的 ...
- java项目上线的流程(将web项目部署到公网)
本博文来源于网络,原文的地址在本篇博文最下方. 如何将java web项目上线/部署到公网 关于如何将Java Web上线,部署到公网,让全世界的人都可以访问的问题.小编将作出系列化,完整的流程介绍. ...
- Spring IOC Container
All the notes are from Spring Framework 5 Doc. 一.Introduction to the Spring IOC Container and Beans ...
- 刚接触neo4j 问下 neo4j 生成的节点图形可以发布为纯网页方式么
6 回复 pangguoming 1楼•3 年前 你是想要neo4j web控制端的可视化功能吗? 那是用D3.js 做的,你用前端用D3.js配合Java自己做 或者 去下载neo4j 的前端 开源 ...
- 怎样理解xhr.overrideMimeType()和xhr.responseType
一般情况下, 我们会通过 xhr.responseType 告诉服务器我们想要什么类型的返回数据. 然后xhr.response 会根据xhr.responseType属性值来自动解析返回值. 但有时 ...
- Redis 的订阅发布(PUB/SUB)示例
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1"); ISubscriber sub ...
- JS 控制特殊字符
1.标签上直接替换方法: JS 控制不能输入特殊字符 1 <input type="text"class="domain"onkeyup="th ...
- JS基础_实参可以是任何值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- caffe笔记
1. 训练 cifar10 示例 ① cd caffe.1.0.0 ./data/cifar10/get_cifar10.sh #获取图片 ② ./examples/cifar10/cre ...
- 【转】Fetch超时设置和终止请求
原文链接:https://www.cnblogs.com/yfrs/p/fetch.html 1.基本使用 Fetch 是一个新的端获取资源的接口,用于替换笨重繁琐XMLHttpRequest.它有了 ...