Codeforces Round #425 (Div. 2) D.Misha, Grisha and Underground
我奇特的脑回路的做法就是
树链剖分 + 树状数组
树状数组是那种 区间修改,区间求和,还有回溯的
当我看到别人写的是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的更多相关文章
- 【树链剖分】【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一样无脑,但是少 ...
- 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles
题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...
- 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- 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 ...
- Codeforces Round #425 (Div. 2))——A题&&B题&&D题
A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...
- 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 ...
- 【 Codeforces Round #425 (Div. 2) D】Misha, Grisha and Underground
[Link]:http://codeforces.com/contest/832/problem/D [Description] 给你一棵树; 然后给你3个点 让你把这3个点和点s,t,f对应; 然后 ...
- Codeforces Round #425 (Div. 2) - D
题目链接:http://codeforces.com/contest/832/problem/D 题意:给定一棵n个点的树,然后给你q个询问,每个询问为三元组(a,b,c),问你从这三个点中选取一个作 ...
随机推荐
- vue 学习中 版本、问题集锦
看学习视频,因为年份比较早了,其实vue早已迭代到vue2.0了,遇到一些问题: v-for遍历数组,获取索引 注意:在2.0版是1~10中,$index已废除,索引 (item,index). 如下 ...
- bzoj 4870: [Shoi2017]组合数问题 [矩阵乘法优化dp]
4870: [Shoi2017]组合数问题 题意:求 \[ \sum_{i=0}^{n-1} \binom{nk}{ik+r} \mod p \] \(n \le 10^9, 0\le r < ...
- 2018/1/21 Netty通过解码处理器和编码处理器来发送接收POJO,Zookeeper深入学习
package com.demo.netty; import org.junit.Before;import org.junit.Test; import io.netty.bootstrap.Boo ...
- Linux 常用命令 (common commands for linux)
Linux 常用命令 (Common Commands For Linux) 1.声明,此文章仅写基于 Bash shell 常用的命令,如果遇上命令在使用过程中提示没有,可能随着更新,命令也被替换掉 ...
- ftp服务器的简单配置使用
yum install -y vsftpd systemctl start vsftpd cd /var/ftp/pub/ mkdir 111 touch weifeng.txt ...
- git 域名配置
在Godaddy购买的域名: 查找DNSpod解析域名,没什么难度,就是添加一条记录,保存而已,记得在添加域名到DNSpod之后,复制两个NS地址到godaddy的域名服务器下: Git项目根目录下创 ...
- Oracle创建表时Storage参数具体含义
本文通过图表和实例的阐述在Oracle数据库创建新表时Storage的参数具体含义. 可用于:表空间.回滚段.表.索引.分区.快照.快照日志 参数名称 缺省值 最小值 最大值 说明 INITIAL 5 ...
- js的继承实现
1.原型链继承 1.创建父类对象2.创建子类函数对象3.将父类的实例对象赋值给子类的原型4.将子类的原型属性的构造函数设置为 子类本身 function Person(name) { this.nam ...
- MySQL 参数- Innodb_File_Per_Table(独立表空间)
Innodb存储引擎可将所有数据存放于ibdata*的共享表空间,也可将每张表存放于独立的.ibd文件的独立表空间.共享表空间以及独立表空间都是针对数据的存储方式而言的. 共享表空间某一个数据库的所有 ...
- 3.2 while 循环
Python 编程中 while 语句用于循环执行程序,即在条件满足的情况下,循环执行某段代码.所以就需要在循环的代码块中设计一种使代码块循环执行一定次数后是while语句的条件不满足,从而中止whi ...