https://www.luogu.org/problemnew/show/3038

将为边加权转化成为点加权

对于每条边,我们将其深度更大的端点加权,则区间 [l, r] 的加权(或询问)即为去掉深度最小的点,给其余点加权(或询问)。

1)最后一次树剖查询时,当前两点 x,y 必定在同一重链上(y 的深度小于 x);

2)根据处理前的 dfs 序,我们是优先遍历重儿子,那么重儿子在线段树中的编号一定紧邻其父节点(即为其父节点编号 + 1),则我们要忽略深度最小的那个点 y ,即为处理区间 [ y 在线段树中的编号 + 1, x 在线段树中的编号](另外还要注意的是,y = x 时在线段树内执行会出错,我们要特判这种情况) 。

#include <bits/stdc++.h>using namespace std;
const int N = 1e5 + 10;#define gc getchar()
#define lson jd << 1
#define rson jd << 1 | 1 struct Node_1{int v, nxt;
}G[N << 1];
struct Node_2{int fa, son, deep, topp, siz, tree;
}P[N];
struct Node_3{int l, r, w, f;
}T[N << 2];
int n, Ti, now = 1, head[N], tim, ans;inline int read(){int x = 0; char c = gc;while(c < '0' || c > '9') c = gc;while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;return x;
}inline void add(int u, int v){G[now].v = v;G[now].nxt = head[u];head[u] = now ++;
}void dfs_find_son(int u, int fa, int dep){P[u].fa = fa;P[u].deep = dep;P[u].siz = 1;for(int i = head[u]; ~ i; i = G[i].nxt){int v = G[i].v;if(v != fa){dfs_find_son(v, u, dep + 1);P[u].siz += P[v].siz;if(P[v].siz > P[P[u].son].siz) P[u].son = v;}}
}void dfs_to_un(int u, int tp){P[u].topp = tp;P[u].tree = ++ tim;if(!P[u].son) return ;dfs_to_un(P[u].son, tp);for(int i = head[u]; ~ i; i = G[i].nxt){int v = G[i].v;if(v != P[u].son && v != P[u].fa) dfs_to_un(v, v);}
}void down(int jd){int F = T[jd].f;T[lson].w += (T[lson].r - T[lson].l + 1) * F; T[lson].f += F;T[rson].w += (T[rson].r - T[rson].l + 1) * F; T[rson].f += F;T[jd].f = 0;
}void Sec_G(int l, int r, int jd, int x, int y){if(x <= l && r <= y){int yj = r - l + 1;T[jd].w += yj;T[jd].f ++;return ;}if(T[jd].f) down(jd);int mid = (l + r) >> 1;if(x <= mid) Sec_G(l, mid, lson, x, y);if(y > mid) Sec_G(mid + 1, r, rson, x, y);
}void Sec_G_imp(int x, int y){int tp1 = P[x].topp, tp2 = P[y].topp;while(tp1 != tp2){if(P[tp1].deep < P[tp2].deep) swap(x, y), swap(tp1, tp2);Sec_G(1, n, 1, P[tp1].tree, P[x].tree);x = P[tp1].fa;tp1 = P[x].topp;}if(x == y) return ;if(P[x].deep > P[y].deep) swap(x, y);Sec_G(1, n, 1, P[x].tree + 1, P[y].tree);
}void build_tree(int l, int r, int jd){T[jd].l = l; T[jd].r = r;if(l == r) return ;int mid = (l + r) >> 1;build_tree(l, mid, lson);build_tree(mid + 1, r, rson);
}void Sec_A(int l, int r, int jd, int x, int y){if(x <= l && r <= y){ans += T[jd].w;return ;}if(T[jd].f) down(jd);int mid = (l + r) >> 1;if(x <= mid) Sec_A(l, mid, lson, x, y);if(y > mid) Sec_A(mid + 1, r, rson, x, y);
}int Sec_A_imp(int x, int y){int ret = 0;int tp1 = P[x].topp, tp2 = P[y].topp;while(tp1 != tp2){if(P[tp1].deep < P[tp2].deep) swap(x, y), swap(tp1, tp2);ans = 0;Sec_A(1, n, 1, P[tp1].tree, P[x].tree);ret += ans;x = P[tp1].fa;tp1 = P[x].topp;}if(x == y) return ret;ans = 0;if(P[x].deep > P[y].deep) swap(x, y);Sec_A(1, n, 1, P[x].tree + 1, P[y].tree);ret += ans;return ret;
}int main()
{n = read(); Ti = read();for(int i = 1; i <= n; i ++) head[i] = -1;for(int i = 1; i < n; i ++) {int u = read(), v = read();add(u, v); add(v, u);}dfs_find_son(1, 0, 1);dfs_to_un(1, 1);build_tree(1, n, 1);while(Ti --){string s; cin >> s;int x = read(), y = read();if(s[0] == 'P') Sec_G_imp(x, y);else cout << Sec_A_imp(x, y) << endl;}return 0;
}

转载于:https://www.cnblogs.com/shandongs1/p/8012595.html

[USACO11DEC] 牧草种植Grass Planting相关推荐

  1. 【LuoguP3038/[USACO11DEC]牧草种植Grass Planting】树链剖分+树状数组【树状数组的区间修改与区间查询】...

    模拟题,可以用树链剖分+线段树维护. 但是学了一个厉害的..树状数组的区间修改与区间查询.. 分割线里面的是转载的: ----------------------------------------- ...

  2. 2419. Grass Planting

    2419. Grass Planting 题目描述 到了一年中Farmer John在他的草地里种草的时间了.整个农场由N块草地组成(1≤N≤10^5),方便起见编号为1-N,由N−1条双向的小路连接 ...

  3. 洛谷 P5197 [USACO19JAN]Grass Planting 题解

    题目:P5197 [USACO19JAN]Grass Planting 图论 - 树 题目大意 给出一棵无根树,要给每个节点染上颜色,要求对于每两个最短距离小于等于222的点,不能染上相同的颜色,求最 ...

  4. (纪中)2419. Grass Planting

    (File IO): input:planting.in output:planting.out 时间限制: 1000 ms 空间限制: 262144 KB 具体限制 Goto ProblemSet ...

  5. Grass Planting 题解

    GrassGrassGrass PlantingPlantingPlanting 题解 题目 解题方法 方法111:树形dpdpdp 设dpidp_idpi​表示以iii为根的子树需要使用的最少的草的 ...

  6. 洛谷5197 USACO Grass Planting

    思维题-其实可以很容易想到要覆盖,类似于大星星覆盖小的,所以入度最多的点的入度+1就是答案 #include <iostream> #define MAXN 500005 using na ...

  7. 2018年12月3031日

    小结:昨天由于做的题目比较少,所以就和今天写在一块了,昨天学习了差分约束和树上差分,当然树上差分是用线段树来维护的,今天重点整理了博客\(233\),然后做了几个题. 一. 完成的题目: 洛谷P327 ...

  8. vue页面跳转没有ajax,vue router路由跳转了,但是页面没有变(已解决)

    IOS开发:监听来电状态的改变. #import #import @property(nonato ... salesforce 零基础学习(三十)工具篇:Debug Log小工具 开发中查看log日 ...

  9. PIE-engine 教程 ——新疆石河子市棉花种植面积提取(阈值法)案例分析

    我们之前有有一个案例用来提取大蒜的种植面积,这里我们首先来解释一下阈值法 阈值法,关于阈值法的介绍很多人还不知道,现在让我们一起来看看吧! 1.阈值分割法是一种基于区域的图像分割技术,原理是把图像象素 ...

  10. 透过“最牛”年报,聊聊优然牧业的“最佳养牛模式”

    乳品行业近年非常受到资本市场推崇,去年刚刚在港股上市的乳业上游龙头企业--优然牧业,长期保持着高速增长,成长性非常好. 今天我们就来重点解读优然牧业上市以来的首份年报. 其营收153.46亿元,同比增 ...

最新文章

  1. 深度学习(十八)基于R-CNN的物体检测-CVPR 2014-未完待续
  2. Unity3D获取Andorid设备返回键,主页键等功能
  3. 【转】C#中StreamWriter与BinaryWriter的区别兼谈编码。
  4. Luogu P3975 [TJOI2015]弦论
  5. Xcode 添加 background modes、Associated Domains 等设置项
  6. 《惢客创业日记》2019.01.18(周五)想象力害死人呀!
  7. 万字自动化测试面试题,助你吊打面试官
  8. wps文档怎么做链接
  9. ChatGPT 爆火!谷歌、微软、百度纷纷下场?
  10. 递归——兔子繁殖问题
  11. nuc972外部中断1实验代码
  12. docker容器状态Exited(1)第一篇
  13. iPhone的解锁、越狱、激活、固件等等是什么意思,有什么分别?
  14. matlab hdf属性,matlab hdf 读取
  15. 验证码的几种方式-普通图形验证码,滑动拼图,图中点选
  16. 线性代数Python计算:向量空间坐标变换
  17. 买股票总是跌多涨少?买啥啥跌?试试“选股助手”吧
  18. 百度董事长李彦宏的理念
  19. 剑指offer_2_二进制加法(java)
  20. iNFTnews|《时代》杂志盈利1000万美元,他做对了什么?

热门文章

  1. 更改自定义按钮显示值并对单元格赋值
  2. 2008年日历带农历_头条文章--Excel中带农历的万年历设计方法一
  3. stringbuilder为什么线程不安全_String Builder 为什么线程不安全?
  4. python进程与线程_Python 进程和线程
  5. 如何在linux下运行php脚本,如何通过Linux命令行使用和运行PHP脚本
  6. antdesign 所兼容的浏览器_React爬坑之路——Antd兼容IE
  7. Git学习的简单笔记
  8. PHP echo 即时输出
  9. [经典php视频]构建正则表达式解析网页中的图像标记img
  10. 七、数值微分与数值积分