题目链接: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 ;
}

最新文章

  1. 【软件推荐】 Moom-窗口布局软件(V.3.2.2)【破解+汉化】
  2. startInstrumentation asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
  3. dedecms内容页调用缩略图 缩略图多种用法(借鉴)
  4. React Native技术知识总结(不定期补充)
  5. DTcms手机版使用余额支付 提示信息跳转到PC版的错误。以及提交订单不打开新页面
  6. 【QT相关】Image Viewer Example
  7. JavaScript入门学习笔记(一)
  8. Ubuntu 16.04更新grub-pc提示脚本/var/lib/dpkg/info/grub-pc.postinst 执行错误
  9. 最小生成树-QS Network(Prim)
  10. Python装饰器用法
  11. jmeter解决request response中文乱码问题
  12. shell if [[ ]]的一次应用
  13. 对象的释放Dispose和Close对比
  14. [转]ORA-12560: TNS: 协议适配器错误
  15. jrebel
  16. OpenGL中的常用绘图的命令与效果(经验设置)
  17. 使用HTML5和CSS3碎语
  18. 理解js中的new
  19. openal 基础知识
  20. 识别User Agent屏蔽一些Web爬虫防采集

热门文章

  1. OpenCV feature2d
  2. 一本通例题埃及分数—题解&amp;&amp;深搜的剪枝技巧总结
  3. permutation 1
  4. 在 mac 系统上安装 python 的 MySQLdb 模块
  5. 006-unity3d GUI初识、贴图、自定义鼠标指针
  6. 008-elasticsearch5.4.3【二】ES使用、ES客户端、索引操作【增加、删除】、文档操作【crud】
  7. IIS 配置备份和还原
  8. 《图解设计模式》读书笔记8-2 MEMENTO模式
  9. 从Android手机中取出已安装的app包,导出apk
  10. 桌面应用开发用到的Framework