我奇特的脑回路的做法就是
树链剖分 + 树状数组

树状数组是那种 区间修改,区间求和,还有回溯的

当我看到别人写的是lca,直接讨论时,感觉自己的智商收到了碾压。。。

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
#define MS(x,y) memset(x,y,sizeof(x))
#define MP(x, y) make_pair(x, y)
const int INF = 0x3f3f3f3f; int n, q;
struct Node{
int to,next;
}edge[N << 1];
int tot;
int head[N];
void addedge(int x,int y){
edge[tot].to = y; edge[tot].next = head[x]; head[x] = tot ++;
} int top[N],fa[N],son[N],deep[N],num[N],p[N],fp[N],pos;
void dfs1(int x,int pre,int dep){
deep[x] = dep;
fa[x] = pre;
num[x] = 1;
for(int i = head[x]; i != -1; i = edge[i].next){
int y = edge[i].to;
if(y == pre) continue;
dfs1(y,x,dep + 1);
num[x] += num[y];
if(son[x] == -1 || num[y] > num[son[x]])
son[x] = y;
}
}
void dfs2(int x,int tp){
top[x] = tp;
p[x] = pos++;
fp[p[x]] = x;
if(son[x] == -1) return;
dfs2(son[x],tp);
for(int i = head[x] ; i != -1; i = edge[i].next){
int y = edge[i].to;
if(y != son[x] && y != fa[x])
dfs2(y,y);
}
} ll tree1[N]; ll tree2[N];
void Add(ll tree[], int pos, int val) {
for(int i = pos; i <= n; i += i&-i) {
tree[i] += val;
}
}
ll Sum(ll tree[], int pos) {
if(pos == 0) return 0;
ll ans = 0;
for(int i = pos; i; i -= i&-i) {
ans += tree[i];
}
return ans;
}
vector<pair<int, int> > Resume;
void Find(int x, int y) {
int fx = top[x]; int fy = top[y];
while(fx != fy) {
if(deep[fx] < deep[fy]) {
swap(fx, fy);
swap(x, y);
}
Add(tree1, p[fx], 1);
Add(tree1, p[x]+1, -1);
Add(tree2, p[fx], p[fx]);
Add(tree2, p[x]+1, -p[x]-1); Resume.push_back(MP(p[fx], -1));
Resume.push_back(MP(p[x]+1, 1));
Resume.push_back(MP(-p[fx], -p[fx]));
Resume.push_back(MP(-p[x]-1, p[x]+1)); x = fa[fx];
fx = top[x];
}
if(deep[x] > deep[y]) swap(x, y);
Add(tree1, p[x], 1);
Add(tree1, p[y]+1, -1);
Add(tree2, p[x], p[x]);
Add(tree2, p[y]+1, -p[y]-1); Resume.push_back(MP(p[x], -1));
Resume.push_back(MP(p[y]+1, 1));
Resume.push_back(MP(-p[x], -p[x]));
Resume.push_back(MP(-p[y]-1, p[y]+1));
}
ll Total(int x, int y) {
ll sum = 0;
int fx = top[x]; int fy = top[y];
while(fx != fy) {
if(deep[fx] < deep[fy]) {
swap(fx, fy);
swap(x, y);
}
sum += 1ll*(p[x]+1)*Sum(tree1, p[x]) - Sum(tree2, p[x]) - 1ll*(p[fx])*Sum(tree1, p[fx]-1) + Sum(tree2, p[fx]-1);
x = fa[fx];
fx = top[x];
}
if(deep[x] > deep[y]) swap(x, y);
sum += 1ll*(p[y]+1)*Sum(tree1, p[y]) - Sum(tree2, p[y]) - 1ll*(p[x])*Sum(tree1, p[x]-1) + Sum(tree2, p[x]-1); return sum;
} ll solve(int a,int b, int c,int d) {
Resume.clear();
Find(a, b);
// for(int i = 1; i <= n; ++i) printf("%d ", Sum(i)); printf("\n");
ll tt = Total(c, d);
// printf("hh %d %d %d %d %d\n",a,b,c,d, tt);
for(int i = 0; i < Resume.size(); ++i) {
if(Resume[i].first > 0) Add(tree1, Resume[i].first, Resume[i].second);
else Add(tree2, -Resume[i].first, Resume[i].second);
}
return tt;
}
int main() {
while(~scanf("%d %d", &n, &q)) {
MS(tree1, 0); MS(tree2, 0);
memset(head, -1, sizeof(head));
memset(son, -1, sizeof(son));
tot = 0; pos = 1; for(int i = 2; i <= n; ++i) {
int a; scanf("%d", &a);
addedge(a, i); addedge(i, a);
} dfs1(1, 1, 1);
dfs2(1, 1); // for(int i = 1; i <= n; ++i) printf("%d ", p[i]); printf("\n"); for(int i = 0; i < q; ++i) {
int a, b, c; scanf("%d %d %d", &a, &b, &c);
ll ans = max(max(solve(a,b, a,c), solve(a,b, b,c)), solve(a,c, b,c));
printf("%lld\n", ans);
} }
return 0;
}

Codeforces Round #425 (Div. 2) D.Misha, Grisha and Underground的更多相关文章

  1. 【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

    一棵树,q次询问,每次给你三个点a b c,让你把它们选做s f t,问你把s到f +1后,询问f到t的和,然后可能的最大值是多少. 最无脑的想法是链剖线段树……但是会TLE. LCT一样无脑,但是少 ...

  2. 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  3. 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

    题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...

  4. 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  5. Codeforces Round #425 (Div. 2) Misha, Grisha and Underground(LCA)

    Misha, Grisha and Underground time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. Codeforces Round #425 (Div. 2))——A题&&B题&&D题

    A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...

  7. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

  8. 【 Codeforces Round #425 (Div. 2) D】Misha, Grisha and Underground

    [Link]:http://codeforces.com/contest/832/problem/D [Description] 给你一棵树; 然后给你3个点 让你把这3个点和点s,t,f对应; 然后 ...

  9. Codeforces Round #425 (Div. 2) - D

    题目链接:http://codeforces.com/contest/832/problem/D 题意:给定一棵n个点的树,然后给你q个询问,每个询问为三元组(a,b,c),问你从这三个点中选取一个作 ...

随机推荐

  1. 洛谷 [P3384] 树链剖分 模版

    支持各种数据结构上树,注意取膜. #include <iostream> #include <cstring> #include <algorithm> #incl ...

  2. BZOJ 1097: [POI2007]旅游景点atr [DP 状压 最短路]

    传送门 题意: 一个无向图,从$1$到$n$,要求必须经过$2,3,...,k+1$,给出一些限制关系,要求在经过$v \le k+1$之前必须经过$u \le k+1$ 求最短路 预处理出$1... ...

  3. BZOJ 3992: [SDOI2015]序列统计 [快速数论变换 生成函数 离散对数]

    3992: [SDOI2015]序列统计 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1017  Solved: 466[Submit][Statu ...

  4. Matplotlib学习笔记(二)

    原  Matplotlib学习笔记 参考:Python数据科学入门教程 Python3.6.1 jupyter notebook .caret, .dropup > .btn > .car ...

  5. XSD详解三 - 复合元素+总结篇

    一.复合元素介绍 1.什么是复合元素? 复合元素指包含其他元素及/或属性的 XML 元素. 有四种类型的复合元素: 空元素 包含其他元素的元素 仅包含文本的元素 包含元素和文本的元素 注释:上述元素均 ...

  6. php语言基础(一)

    一.php标记风格 1.1xml风格(常用) <?php echo "xml风格": ?> 1.2脚本风格 <script language="php& ...

  7. linux内核链表的使用

    linux内核链表:链表通常包括两个域:数据域和指针域.struct list_head{struct list_head *next,*prev;};include/linux/list.h中实现了 ...

  8. 奥酷流媒体服务系统AMS5.0

      2016年6月29日,北极星通对外发布AMS5.0版本,AMS是北极星通公司独立研发的高性能流媒体服务系统软件,可广泛应用于视频直播,视频点播,视频转码,视频录播等场合. AMS5.0增加功能: ...

  9. bzoj 1188 [HNOI2007]分裂游戏 SG函数 SG定理

    [HNOI2007]分裂游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1394  Solved: 847[Submit][Status][Dis ...

  10. Smarty3.1.3安装使用

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Verdana } span.s1 { } Smarty简介 Smarty是一个PHP的模板引 ...