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

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

当我看到别人写的是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. dfs_SPFA 判负环

    感觉有点像tarjan求SCC #include <iostream> #include <cstdio> #include <algorithm> #includ ...

  2. CF528D. Fuzzy Search [FFT]

    CF528D. Fuzzy Search 题意:DNA序列,在母串s中匹配模式串t,对于s中每个位置i,只要s[i-k]到s[i+k]中有c就认为匹配了c.求有多少个位置匹配了t 预处理\(f[i][ ...

  3. CentOS上安装GitBlit服务

    简单介绍 在上一篇文章中,已经简单的介绍了如何在CentOS的服务器上搭建git服务器.但是这种方式实现的服务器功能比较弱,操作起来也比较繁琐.在网上搜索了一圈,感觉Gitblit比较符合我的需求.接 ...

  4. [Python Study Notes] 编程仪式感的Hello World!

    学习还是要有一点仪式感的,学单片机第一步,点个灯:学编程第一步,hello world! C:\Users\Liu>python Python 3.6.4 (v3.6.4:d48eceb, De ...

  5. [bzoj3875] [Ahoi2014]骑士游戏

    3875: [Ahoi2014]骑士游戏 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 844  Solved: 440[Submit][Status ...

  6. easyui+ajax获取同表关联的数据

    easyui是我们常用的前端框架之一,easyui的使用使得前端页面更加美观.为了能够使用combobox,ajax必须同步. 该小程序是使用ssm框架,对数据库的数据进行查询,所以url对应着map ...

  7. 原生ajax写的上拉加载

    上拉加载的思路 1 上拉加载是要把屏幕拉到最底部的时候触发ajax事件请求数据 2.所有要获取屏幕的高度 文档的高度 和滚动的高度 下面的代码是已经做好了兼容的可以直接拿来用 Javascript: ...

  8. tensorflow import 没找到cudnn库问题解决

    ImportError: libcudnn.so.5: cannot open shared object file: No such file or directory 将cuda下lib64中的l ...

  9. 高并发关于微博、秒杀抢单等应用场景在PHP环境下结合Redis队列延迟入库

    第一步:创建模拟数据表. CREATE TABLE `test_table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) NOT NUL ...

  10. axios + mock.js模拟数据实现前后端分离开发的实例代码

    首先就是必须安装axios和mock.js npm install axios npm install mockjs 1. 然后在文档src中新建一个mock.js文件,如图 2. 在main.js中 ...