Codeforces Round #425 (Div. 2) - D
题目链接:http://codeforces.com/contest/832/problem/D
题意:给定一棵n个点的树,然后给你q个询问,每个询问为三元组(a,b,c),问你从这三个点中选取一个作为终点,一个作为Misha的起点,一个作为Grisha的起点。然后每天早上Misha从起点到终点所经过的点都是标记为1, 傍晚Grisha从起点到终点所经过的点中带有标记的点的数目最多是多少?
思路:对于每个询问,我们枚举终点(共3种情况),其余两个点作为一个作为M的起点一个作为G的起点,然后问题就是M的起点到终点这条路径的点赋值1,统计G的起点到终点这条路径的1的个数,然后3种情况取个最大值即可。 然后就是经典的树链剖分题目,树剖之后就是区间覆盖+区间查询问题了。 起初用的是线段树,然后终测TLE掉了(可能我写的线段树不够优美,被卡常了),后来换成树状数组来维护区间覆盖,区间查询就AC掉了。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
using namespace std;
typedef long long int LL;
const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int fa[MAXN],top[MAXN],deep[MAXN],num[MAXN],p[MAXN],fp[MAXN],son[MAXN];
int pos,cp[MAXN],n,q;
LL bit0[MAXN],bit1[MAXN];
vector<int>edge[MAXN];
void init(){
pos = ;
memset(bit0,,sizeof(bit0));
memset(bit1,,sizeof(bit1));
memset(son, -, sizeof(son));
}
void dfs1(int u, int pre, int d){
deep[u] = d; fa[u] = pre; num[u] = ;
for (int i = ; i < edge[u].size(); i++){
int v = edge[u][i];
if (v != pre){
dfs1(v, u, d + );
num[u] += num[v];
if (son[u] == - || num[v] > num[son[u]]
){
son[u] = v;
}
}
}
}
void dfs2(int u, int sp){
top[u] = sp; p[u] = pos++; fp[p[u]] = u;
if (son[u] == -){
return;
}
dfs2(son[u], sp);
for (int i = ; i < edge[u].size(); i++){
int v = edge[u][i];
if (v != fa[u] && v != son[u]){
dfs2(v, v);
}
}
} //BIT
void Add(LL *b,int i,LL val){
while (i<=n){
b[i]+=val; i+=i&-i;
}
}
LL Sum(LL *b,int i){
LL s=;
while (i>){
s+=b[i]; i-=i&-i;
}
return s;
}
void Modify(int l,int r,int val){ //区间[l,r] + val
//printf("M:%d %d %d\n",l,r,val);
Add(bit0,l,-val*(l-));
Add(bit1,l,val);
Add(bit0,r+,val*r);
Add(bit1,r+,-val);
}
int Query(int l,int r){ //区间[l,r] 1 的个数
//printf("Q:%d %d\n",l,r);
LL res=;
res+=Sum(bit0,r)+1LL*Sum(bit1,r)*r;
res-=Sum(bit0,l-)+1LL*Sum(bit1,l-)*(l-);
return res;
}
void solveC(int u, int v,int val){ //修改链
int f1 = top[u], f2 = top[v];
while (f1!=f2){
if (deep[f1] < deep[f2]){
swap(f1, f2); swap(u, v);
}
Modify(p[f1], p[u], val);
u = fa[f1];
f1 = top[u];
}
if (deep[u] > deep[v]){
swap(u, v);
}
Modify(p[u], p[v], val);
}
int solveQ(int u, int v){ //查询链
int f1 = top[u], f2 = top[v];
int tmp = ;
while (f1 != f2){
if (deep[f1] < deep[f2]){
swap(f1, f2); swap(u, v);
}
tmp+=Query(p[f1], p[u]);
u = fa[f1]; f1 = top[u];
}
if (deep[u] > deep[v]){
swap(u, v);
}
tmp+=Query(p[u], p[v]);
return tmp;
}
int solve(int s, int t, int f){
solveC(s, f, );
int tmp = solveQ(t, f);
solveC(s, f, -);
return tmp;
}
int main(){
#ifdef kirito
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
while (~scanf("%d%d",&n,&q)){
init();
for (int i = ; i <= n; i++){
edge[i].clear();
}
for (int i = ; i <= n; i++){
scanf("%d", &cp[i]);
edge[cp[i]].push_back(i);
edge[i].push_back(cp[i]);
}
dfs1(, , );
dfs2(, );
for (int i = ; i <= q; i++){
int a, b, c,res=;
scanf("%d%d%d", &a, &b, &c);
res = max(res, solve(a, b, c));
res = max(res, solve(a, c, b));
res = max(res, solve(b, c, a));
printf("%d\n", res);
}
}
return ;
}
Codeforces Round #425 (Div. 2) - D的更多相关文章
- Codeforces Round #425 (Div. 2)C
题目连接:http://codeforces.com/contest/832/problem/C C. Strange Radiation time limit per test 3 seconds ...
- Codeforces Round #425 (Div. 2)
A 题意:给你n根棍子,两个人每次拿m根你,你先拿,如果该谁拿的时候棍子数<m,这人就输,对手就赢,问你第一个拿的人能赢吗 代码: #include<stdio.h>#define ...
- 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) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论
n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...
- Codeforces Round #425 (Div. 2) Problem B Petya and Exam (Codeforces 832B) - 暴力
It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy ...
- Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks (Codeforces 832A)
It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day h ...
- Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)
题目链接:http://codeforces.com/contest/832/problem/B B. Petya and Exam time limit per test 2 seconds mem ...
- 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) B - Petya and Exam
地址:http://codeforces.com/contest/832/problem/B 题目: B. Petya and Exam time limit per test 2 seconds m ...
- Codeforces Round #425 (Div. 2) C - Strange Radiation
地址:http://codeforces.com/contest/832/problem/C 题目: C. Strange Radiation time limit per test 3 second ...
随机推荐
- 封装通用的 ajax, 基于 jQuery。
在前端异步获取数据时候每次都是使用 ajax:为了通用性更好,然而封装通用的 ajax 是一个一劳永逸的办法. 本次基于 jQuery 封装实现: 第一步: 引入 jQuery: <script ...
- D1. Kirk and a Binary String (easy version)
D1. Kirk and a Binary String (easy version) 01串找最长不降子序列 给定字符串s,要求生成一个等长字符串t,使得任意l到r位置的最长不降子序列长度一致 从后 ...
- SpringBoot 集成 Spring Session
SpringBoot 集成 Spring Session 应该讲解清楚,为什么要使用 Redis 进行 Session 的管理. Session 复制又是什么概念. Spring Session 在汪 ...
- java 中 进程和线程的区别
目录 什么是进程?什么是线程? 为什么要有线程? 进程与线程的区别? 进程与线程的选择取决条件? 什么是进程?什么是线程?进程:进程是并发执行程序在执行过程中资源分配和管理的基本单位(资源分配的最小单 ...
- fedora18 [linux]Error: failure: repodata/repomd.xml from fedora: [Errno 256] No more mirrors to try.
在使用fedora17 系统的yum源的时候出现了如下错误: Error: failure: repodata/repomd.xml from fedora: [Errno 256] No more ...
- grep的用法,小技巧,模板中含有\t时:grep -P "^\t" file
linux中grep和find的用法区别 本文章详细的介绍了关于在linux中的grep和find两个命令的用法介绍,以及后面总结了它们两年用法区别哦. 先我们来介绍一下关于grep用法和一些小注意事 ...
- view组件
view标签的属性值: hover-class:按下的点击态 属性值:字符串 如果:hover-class="none" 按下就没有点击态 hover-stop-pro ...
- IntelliJ常用配置备忘
前言 最近IntelliJ又由于自己的骚操作给弄崩溃了,导致之前弄的一大波配置又找不到了,十分蛋疼的又要开始重头开始弄环境.很多之前精心搞过的配置又都记不住了,为了防止以后出现这种情况,这里就把我日常 ...
- apache配置补充
apache的安装: 分成三种方式: tar包 rpm安装 yum安装. ============ tar包安装 ======================== 下载.tar.gz的安装包 解压和安 ...
- python安装使用(windows)
安装 参考:http://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/install.html#scrapy 用到的文件:https://share.weiy ...