Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状
Posted 阿波罗2003
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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就怪了)。
Code
1 /** 2 * Codeforces 3 * Problem#832D 4 * Accepted 5 * Time:1466ms 6 * Memory:14200k 7 */ 8 #include <iostream> 9 #include <cstdio> 10 #include <ctime> 11 #include <cmath> 12 #include <cctype> 13 #include <cstring> 14 #include <cstdlib> 15 #include <fstream> 16 #include <sstream> 17 #include <algorithm> 18 #include <map> 19 #include <set> 20 #include <stack> 21 #include <queue> 22 #include <vector> 23 #include <stack> 24 #ifndef WIN32 25 #define Auto "%lld" 26 #else 27 #define Auto "%I64d" 28 #endif 29 using namespace std; 30 typedef bool boolean; 31 const signed int inf = (signed)((1u << 31) - 1); 32 const signed long long llf = (signed long long)((1ull << 61) - 1); 33 const double eps = 1e-9; 34 const int binary_limit = 256; 35 #define smin(a, b) a = min(a, b) 36 #define smax(a, b) a = max(a, b) 37 #define max3(a, b, c) max(a, max(b, c)) 38 #define min3(a, b, c) min(a, min(b, c)) 39 template<typename T> 40 inline boolean readInteger(T& u){ 41 char x; 42 int aFlag = 1; 43 while(!isdigit((x = getchar())) && x != ‘-‘ && x != -1); 44 if(x == -1) { 45 ungetc(x, stdin); 46 return false; 47 } 48 if(x == ‘-‘){ 49 x = getchar(); 50 aFlag = -1; 51 } 52 for(u = x - ‘0‘; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - ‘0‘); 53 ungetc(x, stdin); 54 u *= aFlag; 55 return true; 56 } 57 58 #define LL int 59 #define lowbit(x) (x & (-x)) 60 61 typedef class IndexedTree { 62 public: 63 LL* a; 64 int s; 65 IndexedTree():a(NULL), s(0) { } 66 IndexedTree(int n):s(n) { 67 a = new LL[(n + 1)]; 68 memset(a, 0, sizeof(LL) * (n + 1)); 69 } 70 71 inline void add(int idx, LL val) { 72 for(; idx <= s; idx += lowbit(idx)) 73 a[idx] += val; 74 } 75 76 inline LL getSum(int idx) { 77 LL rt = 0; 78 for(; idx; idx -= lowbit(idx)) 79 rt += a[idx]; 80 return rt; 81 } 82 }IndexedTree; 83 84 typedef class SegmentableIndexedTree { 85 public: 86 IndexedTree s; // sum 87 IndexedTree us; // unique sum 88 stack< pair<int, int> > bs; 89 90 SegmentableIndexedTree() { } 91 SegmentableIndexedTree(int n) { 92 s = IndexedTree(n + 1); 93 us = IndexedTree(n + 1); 94 } 95 96 inline void add(int l, int r, LL val) { 97 s.add(l, val), us.add(l, l * val); 98 s.add(r + 1, -val), us.add(r + 1, -(r + 1) * val); 99 if(val > 0) 100 bs.push(pair<int, int>(l, r)); 101 } 102 103 inline LL getSum(int idx) { 104 return (idx + 1) * s.getSum(idx) - us.getSum(idx); 105 } 106 107 inline LL getSum(int l, int r) { 108 return getSum(r) - getSum(l - 1); 109 } 110 111 inline void reset() { 112 pair<int, int> p; 113 while(!bs.empty()) { 114 p = bs.top(); 115 bs.pop(); 116 add(p.first, p.second, -1); 117 } 118 } 119 }SegmentableIndexedTree; 120 121 const int maxn = 1e5 + 5; 122 int n, m; 123 vector<int> g[maxn]; 124 125 inline void init() { 126 readInteger(n); 127 readInteger(m); 128 for(int i = 1, x; i < n; i++) { 129 readInteger(x); 130 g[x].push_back(i + 1); 131 } 132 } 133 134 int cnt = 0; 135 int visitID[maxn], size[maxn], fa[maxn]; 136 int zson[maxn], dep[maxn], top[maxn]; 137 SegmentableIndexedTree st; 138 139 void dfs1(int node, int last) { 140 dep[node] = dep[last] + 1; 141 size[node] = 1; 142 fa[node] = last; 143 int maxs = 0, maxid = 0; 144 for(int i = 0; i < (signed)g[node].size(); i++) { 145 int& e = g[node][i]; 146 if(e == last) continue; 147 dfs1(e, node); 148 size[node] += size[e]; 149 if(size[e] > maxs) maxs = size[e], maxid = e;//if(siz[son[u]] < siz[v]) son[u] = v; 150 } 151 zson[node] = maxid; 152 } 153 154 void dfs2(int node, int last, boolean iszson) { 155 top[node] = (iszson) ? (top[last]) : (node); 156 visitID[node] = ++cnt; 157 if(zson[node] != 0) dfs2(zson[node], node, true); 158 for(int i = 0; i < (signed)g[node].size(); i++) { 159 int& e = g[node][i]; 160 if(e == last || e == zson[node]) continue; 161 dfs2(e, node, false); 162 } 163 } 164 165 void update(int a, int b){ 166 // cout << st.query(st.root, 1, n, 1, n) << endl; 167 while(top[a] != top[b]){ 168 int& d = (dep[top[a]] > dep[top[b]]) ? (a) : (b); 169 st.add(visitID[top[d]], visitID[d], 1); 170 d = fa[top[d]]; 171 } 172 if(visitID[a] > visitID[b]) swap(a, b); 173 st.add(visitID[a], visitID[b], 1); 174 // cout << st.query(st.root, 1, n, 1, n) << endl; 175 } 176 177 int query(int a, int b) { 178 int ret = 0; 179 while(top[a] != top[b]){ 180 int& d = (dep[top[a]] > dep[top[b]]) ? (a) : (b); 181 ret += st.getSum(visitID[top[d]], visitID[d]); 182 d = fa[top[d]]; 183 } 184 if(visitID[a] > visitID[b]) swap(a, b); 185 ret += st.getSum(visitID[a], visitID[b]); 186 // cout << ret << endl; 187 // cout << "============================" << endl; 188 return ret; 189 } 190 191 int query(int s, int t, int f) { 192 st.reset(); 193 update(s, f); 194 return query(t, f); 195 } 196 197 inline void solve() { 198 int a, b, c; 199 dfs1(1, 0); 200 dfs2(1, 0, false); 201 st = SegmentableIndexedTree(n); 202 // for(int i = 1; i <= n; i++) 203 // cout << zson[i] << " "; 204 while(m--) { 205 readInteger(a); 206 readInteger(b); 207 readInteger(c); 208 int res = query(a, b, c); 209 smax(res, query(b, a, c)); 210 smax(res, query(b, c, a)); 211 smax(res, query(b, a, c)); 212 smax(res, query(c, a, b)); 213 smax(res, query(c, b, a)); 214 printf("%d\n", res); 215 } 216 } 217 218 int main() { 219 init(); 220 solve(); 221 return 0; 222 }
1 /** 2 * Codeforces 3 * Problem#832D 4 * Accepted 5 * Time:1466ms 6 * Memory:14200k 7 */ 8 #include <iostream> 9 #include <cstdio> 10 #include <ctime> 11 #include <cmath> 12 #include <cctype> 13 #include <cstring> 14 #include <cstdlib> 15 #include <fstream> 16 #include <sstream> 17 #include <algorithm> 18 #include <map> 19 #include <set> 20 #include <stack> 21 #include <queue> 22 #include <vector> 23 #include <stack> 24 #ifndef WIN32 25 #define Auto "%lld" 26 #else 27 #define Auto "%I64d" 28 #endif 29 using namespace std; 30 typedef bool boolean; 31 const signed int inf = (signed)((1u << 31) - 1); 32 const signed long long llf = (signed long long)((1ull << 61) - 1); 33 const double eps = 1e-9; 34 const int binary_limit = 256; 35 #define smin(a, b) a = min(a, b) 36 #define smax(a, b) a = max(a, b) 37 #define max3(a, b, c) max(a, max(b, c)) 38 #define min3(a, b, c) min(a, min(b, c)) 39 template<typename T> 40 inline boolean readInteger(T& u){ 41 char x; 42 int aFlag = 1; 43 while(!isdigit((x = getchar())) && x != ‘-‘ && x != -1); 44 if(x == -1) { 45 ungetc(x, stdin); 46 return false; 47 } 48 if(x == ‘-‘){ 49 x = getchar(); 50 aFlag = -1; 51 } 52 for(u = x - ‘0‘; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - ‘0‘); 53 ungetc(x, stdin); 54 u *= aFlag; 55 return true; 56 } 57 58 #define LL int 59 #define lowbit(x) (x & (-x)) 60 61 typedef class IndexedTree { 62 public: 63 LL* a; 64 int s; 65 IndexedTree():a(NULL), s(0) { } 66 IndexedTree(int n):s(n) { 67 a = new LL[(n + 1)]; 68 memset(a, 0, sizeof(LL) * (n + 1)); 69 } 70 71 inline void add(int idx, LL val) { 72 for(; idx <= s; idx += lowbit(idx)) 73 a[idx] += val; 74 } 75 76 inline LL getSum(int idx) { 77 LL rt = 0; 78 for(; idx; idx -= lowbit(idx)) 79 rt += a[idx]; 80 return rt; 81 } 82 }IndexedTree; 83 84 typedef class SegmentableIndexedTree { 85 public: 86 IndexedTree s; // sum 87 IndexedTree us; // unique sum 88 stack< pair<int, int> > bs; 89 90 SegmentableIndexedTree() { } 91 SegmentableIndexedTree(int n) { 92 s = IndexedTree(n + 1); 93 us = IndexedTree(n + 1); 94 } 95 96 inline void add(int l, int r, LL val) { 97 s.add(l, val), us.add(l, l * val); 98 s.add(r + 1, -val), us.add(r + 1, -(r + 1) * val); 99 if(val > 0) 100 bs.push(pair<int, int>(l, r)); 101 } 102 103 inline LL getSum(int idx) { 104 return (idx + 1) * s.getSum(idx) - us.getSum(idx); 105 } 106 107 inline LL getSum(int l, int r) { 108 return getSum(r) - getSum(l - 1); 109 } 110 111 inline void reset() { 112 pair<int, int> p; 113 while(!bs.empty()) { 114 p = bs.top(); 115 bs.pop(); 116 add(p.first, p.second, -1); 117 } 118 } 119 }SegmentableIndexedTree; 120 121 const int maxn = 1e5 + 5; 122 int n, m; 123 vector<int> g[maxn]; 124 125 inline void init() { 126 readInteger(n); 127 readInteger(m); 128 for(int i = 1, x; i < n; i++) { 129 readInteger(x); 130 g[x].push_back(i + 1); 131 } 132 } 133 134 int cnt = 0; 135 int visitID[maxn], size[maxn], fa[maxn]; 136 int zson[maxn], dep[maxn], top[maxn]; 137 SegmentableIndexedTree st; 138 139 void dfs1(int node, int last) { 140 dep[node] = dep[last] + 1; 141 size[node] = 1; 142 fa[node] = last; 143 int maxs = 0, maxid = 0; 144 for(int i = 0; i < (signed)g[node].size(); i++) { 145 int& e = g[node][i]; 146 if(e == last) continue; 147 dfs1(e, node); 148 size[node] += size[e]; 149 if(size[e] > maxs) maxs = size[e], maxid = e;//if(siz[son[u]] < siz[v]) son[u] = v; 150 } 151 zson[node] = maxid; 152 } 153 154 void dfs2(int node, int last, boolean iszson) { 155 top[node] = (iszson) ? (top[last]) : (node); 156 visitID[node] = ++cnt; 157 if(zson[node] != 0) dfs2(zson[node], node, true); 158 for(int i = 0; i < (signed)g[node].size(); i++) { 159 int& e = g[node][i]; 160 if(e == last || e == zson[node]) continue; 161 dfs2(e, node, false); 162 } 163 } 164 165 void update(int a, int b){ 166 // cout << st.query(st.root, 1, n, 1, n) << endl; 167 while(top[a] != top[b]){ 168 int& d = (dep[top[a]] > dep[top[b]]) ? (a) : (b); 169 st.add(visitID[top[d]], visitID[d], 1); 170 d = fa[top[d]]; 171 } 172 if(visitID[a] > visitID[b]) swap(a, b); 173 st.add(visitID[a], visitID[b], 1); 174 // cout << st.query(st.root, 1, n, 1, n) << endl; 175 } 176 177 int query(int a, int b) { 178 int ret = 0; 179 while(top[a] != top[b]){ 180 int& d = (dep[top[a]] > dep[top[b]]) ? (a) : (b); 181 ret += st.getSum(visitID[top[d]], visitID[d]); 182 d = fa[top[d]]; 183 } 184 if(visitID[a] > visitID[b]) swap(a, b); 185 ret += st.getSum(visitID[a], visitID[b]); 186 // cout << ret << endl; 187 // cout << "============================" << endl; 188 return ret; 189 } 190 191 int query(int s, int t, int f) { 192 st.reset(); 193 update(s, f); 194 return query(t, f); 195 } 196 197 inline void solve() { 198 int a, b, c; 199 dfs1(1, 0); 200 dfs2(1, 0, false); 201 st = SegmentableIndexedTree(n); 202 // for(int i = 1; i <= n; i++) 203 // cout << zson[i] << " "; 204 while(m--) { 205 readInteger(a); 206 readInteger(b); 207 readInteger(c); 208 int res = query(a, b, c); 209 smax(res, query(b, a, c)); 210 smax(res, query(b, c, a)); 211 smax(res, query(b, a, c)); 212 smax(res, query(c, a, b)); 213 smax(res, query(c, b, a)); 214 printf("%d\n", res); 215 } 216 } 217 218 int main() { 219 init(); 220 solve(); 221 return 0; 222 }
!-- h3>
以上是关于Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论
Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks
Codeforces Round #425 (Div. 2) Problem B Petya and Exam - 暴力
Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状