Solution

智障暴力题, 每个点维护一下子树信息, 树剖就好了. 我居然还傻了写了一发毛毛虫...

#include <cstdio>
#include <cctype>
#include <vector>
#include <algorithm>
#include <cstring>
#define vector std::vector
#define max std::max
#define min std::min
#define sort std::sort
#define swap std::swap namespace Zeonfai
{
inline int getInt()
{
int a = 0, sgn = 1; char c;
while(! isdigit(c = getchar())) if(c == '-') sgn *= -1;
while(isdigit(c)) a = a * 10 + c - '0', c = getchar();
return a * sgn;
}
}
const int N = (int)1e5;
int n, m;
int a[N];
struct query
{
int u, v, val;
inline query(int _u, int _v, int _val) {u = _u; v = _v; val = _val;}
};
struct binaryIndexedTree
{
int a[N + 1];
inline void clear() {memset(a, 0, sizeof(a));}
inline void modify(int pos, int x)
{
for(int i = pos; i <= n; i += i & - i) a[i] += x;
}
inline int query(int pos)
{
int res = 0;
for(int i = pos; i; i -= i & - i) res += a[i];
return res;
}
inline int query(int L, int R)
{
if(L > R) return 0; else return query(R) - query(L - 1);
}
}BIT[2];
struct tree
{
struct node
{
vector<int> edg;
int hvy, tp, pre;
int sz, dep;
int id[2], L, R;
vector<query> qry;
int ans;
inline void clear() {edg.clear(); qry.clear();}
}nd[N + 1];
inline void clear() {for(int i = 1; i <= n; ++ i) nd[i].clear();}
inline void addEdge(int u, int v) {nd[u].edg.push_back(v); nd[v].edg.push_back(u);}
void getSize(int u, int pre)
{
nd[u].sz = 1; nd[u].dep = ~ pre ? nd[pre].dep + 1 : 0; nd[u].hvy = -1; nd[u].pre = pre;
for(auto v : nd[u].edg) if(v != pre)
{
getSize(v, u); nd[u].sz += nd[v].sz;
if(nd[u].hvy == -1 || nd[v].sz > nd[nd[u].hvy].sz) nd[u].hvy = v;
}
}
int clk;
void decomposition(int u, int tp)
{
nd[u].tp = tp; nd[u].id[0] = ++ clk;
if(~ nd[u].hvy) decomposition(nd[u].hvy, tp);
for(auto v : nd[u].edg) if(v != nd[u].pre && v != nd[u].hvy) decomposition(v, v);
}
inline void decomposition() {getSize(1, -1); clk = 0; decomposition(1, 1);}
void getSection(int u)
{
if(nd[u].hvy == -1) return;
nd[u].L = (int)1e9; nd[u].R = -1;
for(auto v : nd[u].edg) if(v != nd[u].pre) getSection(v), nd[u].L = min(nd[u].L, nd[v].id[1]), nd[u].R = max(nd[u].R, nd[v].id[1]);
}
inline void getSection() {getSection(1);}
inline int getLCA(int u, int v)
{
while(nd[u].tp != nd[v].tp)
{
if(nd[nd[u].tp].dep > nd[nd[v].tp].dep) u = nd[nd[u].tp].pre;
else if(nd[nd[u].tp].dep < nd[nd[v].tp].dep) v = nd[nd[v].tp].pre;
else u = nd[nd[u].tp].pre, v = nd[nd[v].tp].pre;
}
if(nd[u].dep > nd[v].dep) swap(u, v);
return u;
}
inline int getPath(int u, int v, int opt)
{
int res = 0;
if(opt)
{
if(nd[u].hvy == -1) u = nd[u].pre; if(nd[v].hvy == -1) v = nd[v].pre;
while(nd[u].tp != nd[v].tp)
{
if(nd[nd[u].tp].dep > nd[nd[v].tp].dep) res += BIT[1].query(nd[nd[u].tp].L, nd[u].R), u = nd[nd[u].tp].pre;
else if(nd[nd[u].tp].dep < nd[nd[v].tp].dep) res += BIT[1].query(nd[nd[v].tp].L, nd[v].R), v = nd[nd[v].tp].pre;
else res += BIT[1].query(nd[nd[u].tp].L, nd[u].R) + BIT[1].query(nd[nd[v].tp].L, nd[v].R), u = nd[nd[u].tp].pre, v = nd[nd[v].tp].pre;
}
if(nd[u].dep > nd[v].dep) swap(u, v);
res += BIT[1].query(nd[u].L, nd[v].R);
}
else
{
while(nd[u].tp != nd[v].tp)
{
if(nd[nd[u].tp].dep > nd[nd[v].tp].dep) res += BIT[0].query(nd[nd[u].tp].id[0], nd[u].id[0]), u = nd[nd[u].tp].pre;
else if(nd[nd[u].tp].dep < nd[nd[v].tp].dep) res += BIT[0].query(nd[nd[v].tp].id[0], nd[v].id[0]), v = nd[nd[v].tp].pre;
else res += BIT[0].query(nd[nd[u].tp].id[0], nd[u].id[0]) + BIT[0].query(nd[nd[v].tp].id[0], nd[v].id[0]), u = nd[nd[u].tp].pre, v = nd[nd[v].tp].pre;
}
if(nd[u].dep > nd[v].dep) swap(u, v);
res += BIT[0].query(nd[u].id[0], nd[v].id[0]);
}
return res;
}
int getAnswer(int u)
{
nd[u].ans = 0;
for(auto v : nd[u].edg) if(v != nd[u].pre) nd[u].ans += getAnswer(v);
for(auto qry : nd[u].qry)
{
int cur = qry.val;
cur += getPath(qry.u, qry.v, 1) - getPath(qry.u, qry.v, 0);
nd[u].ans = max(nd[u].ans, cur);
}
for(int i = 0; i < 2; ++ i) BIT[i].modify(nd[u].id[i], nd[u].ans);
return nd[u].ans;
}
inline int getAnswer() {return getAnswer(1);}
}T;
inline int cmp(int a, int b)
{
int preA = T.nd[a].pre, preB = T.nd[b].pre;
if(preA == -1) return 1; else if(preB == -1) return 0; else return T.nd[preA].id[0] < T.nd[preB].id[0];
}
int main()
{ #ifndef ONLINE_JUDGE freopen("tchain.in", "r", stdin);
freopen("tchain.out", "w", stdout); #endif using namespace Zeonfai;
for(int cs = getInt(); cs --;)
{
n = getInt(), m = getInt();
T.clear(); for(int i = 0; i < 2; ++ i) BIT[i].clear();
for(int i = 1; i < n; ++ i)
{
int u = getInt(), v = getInt();
T.addEdge(u, v);
}
T.decomposition();
for(int i = 1; i <= n; ++ i) a[i] = i; sort(a + 1, a + n + 1, cmp); for(int i = 1; i <= n; ++ i) T.nd[a[i]].id[1] = i;
T.getSection();
for(int i = 0; i < m; ++ i)
{
int u = getInt(), v = getInt(), val = getInt();
T.nd[T.getLCA(u, v)].qry.push_back(query(u, v, val));
}
printf("%d\n", T.getAnswer());
}
}

2016北京集训测试赛(十一)Problem C: 树链问题的更多相关文章

  1. 2016北京集训测试赛(十七)Problem C: 数组

    Solution 线段树好题. 我们考虑用last[i]表示\(i\)这个位置的颜色的上一个出现位置. 考虑以一个位置\(R\)为右端点的区间最远能向左延伸到什么位置: \(L = \max_{i \ ...

  2. 2016北京集训测试赛(十七)Problem B: 银河战舰

    Solution 好题, 又是长链剖分2333 考虑怎么统计答案, 我场上的思路是统计以一个点作为结尾的最长上升链, 但这显然是很难处理的. 正解的方法是统计以每个点作为折弯点的最长上升链. 具体的内 ...

  3. 2016北京集训测试赛(十七)Problem A: crash的游戏

    Solution 相当于要你计算这样一个式子: \[ \sum_{x = 0}^m \left( \begin{array}{} m \\ x \end{array} \right) \left( \ ...

  4. 2016北京集训测试赛(十六)Problem C: ball

    Solution 这是一道好题. 考虑球体的体积是怎么计算的: 我们令\(f_k(r)\)表示\(x\)维单位球的体积, 则 \[ f_k(1) = \int_{-1}^1 f_{k - 1}(\sq ...

  5. 2016北京集训测试赛(十六)Problem B: river

    Solution 这题实际上并不是构造题, 而是一道网络流. 我们考虑题目要求的一条路径应该是什么样子的: 它是一个环, 并且满足每个点有且仅有一条出边, 一条入边, 同时这两条边的权值还必须不一样. ...

  6. 2016北京集训测试赛(十六)Problem A: 任务安排

    Solution 这道题告诉我们, 不能看着数据范围来推测正解的时间复杂度. 事实证明, 只要常数足够小, \(5 \times 10^6\)也是可以跑\(O(n \log n)\)算法的!!! 这道 ...

  7. BZOJ 4543 2016北京集训测试赛(二)Problem B: thr 既 长链剖分学习笔记

    Solution 这题的解法很妙啊... 考虑这三个点可能的形态: 令它们的重心为距离到这三个点都相同的节点, 则其中两个点分别在重心的两棵子树中, 且到重心的距离相等; 第三个点可能在重心的一棵不同 ...

  8. 2016北京集训测试赛(十四)Problem B: 股神小D

    Solution 正解是一个\(\log\)的link-cut tree. 将一条边拆成两个事件, 按照事件排序, link-cut tree维护联通块大小即可. link-cut tree维护子树大 ...

  9. 2016北京集训测试赛(十四)Problem A: 股神小L

    Solution 考虑怎么卖最赚钱: 肯定是只卖不买啊(笑) 虽然说上面的想法很扯淡, 但它确实能给我们提供一种思路, 我们能不买就不买; 要买的时候就买最便宜的. 我们用一个优先队列来维护股票的价格 ...

随机推荐

  1. MySQL之体系结构与存储实例

    定义数据库和实例 在数据库领域中有两个词很容易混淆,这就是“数据库”(database)和“实例”(instance).作为常见的数据库术语,这两个词的定义如下: 数据库:物理操作系统文件或其他形式文 ...

  2. Redis实现之服务器

    命令请求的执行过程 一个命令请求从发送到获得回复的过程中,客户端和服务器需要完成一系列操作.举个栗子,如果我们使用客户端执行以下命令: 127.0.0.1:6379> SET KEY VALUE ...

  3. 谈一谈Tomcat中webSocket,Jetty WebSocket 和Spring WebSocket以及github中Java-WebSocket.jar分别对Socket协议的角色定位以及效果的不同点;

    开局先上一张图:(http://tomcat.apache.org/tomcat-7.0-doc/web-socket-howto.html)   当前截图来自于apache的tomcat官网(问:为 ...

  4. Hyper-v Server 2012 R2增强会话模式

    从Windows Server 2012 R2开始,通过使用Hyper-v增强会话模式Hyper-v中的虚拟机允许重定向虚拟机连接会话中的本地资源.这是因为Windows Server 2012 及早 ...

  5. Java并发之(2):线程通信wait/notify(TIJ_21_5)

    简介: java中线程间同步的最基本的方式就是使用wait()&notify()&notifyAll(),它们是线程间的握手机制.除了上述方法,java5还在java.util.con ...

  6. Python-S9-Day126——Scrapy爬虫框架

    01 今日内容概要 02 内容回顾和补充:scrapy 03 内容回顾和补充:网络和并发编程 04 Scrapy爬虫框架:pipeline做持久化(一) 05 Scrapy爬虫框架:pipeline做 ...

  7. jquery使用ajax传内容到asp.net乱码解决【转】

    转自:http://www.cnblogs.com/qiantuwuliang/archive/2009/08/02/1537160.html#undefined Jquery强大的功能越来越收到广大 ...

  8. Swift全栈开发

    前段时间学习了一下Swift web framework-Vapor, 类似于PHP Laravel的web框架. Apple也成立了Server APIs Project, Server-side ...

  9. [oldboy-django][2深入django]mysql查询语句--原生sql

    # 增(一共有三种方式) # 插入单条记录 insert into t1(name,...) values('lzp',..); 注意一点:t1(name,...)必须包含所有非空列(除去自增列) # ...

  10. Unity --yield return

    1.yield return null; //暂停协同程序,下一帧再继续往下执行 yield new WaitForFixedUpdate (); //暂停协同程序,等到下一次调用FixedUpdat ...