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 connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other.
The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the underground from station s to station f by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including s and f). After that on the same day at evening Grisha will ride from station t to station f by the shortest path and will count stations with Misha's text. After that at night the underground workers will wash the texts out, because the underground should be clean.
The boys have already chosen three stations a, b and c for each of several following days, one of them should be station s on that day, another should be station f, and the remaining should be station t. They became interested how they should choose these stations s, f, t so that the number Grisha will count is as large as possible. They asked you for help.
The first line contains two integers n and q (2 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of stations and the number of days.
The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi ≤ n). The integer pi means that there is a route between stations pi and i. It is guaranteed that it's possible to reach every station from any other.
The next q lines contains three integers a, b and c each (1 ≤ a, b, c ≤ n) — the ids of stations chosen by boys for some day. Note that some of these ids could be same.
Print q lines. In the i-th of these lines print the maximum possible number Grisha can get counting when the stations s, t and f are chosen optimally from the three stations on the i-th day.
- 3 2
1 1
1 2 3
2 3 3
- 2 3
- 4 1
1 2 3
1 2 3
- 2
In the first example on the first day if s = 1, f = 2, t = 3, Misha would go on the route 1 2, and Grisha would go on the route 3
1
2. He would see the text at the stations 1 and 2. On the second day, if s = 3, f = 2, t = 3, both boys would go on the route 3
1
2. Grisha would see the text at 3 stations.
In the second examle if s = 1, f = 3, t = 2, Misha would go on the route 1 2
3, and Grisha would go on the route 2
3 and would see the text at both stations.
题目大意 给定一个有n个点的树,有m个询问,每个询问给出3个点,你需要分别选出一个点作为s,f,t,如果将s到f的路径上的所有点权加1,计算t到f的路径上的点权和,问可能的点权和最大为多少。
显然树链剖分。
枚举6种可能,然后取个max。然后水完了。
然而比赛最后30分钟发现这特么是道水题,早知道该先写D题(内心崩溃...)。然后最后30分钟内20分钟写了出来,然后gdb挂了。。
然后在没gdb的情况下,今天上午把它改出来(不可思议),然后发现,不会线段树区间lazy标记如何赋初值,树链剖分数组开始滥用。
另外线段树似乎容易被卡常,所以我就改用树状数组了(动态new点,能不TLE就怪了)。
(PS:其实这道题有一个很傻逼的$O(nlogn)$的做法,根本没必要树剖,直接求lca就行了,可惜当时没有想出来。。)
Code
- /**
- * Codeforces
- * Problem#832D
- * Accepted
- * Time:1466ms
- * Memory:14200k
- */
- #include <iostream>
- #include <cstdio>
- #include <ctime>
- #include <cmath>
- #include <cctype>
- #include <cstring>
- #include <cstdlib>
- #include <fstream>
- #include <sstream>
- #include <algorithm>
- #include <map>
- #include <set>
- #include <stack>
- #include <queue>
- #include <vector>
- #include <stack>
- #ifndef WIN32
- #define Auto "%lld"
- #else
- #define Auto "%I64d"
- #endif
- using namespace std;
- typedef bool boolean;
- const signed int inf = (signed)((1u << ) - );
- const signed long long llf = (signed long long)((1ull << ) - );
- const double eps = 1e-;
- const int binary_limit = ;
- #define smin(a, b) a = min(a, b)
- #define smax(a, b) a = max(a, b)
- #define max3(a, b, c) max(a, max(b, c))
- #define min3(a, b, c) min(a, min(b, c))
- template<typename T>
- inline boolean readInteger(T& u){
- char x;
- int aFlag = ;
- while(!isdigit((x = getchar())) && x != '-' && x != -);
- if(x == -) {
- ungetc(x, stdin);
- return false;
- }
- if(x == '-'){
- x = getchar();
- aFlag = -;
- }
- for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
- ungetc(x, stdin);
- u *= aFlag;
- return true;
- }
- #define LL int
- #define lowbit(x) (x & (-x))
- typedef class IndexedTree {
- public:
- LL* a;
- int s;
- IndexedTree():a(NULL), s() { }
- IndexedTree(int n):s(n) {
- a = new LL[(n + )];
- memset(a, , sizeof(LL) * (n + ));
- }
- inline void add(int idx, LL val) {
- for(; idx <= s; idx += lowbit(idx))
- a[idx] += val;
- }
- inline LL getSum(int idx) {
- LL rt = ;
- for(; idx; idx -= lowbit(idx))
- rt += a[idx];
- return rt;
- }
- }IndexedTree;
- typedef class SegmentableIndexedTree {
- public:
- IndexedTree s; // sum
- IndexedTree us; // unique sum
- stack< pair<int, int> > bs;
- SegmentableIndexedTree() { }
- SegmentableIndexedTree(int n) {
- s = IndexedTree(n + );
- us = IndexedTree(n + );
- }
- inline void add(int l, int r, LL val) {
- s.add(l, val), us.add(l, l * val);
- s.add(r + , -val), us.add(r + , -(r + ) * val);
- if(val > )
- bs.push(pair<int, int>(l, r));
- }
- inline LL getSum(int idx) {
- return (idx + ) * s.getSum(idx) - us.getSum(idx);
- }
- inline LL getSum(int l, int r) {
- return getSum(r) - getSum(l - );
- }
- inline void reset() {
- pair<int, int> p;
- while(!bs.empty()) {
- p = bs.top();
- bs.pop();
- add(p.first, p.second, -);
- }
- }
- }SegmentableIndexedTree;
- const int maxn = 1e5 + ;
- int n, m;
- vector<int> g[maxn];
- inline void init() {
- readInteger(n);
- readInteger(m);
- for(int i = , x; i < n; i++) {
- readInteger(x);
- g[x].push_back(i + );
- }
- }
- int cnt = ;
- int visitID[maxn], size[maxn], fa[maxn];
- int zson[maxn], dep[maxn], top[maxn];
- SegmentableIndexedTree st;
- void dfs1(int node, int last) {
- dep[node] = dep[last] + ;
- size[node] = ;
- fa[node] = last;
- int maxs = , maxid = ;
- for(int i = ; i < (signed)g[node].size(); i++) {
- int& e = g[node][i];
- if(e == last) continue;
- dfs1(e, node);
- size[node] += size[e];
- if(size[e] > maxs) maxs = size[e], maxid = e;//if(siz[son[u]] < siz[v]) son[u] = v;
- }
- zson[node] = maxid;
- }
- void dfs2(int node, int last, boolean iszson) {
- top[node] = (iszson) ? (top[last]) : (node);
- visitID[node] = ++cnt;
- if(zson[node] != ) dfs2(zson[node], node, true);
- for(int i = ; i < (signed)g[node].size(); i++) {
- int& e = g[node][i];
- if(e == last || e == zson[node]) continue;
- dfs2(e, node, false);
- }
- }
- void update(int a, int b){
- // cout << st.query(st.root, 1, n, 1, n) << endl;
- while(top[a] != top[b]){
- int& d = (dep[top[a]] > dep[top[b]]) ? (a) : (b);
- st.add(visitID[top[d]], visitID[d], );
- d = fa[top[d]];
- }
- if(visitID[a] > visitID[b]) swap(a, b);
- st.add(visitID[a], visitID[b], );
- // cout << st.query(st.root, 1, n, 1, n) << endl;
- }
- int query(int a, int b) {
- int ret = ;
- while(top[a] != top[b]){
- int& d = (dep[top[a]] > dep[top[b]]) ? (a) : (b);
- ret += st.getSum(visitID[top[d]], visitID[d]);
- d = fa[top[d]];
- }
- if(visitID[a] > visitID[b]) swap(a, b);
- ret += st.getSum(visitID[a], visitID[b]);
- // cout << ret << endl;
- // cout << "============================" << endl;
- return ret;
- }
- int query(int s, int t, int f) {
- st.reset();
- update(s, f);
- return query(t, f);
- }
- inline void solve() {
- int a, b, c;
- dfs1(, );
- dfs2(, , false);
- st = SegmentableIndexedTree(n);
- // for(int i = 1; i <= n; i++)
- // cout << zson[i] << " ";
- while(m--) {
- readInteger(a);
- readInteger(b);
- readInteger(c);
- int res = query(a, b, c);
- smax(res, query(b, a, c));
- smax(res, query(b, c, a));
- smax(res, query(b, a, c));
- smax(res, query(c, a, b));
- smax(res, query(c, b, a));
- printf("%d\n", res);
- }
- }
- int main() {
- init();
- solve();
- return ;
- }
- /**
- * Codeforces
- * Problem#832D
- * Accepted
- * Time:1466ms
- * Memory:14200k
- */
- #include <iostream>
- #include <cstdio>
- #include <ctime>
- #include <cmath>
- #include <cctype>
- #include <cstring>
- #include <cstdlib>
- #include <fstream>
- #include <sstream>
- #include <algorithm>
- #include <map>
- #include <set>
- #include <stack>
- #include <queue>
- #include <vector>
- #include <stack>
- #ifndef WIN32
- #define Auto "%lld"
- #else
- #define Auto "%I64d"
- #endif
- using namespace std;
- typedef bool boolean;
- const signed int inf = (signed)((1u << ) - );
- const signed long long llf = (signed long long)((1ull << ) - );
- const double eps = 1e-;
- const int binary_limit = ;
- #define smin(a, b) a = min(a, b)
- #define smax(a, b) a = max(a, b)
- #define max3(a, b, c) max(a, max(b, c))
- #define min3(a, b, c) min(a, min(b, c))
- template<typename T>
- inline boolean readInteger(T& u){
- char x;
- int aFlag = ;
- while(!isdigit((x = getchar())) && x != '-' && x != -);
- if(x == -) {
- ungetc(x, stdin);
- return false;
- }
- if(x == '-'){
- x = getchar();
- aFlag = -;
- }
- for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
- ungetc(x, stdin);
- u *= aFlag;
- return true;
- }
- #define LL int
- #define lowbit(x) (x & (-x))
- typedef class IndexedTree {
- public:
- LL* a;
- int s;
- IndexedTree():a(NULL), s() { }
- IndexedTree(int n):s(n) {
- a = new LL[(n + )];
- memset(a, , sizeof(LL) * (n + ));
- }
- inline void add(int idx, LL val) {
- for(; idx <= s; idx += lowbit(idx))
- a[idx] += val;
- }
- inline LL getSum(int idx) {
- LL rt = ;
- for(; idx; idx -= lowbit(idx))
- rt += a[idx];
- return rt;
- }
- }IndexedTree;
- typedef class SegmentableIndexedTree {
- public:
- IndexedTree s; // sum
- IndexedTree us; // unique sum
- stack< pair<int, int> > bs;
- SegmentableIndexedTree() { }
- SegmentableIndexedTree(int n) {
- s = IndexedTree(n + );
- us = IndexedTree(n + );
- }
- inline void add(int l, int r, LL val) {
- s.add(l, val), us.add(l, l * val);
- s.add(r + , -val), us.add(r + , -(r + ) * val);
- if(val > )
- bs.push(pair<int, int>(l, r));
- }
- inline LL getSum(int idx) {
- return (idx + ) * s.getSum(idx) - us.getSum(idx);
- }
- inline LL getSum(int l, int r) {
- return getSum(r) - getSum(l - );
- }
- inline void reset() {
- pair<int, int> p;
- while(!bs.empty()) {
- p = bs.top();
- bs.pop();
- add(p.first, p.second, -);
- }
- }
- }SegmentableIndexedTree;
- const int maxn = 1e5 + ;
- int n, m;
- vector<int> g[maxn];
- inline void init() {
- readInteger(n);
- readInteger(m);
- for(int i = , x; i < n; i++) {
- readInteger(x);
- g[x].push_back(i + );
- }
- }
- int cnt = ;
- int visitID[maxn], size[maxn], fa[maxn];
- int zson[maxn], dep[maxn], top[maxn];
- SegmentableIndexedTree st;
- void dfs1(int node, int last) {
- dep[node] = dep[last] + ;
- size[node] = ;
- fa[node] = last;
- int maxs = , maxid = ;
- for(int i = ; i < (signed)g[node].size(); i++) {
- int& e = g[node][i];
- if(e == last) continue;
- dfs1(e, node);
- size[node] += size[e];
- if(size[e] > maxs) maxs = size[e], maxid = e;//if(siz[son[u]] < siz[v]) son[u] = v;
- }
- zson[node] = maxid;
- }
- void dfs2(int node, int last, boolean iszson) {
- top[node] = (iszson) ? (top[last]) : (node);
- visitID[node] = ++cnt;
- if(zson[node] != ) dfs2(zson[node], node, true);
- for(int i = ; i < (signed)g[node].size(); i++) {
- int& e = g[node][i];
- if(e == last || e == zson[node]) continue;
- dfs2(e, node, false);
- }
- }
- void update(int a, int b){
- // cout << st.query(st.root, 1, n, 1, n) << endl;
- while(top[a] != top[b]){
- int& d = (dep[top[a]] > dep[top[b]]) ? (a) : (b);
- st.add(visitID[top[d]], visitID[d], );
- d = fa[top[d]];
- }
- if(visitID[a] > visitID[b]) swap(a, b);
- st.add(visitID[a], visitID[b], );
- // cout << st.query(st.root, 1, n, 1, n) << endl;
- }
- int query(int a, int b) {
- int ret = ;
- while(top[a] != top[b]){
- int& d = (dep[top[a]] > dep[top[b]]) ? (a) : (b);
- ret += st.getSum(visitID[top[d]], visitID[d]);
- d = fa[top[d]];
- }
- if(visitID[a] > visitID[b]) swap(a, b);
- ret += st.getSum(visitID[a], visitID[b]);
- // cout << ret << endl;
- // cout << "============================" << endl;
- return ret;
- }
- int query(int s, int t, int f) {
- st.reset();
- update(s, f);
- return query(t, f);
- }
- inline void solve() {
- int a, b, c;
- dfs1(, );
- dfs2(, , false);
- st = SegmentableIndexedTree(n);
- // for(int i = 1; i <= n; i++)
- // cout << zson[i] << " ";
- while(m--) {
- readInteger(a);
- readInteger(b);
- readInteger(c);
- int res = query(a, b, c);
- smax(res, query(b, a, c));
- smax(res, query(b, c, a));
- smax(res, query(b, a, c));
- smax(res, query(c, a, b));
- smax(res, query(c, b, a));
- printf("%d\n", res);
- }
- }
- int main() {
- init();
- solve();
- return ;
- }
Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组的更多相关文章
- 【 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) 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) Misha, Grisha and Underground(LCA)
Misha, Grisha and Underground time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维
& -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...
- 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) - D
题目链接:http://codeforces.com/contest/832/problem/D 题意:给定一棵n个点的树,然后给你q个询问,每个询问为三元组(a,b,c),问你从这三个点中选取一个作 ...
- Codeforces Round #425 (Div. 2) D.Misha, Grisha and Underground
我奇特的脑回路的做法就是 树链剖分 + 树状数组 树状数组是那种 区间修改,区间求和,还有回溯的 当我看到别人写的是lca,直接讨论时,感觉自己的智商收到了碾压... #include<cmat ...
随机推荐
- Appium环境搭建(一)
python环境做测试,需要准备工具如下: 1.python2.7(这里使用的是python2你也可以选更高版本) 2.Appium(Window版) 3.Android SDK 4.Appium_P ...
- Spring.之.报错:Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWo ...
- Hello world!(内含自己编写的C语言二叉树同学录)
修改:刷了一段时间的题,水平渐涨,发现同学录真的要做成市面可行的应用的话,应该按学号建立二叉平衡树,红黑树是一个可行的选择. 在同学的推荐下,来到博客园来找志同道合的人交流代码.3个月后参加蓝桥杯 ...
- python 定义函数 调用函数
创建test.py文件 #coding=utf-8 #定义函数 def hello(): print "hello world" #调用函数 hello() 在cmd下面运行
- 提示“此Flash Player与您的地区不相容,请重新安装Flash”的解决办法
问题原因: 因为Flash相对于HTML5,有着运算效率低.资源占用大.安全性不高等缺点,随着HTML5越来越普及,Adobe已宣布2020年正式停止支持Flash这项技术. 但Adobe公司为了利益 ...
- git分支流
## 新建一个iss1分支 $ git branch iss1 ## 切换到iss1分支 $ git checkout iss1 Switched to branch 'iss1' ## 查看分支,当 ...
- sitecore系统教程之体验编辑器
体验编辑器是一个WYSIWYG编辑器,允许您直接在页面上轻松更改项目.您可以编辑页面上可见的所有项目 - 文本,图形,徽标,链接等. 在体验编辑器中,您可以编辑当前项目的字段以及页面上呈现的任何项目的 ...
- [转]LoadRunner 各个指标分析
转载:https://www.cnblogs.com/dvbbs2012/p/4073635.html 我们要监视CPU,内存.硬盘的资源情况.得到以下的参数提供分析的依据.%processor ti ...
- Linux基础命令---文本编辑ex
ex ex会启动vim编辑器,它的执行效果和vim –E相同.从ex模式回到普通模式,可以在vim中输入:vim. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.op ...
- tomcat2章1
package ex02.pyrmont; import java.io.File; public class Constants { public static final String WEB_R ...