題目傳送門
題目大意:給你一棵樹,有三種操作,在兩個點之間連乙個傳送門,拆毀乙個已有的傳送門,詢問兩個點之間的合法路徑數量。一條合法路徑滿足 1.經過且僅經過乙個傳送門 2.不經過起點終點簡單路徑上的任何一條邊
這模型轉化好神啊
首先把樹拍成$dfs$序
問題是在樹上,我們把$x,y$這條鏈拎出來攤平,那麼鏈上每個點都掛了一些子樹。
容易發現合法路徑數=連線以$x,y$為根的子樹的傳送門數量
而無根樹並沒有「子樹」這一概念,所以先隨便挑乙個根跑出來dfs序。
發現「子樹」的$dfs$序一定是乙個或兩個連續的區間,我們分$x,y$是否為$lca$討論一下就可以了
然後把問題放到二維座標系上。
問題轉化成,動態在乙個二維平面內加入/刪除乙個點,以及查詢矩形內點的數量
由於存在修改操作,需要再加上一維。那麼整個問題變成了乙個三維偏序問題。用$cdq$分治+樹狀陣列即可
1 #include 2 #include 3 #include 4 #include 5#define n1 100005
6#define m1 500005
7#define ll long long
8#define uint unsigned int
9using
namespace
std;
1011 template void read(_t &ret)
1215
while(c>='
0'&&c<='9')
16 ret=ret*fh;17}
1819
struct
edge
23}e;
2425
intn;
2627
namespace
tree
38 ed[x]=cur;39}
40void
init()
4148
int lca(int x,int
y)49
60int lcb(int x,int
d)61
67};
6869
struct
bit77
int query(int
x)78
84void clr(int
x)85
90}bit;
9192
struct opop[m1],tmp[m1];
93int cmp2(op &s1,op &s2)
9499
intans[n1],que[m1],isquery[n1],tl;
100101
void cdq(int l,int
r)102
else
115}
116while(i<=m)
117while(j<=r)
118for(i=l;i<=r;i++) op[i]=tmp[i-l+1
];119
while(tl)
120}
121122
intm,q1,q2;
123using tree::st; using tree::ed; using tree::dep; using tree::lca; using
tree::lcb;
124125
intmain()
126; op[++m]=(op);
136}
137read(q2);
138for(q=1;q<=q2;q++) ; op[++m]=(op);
143 }else
if(fl==2
); op[++m]=(op);
145 }else
if(fl==3
); //
op[++m]=(op);
150 op[++m]=(op); //
op[++m]=(op);
151 op[++m]=(op); op[++m]=(op);
152 op[++m]=(op); op[++m]=(op);
153 }else
; op[++m]=(op);
155 op[++m]=(op); op[++m]=(op);
156}
157}
158159
}160
161 cdq(1
,m);
162for(i=1;i<=q2;i++) if
(isquery[i])
163 printf("
%d\n
",ans[i]);
164return0;
165 }
bzoj4170 極光(CDQ分治)
time limit 30 sec memory limit 512 mb 若是萬一琪露諾 俗稱rhl 進行攻擊,什麼都好,冷靜地回答她的問題來吸引她。對方表現出興趣的話,那就慢 慢地反問。在她考慮答案的時候,趁機逃吧。就算是很簡單的問題,她一定也答不上來。上古之魔書 天空中出現了許多的北極光,這些...
BZOJ1173 CDQ分治 筆記
目錄一類特殊的cdq分治 附 bzoj mokia ac 偏序關係 link1 資料結構 link1,高階指南p212 雜燴 link1 hdu1892 一維樹狀陣列 二進位制分段思想 可以把 m 2 a 2 b cdots a b cdots 分解成 0,2 a 2 a 2 a 2 b m 2 z...
BZOJ 2253 紙箱堆疊(CDQ分治)
description p 工廠是乙個生產紙箱的工廠。紙箱生產線在人工輸入三個引數 n p a 之後,即可自動化生產三邊邊長為 a mod p,a 2 mod p,a 3 mod p a 4 mod p,a 5 mod p,a 6 mod p a 3n 2 mod p,a 3n 1 mod p,a ...