[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 ...
随机推荐
- Python学习3——列表和元组
一.通用序列操作——索引.切片.相加.相乘.成员资格检查 1.索引,正序从0开始为第一个元素,逆序从-1开始,-1为最后一个元素 >>> greeting[0] 'h' >&g ...
- 第六章 ZYNQ-MIZ701 GPIO使用之MIO
6.0 本章难度系数★★☆☆☆☆☆ 6.1 GPIO简介 Zynq7000系列芯片有54个MIO(multiuse I/O),它们分配在 GPIO 的Bank0 和Bank1隶属于PS部分,这些I ...
- idea插件,配置
1.插件 2.配置 1.去除double shift 全局搜索的功能,反正不符合我的习惯 Help -> Find Action 搜索 registry ,勾选 double.click.ha ...
- JAVA学习篇--静态代理VS动态代理
本篇博客的由来,之前我们学习大话设计,就了解了代理模式,但为什么还要说呢? 原因: 1,通过DRP这个项目,了解到了动态代理,认识到我们之前一直使用的都是静态代理,那么动态代理又有什么好处呢?它们二者 ...
- MongoDB实战读书笔记(一):JavaScript shell操作MongoDB
1 基本增删改查 基本概念: 数据库:同关系型数据库 集合:类似关系型数据库的表 文档:类似关系型数据库的行 字段:类似关系型数据库的列 操作: insert:新增,若新增数据的主键已经存在,则会抛异 ...
- Python脚本:Linux自动化执行Python脚本
1.环境及其工具: ubuntu 16.04 python2.7(自带) pip2.7(安装) virtualenv(安装) crontab (自带) 2.pip2.7安装 (1)尝试使用 sudo ...
- H5的本地存储(localStorage)和cookie比较
HTML5 的 web Storage 存储方式有两种:localStorage 和 sessionStorage. sessionStorage就像是会话级别的cookie,数据会随着浏览器关闭而清 ...
- 处理python错误问题
------------恢复内容开始------------ 调试过程中遇到的问题 (1)爬取首页源码出现中文乱码 解决方案: 将网页编码强制转换成gbk,并去除解决乱码问题的三行代码. (2)程序运 ...
- Spark机器学习API之特征处理(一)
Spark机器学习库中包含了两种实现方式,一种是spark.mllib,这种是基础的API,基于RDDs之上构建,另一种是spark.ml,这种是higher-level API,基于DataFram ...
- 正则表达式split匹配多种例如 “】”,“,”两种(页面级中英文切换方案)
在做登陆界面的时候,因为涉及到中英文 因为前后台已经分离,所以前端需要自行设计中英文 做法: 编写两个文件,一个中文文件,一个是英文文件,分别放在对应的目录下面 文件的内容 { "login ...