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

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

当我看到别人写的是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. BZOJ 3239: Discrete Logging [BGSG]

    裸题 求\(ind_{n,a}b\),也就是\(a^x \equiv b \pmod n\) 注意这里开根不能直接下取整 这个题少了一些特判也可以过... #include <iostream& ...

  2. POJ 3304 Segments[直线与线段相交]

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13514   Accepted: 4331 Descrip ...

  3. SUSE-11 本地 zypper 配置

    配置本地 zypper 目的: 安装 SUSE-11 后想要再添加或删除软件组件将比较麻烦.通过配置本地 zypper 将可以从下载的软件仓库(repository)中安装软件包.   本地 zypp ...

  4. linux集群架构

    Linux集群架构   根据功能划分为两大类:高可用和负载均衡 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 实现高可用的开源软件有:heart ...

  5. ansible实践3-playbook条件判断

    --- - hosts: testhost   user: root   gather_facts: True   tasks:     - name: use when       shell: t ...

  6. 【实用】需要收藏备用的JQuery代码片段

    1 元素屏幕居中 jQuery.fn.center = function () { this.css("position","absolute"); this. ...

  7. python并发编程之多进程(三):共享数据&进程池

    一,共享数据 展望未来,基于消息传递的并发编程是大势所趋 即便是使用线程,推荐做法也是将程序设计为大量独立的线程集合 通过消息队列交换数据.这样极大地减少了对使用锁定和其他同步手段的需求, 还可以扩展 ...

  8. Winform下去除MDI窗体边框

    做项目中间遇到了MDI窗体内边框的问题,经过苦苦寻找,最终得到了解决方案 在Main窗体中调用API // Win32 Constants ; ; private const int WS_BORDE ...

  9. 初识vue——起步

    一.目录结构: 我们经常使用的是以下几个目录: 1.assets:静态资产文件:在vue组件中,所有组件中,所有模板和CSS都会被vue-html-loader和css-loader解析,并查找资源u ...

  10. PHP动态编译出现Cannot find autoconf的解决方法

    wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz tar -zvxf m4-.tar.gz cd m4-/ ./configure && m ...