1. 程式人生 > >GDSOI2017 中學生資料結構題(Lct練習)

GDSOI2017 中學生資料結構題(Lct練習)

題目大意:

給出一棵樹。
要求維護:一條路徑上的點權和。
修改1:一條路徑上的點的點權全部加上一個數。
修改2:shift
假設一個路徑上的點是ax,ak1,ak2,…ay
就把ax的點權放到ak1上,把ak1的點權放到ak2上,……
把ay的點權放到ax上。

吹水:

我連去SOI的資格都沒有……
當時去聽講,不會splay,不會lct,對映是什麼也聽不懂,處於呆滯狀態。
大佬說是他出的題sone0的子集(%%%)

回來之後,跟著車隊把splay學了。
發現這題樹鏈剖分套splay挺“好”做的。
於是車隊用這個方法紛紛以5000+bytes過了。
但是我沒有打,因為我那會兒沒有時間。

現在我學了lct,於是找了這題來練練手。

題解:

lct中,splay把整棵樹分成了若干部分,我們可以把每個splay再對映一顆splay,對映這棵splay維護的就是權值。
這兩個splay的大小一定相同,但是形態不一定相同。
並且如果一個點x在第一棵splay中的位置是k,那麼它所對應的權值就在第二棵splay的第k個位置。
如果要shift操作,我們只用在第二棵splay中弄就好了。

操作不是重點,重點是我們如何在lct的access和makeroot中使兩顆splay是對應的。

我們可以在每一棵第一棵splay的記錄根記錄這棵splay所對映的那棵splay的根是什麼。
在rotate操作中,維護一下這個東西。
注意在各個操作中,這個東西都可能會發生改變,一定要維護好。

要找到x映射出來的是哪個點,就先把x旋到所在splay的根,對映的splay要維護子樹的大小,然後就可以遞迴找。

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define fo(i, x, y) for(int i = x; i <= y; i ++)
using namespace std;

const int Maxn = 200005;
int dd[Maxn], fa[2][Maxn], t[2][Maxn][2], pf[2
][Maxn], cas[2][Maxn]; struct node { ll siz, sum, rev, bz, z; }a[2][Maxn]; int lr(int o, int x) {return t[o][fa[o][x]][1] == x;} void fan(int o, int x) { if(!x) return; swap(t[o][x][0], t[o][x][1]); a[o][x].rev ^= 1; } void add(int o, int x, ll y) { if(!x) return; a[o][x].z += y; a[o][x].sum += a[o][x].siz * y; a[o][x].bz += y; } void down(int o, int x) { if(!x) return; if(a[o][x].rev) fan(o, t[o][x][0]), fan(o, t[o][x][1]), a[o][x].rev = 0; if(a[o][x].bz) add(o, t[o][x][0], a[o][x].bz), add(o, t[o][x][1], a[o][x].bz), a[o][x].bz = 0; } void update(int o, int x) { if(!x) return; a[o][x].siz = a[o][t[o][x][0]].siz + a[o][t[o][x][1]].siz + 1; a[o][x].sum = a[o][t[o][x][0]].sum + a[o][t[o][x][1]].sum + a[o][x].z; } void rotate(int o, int x) { int y = fa[o][x], k = lr(o, x); t[o][y][k] = t[o][x][!k]; if(t[o][x][!k]) fa[o][t[o][x][!k]] = y; fa[o][x] = fa[o][y]; if(fa[o][y]) t[o][fa[o][y]][lr(o, y)] = x; t[o][x][!k] = y; fa[o][y] = x; pf[o][x] = pf[o][y]; cas[o][x] = cas[o][y]; update(o, y); update(o, x); } void xc(int o, int x) { for(; x; x = fa[o][x]) dd[++ dd[0]] = x; for(; dd[0]; dd[0] --) down(o, dd[dd[0]]); } void splay(int o, int x, int y) { xc(o, x); while(fa[o][x] != y) { if(fa[o][fa[o][x]] != y) if(lr(o, x) == lr(o, fa[o][x])) rotate(o, fa[o][x]); else rotate(o, x); rotate(o, x); } } int dfs(int x, int y) { down(1, x); if(a[1][t[1][x][0]].siz + 1 == y) return x; if(a[1][t[1][x][0]].siz >= y) return dfs(t[1][x][0], y); dfs(t[1][x][1], y - a[1][t[1][x][0]].siz - 1); } int num(int x) { if(x == 0) return 0; splay(0, x, 0); return dfs(cas[0][x], a[0][t[0][x][0]].siz + 1); } void access(int x) { for(int y = 0; x; update(0, x), y = x, x = pf[0][x]) { int yz = num(y), xz = num(x); fa[0][t[0][x][1]] = 0; pf[0][t[0][x][1]] = x; splay(1, xz, 0); cas[0][t[0][x][1]] = t[1][xz][1]; fa[1][t[1][xz][1]] = 0; fa[0][y] = x; t[0][x][1] = y; fa[1][yz] = xz; t[1][xz][1] = yz; update(1, xz); cas[0][x] = xz; } } void makeroot(int x) { access(x); splay(0, x, 0); int xz = num(x); splay(1, xz, 0); fan(0, x); fan(1, xz); cas[0][x] = xz; } void link(int x, int y) { makeroot(x); pf[0][x] = y; } int Q, n, x, y, z; char s[10]; int main() { freopen("shift.in", "r", stdin); freopen("shift.out", "w", stdout); scanf("%d", &n); fo(i, 1, n) a[0][i].siz = a[1][i].siz = 1, cas[0][i] = i; fo(i, 1, n - 1) scanf("%d %d", &x, &y), link(x, y); for(scanf("%d", &Q); Q; Q --) { scanf("%s", s + 1); if(s[1] == 'A') { scanf("%d %d %d", &x, &y, &z); int yz = num(y); makeroot(x); access(y); splay(0, y, 0); splay(1, yz, 0); add(1, yz, z); cas[0][y] = yz; } else if(s[1] == 'S') { scanf("%d %d", &x, &y); if(x == y) continue; swap(x, y); int xz = num(x), yz = num(y); makeroot(x); access(y); splay(0, y, 0); splay(1, yz, 0); splay(1, xz, yz); xc(1, xz); t[1][fa[1][xz]][0] = t[1][xz][1]; if(t[1][xz][1]) fa[1][t[1][xz][1]] = fa[1][xz]; t[1][xz][1] = 0; update(1, xz); fa[1][xz] = yz; t[1][yz][1] = xz; update(1, yz); cas[0][y] = yz; } else { scanf("%d %d", &x, &y); int yz = num(y); makeroot(x); access(y); splay(0, y, 0); splay(1, yz, 0); printf("%lld\n", a[1][yz].sum); cas[0][y] = yz; } } }