[国家集训队]旅游
Posted mrclr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[国家集训队]旅游相关的知识,希望对你有一定的参考价值。
这其实就是一道树剖板子题,只不过就是写的长了一点。
还是叨叨几点吧:
1.区间取相反数:开一个标记数组,每一次亦或1,然后对应的sum取相反数,Max, Min交换,并且取相反数。
2.题目中给的是边权,但要转化成点权:这条边的边权转化成儿子节点的点权,然后每一次链上操作的时候,x, y的LCA不要管。实现的时候就是当x,y跳到同一条链的时候,对[dfsx[y] + 1, dfsx[x]](假设dfsx[y] < dfsx[x])操作即可,因为同一条链上的dfs序是连续的。
耐着性子写完吧~
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cctype> 8 #include<vector> 9 #include<stack> 10 #include<queue> 11 using namespace std; 12 #define enter puts("") 13 #define space putchar(‘ ‘) 14 #define Mem(a, x) memset(a, x, sizeof(a)) 15 #define rg register 16 typedef long long ll; 17 typedef double db; 18 const int INF = 0x3f3f3f3f; 19 const db eps = 1e-8; 20 const int maxn = 1e5 + 5; 21 inline ll read() 22 { 23 ll ans = 0; 24 char ch = getchar(), last = ‘ ‘; 25 while(!isdigit(ch)) {last = ch; ch = getchar();} 26 while(isdigit(ch)) {ans = ans * 10 + ch - ‘0‘; ch = getchar();} 27 if(last == ‘-‘) ans = -ans; 28 return ans; 29 } 30 inline void write(ll x) 31 { 32 if(x < 0) x = -x, putchar(‘-‘); 33 if(x >= 10) write(x / 10); 34 putchar(x % 10 + ‘0‘); 35 } 36 37 int n, m, a[maxn]; 38 39 vector<int> v[maxn], c[maxn]; 40 bool vis[maxn]; 41 int dep[maxn], fa[maxn], siz[maxn], son[maxn]; 42 void dfs1(int now) 43 { 44 vis[now] = 1; siz[now] = 1; 45 for(int i = 0; i < (int)v[now].size(); ++i) 46 { 47 if(!vis[v[now][i]]) 48 { 49 a[v[now][i]] = c[now][i]; 50 dep[v[now][i]] = dep[now] + 1; 51 fa[v[now][i]] = now; 52 dfs1(v[now][i]); 53 siz[now] += siz[v[now][i]]; 54 if(!son[now] || siz[son[now]] < siz[v[now][i]]) son[now] = v[now][i]; 55 } 56 } 57 } 58 int dfsx[maxn], pos[maxn], top[maxn], cnt = 0; 59 void dfs2(int now) 60 { 61 vis[now] = 1; 62 dfsx[now] = ++cnt; pos[cnt] = now; 63 if(son[now]) 64 { 65 top[son[now]] = top[now]; 66 dfs2(son[now]); 67 } 68 for(int i = 0; i < (int)v[now].size(); ++i) 69 { 70 if(!vis[v[now][i]] && v[now][i] != son[now]) 71 { 72 top[v[now][i]] = v[now][i]; 73 dfs2(v[now][i]); 74 } 75 } 76 } 77 78 int l[maxn << 2], r[maxn << 2], sum[maxn << 2], Max[maxn << 2], Min[maxn << 2]; 79 int lzy[maxn << 2]; //相反数标记 80 void pushup(int now) 81 { 82 sum[now] = sum[now << 1] + sum[now << 1 | 1]; 83 Max[now] = max(Max[now << 1], Max[now << 1 | 1]); 84 Min[now] = min(Min[now << 1], Min[now << 1 | 1]); 85 } 86 void build(int L, int R, int now) 87 { 88 l[now] = L; r[now] = R; 89 if(L == R) {sum[now] = Max[now] = Min[now] = a[pos[L]]; return;} 90 int mid = (L + R) >> 1; 91 build(L, mid, now << 1); 92 build(mid + 1, R, now << 1 | 1); 93 pushup(now); 94 } 95 void pushdown(int now) 96 { 97 if(lzy[now]) 98 { 99 sum[now << 1] = -sum[now << 1]; sum[now << 1 | 1] = -sum[now << 1 | 1]; 100 swap(Max[now << 1], Min[now << 1]); swap(Max[now << 1 | 1], Min[now << 1 | 1]); 101 Max[now << 1] = -Max[now << 1]; Max[now << 1 | 1] = -Max[now << 1 | 1]; 102 Min[now << 1] = -Min[now << 1]; Min[now << 1 | 1] = -Min[now << 1 | 1]; 103 lzy[now << 1] ^= 1; lzy[now << 1 | 1] ^= 1; 104 lzy[now] = 0; 105 } 106 107 } 108 void update_sin(int now, int idx, int d) 109 { 110 if(l[now] == r[now]) 111 { 112 sum[now] = Max[now] = Min[now] = d; 113 return; 114 } 115 pushdown(now); 116 int mid = (l[now] + r[now]) >> 1; 117 if(idx <= mid) update_sin(now << 1, idx, d); 118 else update_sin(now << 1 | 1, idx, d); 119 pushup(now); 120 } 121 void update_opp(int L, int R, int now) 122 { 123 if(L == l[now] && R == r[now]) 124 { 125 sum[now] = -sum[now]; 126 swap(Max[now], Min[now]); 127 Max[now] = -Max[now]; 128 Min[now] = -Min[now]; 129 lzy[now] ^= 1; 130 return; 131 } 132 pushdown(now); 133 int mid = (l[now] + r[now]) >> 1; 134 if(R <= mid) update_opp(L, R, now << 1); 135 else if(L > mid) update_opp(L, R, now << 1 | 1); 136 else update_opp(L, mid, now << 1), update_opp(mid + 1, R, now << 1 | 1); 137 pushup(now); 138 } 139 int query_sum(int L, int R, int now) 140 { 141 if(L == l[now] && R == r[now]) return sum[now]; 142 pushdown(now); 143 int mid = (l[now] + r[now]) >> 1; 144 if(R <= mid) return query_sum(L, R, now << 1); 145 else if(L > mid) return query_sum(L, R, now << 1 | 1); 146 else return query_sum(L, mid, now << 1) + query_sum(mid + 1, R, now << 1 | 1); 147 } 148 int query_M(int L, int R, int now, bool flg) 149 { 150 if(L == l[now] && R == r[now]) return flg ? Max[now] : Min[now]; 151 pushdown(now); 152 int mid = (l[now] + r[now]) >> 1; 153 if(R <= mid) return query_M(L, R, now << 1, flg); 154 else if(L > mid) return query_M(L, R, now << 1 | 1, flg); 155 else 156 { 157 if(flg) return max(query_M(L, mid, now << 1, flg), query_M(mid + 1, R, now << 1 | 1, flg)); 158 else return min(query_M(L, mid, now << 1, flg), query_M(mid + 1, R, now << 1 | 1, flg)); 159 } 160 } 161 162 void update_path(int x, int y) 163 { 164 while(top[x] != top[y]) 165 { 166 if(dep[top[x]] < dep[top[y]]) swap(x, y); 167 update_opp(dfsx[top[x]], dfsx[x], 1); 168 x = fa[top[x]]; 169 } 170 if(dfsx[x] < dfsx[y]) swap(x, y); 171 if(x == y) return; 172 update_opp(dfsx[y] + 1, dfsx[x], 1); 173 } 174 int queryS_path(int x, int y) 175 { 176 int ret = 0; 177 while(top[x] != top[y]) 178 { 179 if(dep[top[x]] < dep[top[y]]) swap(x, y); 180 ret += query_sum(dfsx[top[x]], dfsx[x], 1); 181 x = fa[top[x]]; 182 } 183 if(dfsx[x] < dfsx[y]) swap(x, y); 184 if(x != y) ret += query_sum(dfsx[y] + 1, dfsx[x], 1); 185 return ret; 186 } 187 int queryM_path(int x, int y, int flg) 188 { 189 int ret = flg ? -INF : INF; 190 while(top[x] != top[y]) 191 { 192 if(dep[top[x]] < dep[top[y]]) swap(x, y); 193 if(flg) ret = max(ret, query_M(dfsx[top[x]], dfsx[x], 1, flg)); 194 else ret = min(ret, query_M(dfsx[top[x]], dfsx[x], 1, flg)); 195 x = fa[top[x]]; 196 } 197 if(dfsx[x] < dfsx[y]) swap(x, y); 198 if(x != y) 199 { 200 if(flg) ret = max(ret, query_M(dfsx[y] + 1, dfsx[x], 1, flg)); 201 else ret = min(ret, query_M(dfsx[y] + 1, dfsx[x], 1, flg)); 202 } 203 return ret; 204 } 205 206 char ch[5]; 207 208 int main() 209 { 210 n = read(); 211 for(int i = 1; i < n; ++i) 212 { 213 int x = read() + 1, y = read() + 1, co = read(); 214 v[x].push_back(y); c[x].push_back(co); 215 v[y].push_back(x); c[y].push_back(co); 216 } 217 dfs1(1); 218 Mem(vis, 0); top[1] = 1; dfs2(1); 219 build(1, cnt, 1); 220 m = read(); 221 for(int i = 1; i <= m; ++i) 222 { 223 scanf("%s", ch); int x = read() + 1, y = read() + 1; 224 if(ch[0] == ‘C‘) update_sin(1, dfsx[x], y - 1); 225 else if(ch[0] == ‘N‘) update_path(x, y); 226 else if(ch[0] == ‘S‘) write(queryS_path(x, y)), enter; 227 else if(ch[1] == ‘A‘) write(queryM_path(x, y, 1)), enter; 228 else write(queryM_path(x, y, 0)), enter; 229 } 230 return 0; 231 }
以上是关于[国家集训队]旅游的主要内容,如果未能解决你的问题,请参考以下文章