日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【Luogu】P3950部落冲突(树链剖分)

發(fā)布時(shí)間:2025/3/15 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Luogu】P3950部落冲突(树链剖分) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  題目鏈接

  狀態(tài)奇差無(wú)比,sbt都能錯(cuò)一遍。

  不動(dòng)筆光想沒(méi)有想到怎么做,畫(huà)圖之后發(fā)現(xiàn)一個(gè)很明顯的性質(zhì)……

  那就是兩個(gè)開(kāi)戰(zhàn)的部落,其中一個(gè)是另一個(gè)的父親。

  所以在兒子那里加個(gè)權(quán)值。查詢的時(shí)候樹(shù)鏈剖分查詢鏈上點(diǎn)權(quán)和,減去lca的點(diǎn)權(quán)(因?yàn)閘ca那如果有點(diǎn)權(quán),代表的是lca和lca的父親之間的那條邊)。

  

#include<cstdio> #include<algorithm> #include<cctype> #include<cstring> #include<cstdlib> #define left (rt<<1) #define right (rt<<1|1) #define mid ((l+r)>>1) #define lson l,mid,left #define rson mid+1,r,right #define maxn 300050 using namespace std; inline long long read(){long long num=0,f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}while(isdigit(ch)){num=num*10+ch-'0';ch=getchar();}return num*f; }struct Edge{int next,to; }edge[maxn*3]; int head[maxn],num; inline void add(int from,int to){edge[++num]=(Edge){head[from],to};head[from]=num; }int tree[maxn*4]; int dfn[maxn]; int deep[maxn]; int father[maxn]; int size[maxn]; int top[maxn]; int son[maxn]; int ID,n,m;void unifnd(int x,int fa){deep[x]=deep[fa]+1; size[x]=1;for(int i=head[x];i;i=edge[i].next){int to=edge[i].to;if(to==fa) continue;father[to]=x;unifnd(to,x);size[x]+=size[to];if(son[x]==0||size[son[x]]<size[to]) son[x]=to;} }void unionn(int x,int Top){dfn[x]=++ID; top[x]=Top;if(son[x]==0) return;unionn(son[x],Top);for(int i=head[x];i;i=edge[i].next){int to=edge[i].to;if(to==father[x]||to==son[x]) continue;unionn(to,to);} }inline void pushup(int rt){tree[rt]=tree[left]+tree[right]; }void update(int o,int num,int l,int r,int rt){if(l==r){tree[rt]+=num;return;}if(o<=mid) update(o,num,lson);else update(o,num,rson);pushup(rt);return; }int query(int from,int to,int l,int r,int rt){if(from<=l&&to>=r) return tree[rt];int ans=0;if(from<=mid) ans+=query(from,to,lson);if(to>mid) ans+=query(from,to,rson);return ans; }struct War{int from,to; }q[maxn];int cnt;inline void adds(int from,int to){if(deep[from]<deep[to]) swap(from,to);q[++cnt]=(War){from,to};update(dfn[from],1,1,n,1);return; }inline void del(int rnk){int from=q[rnk].from;update(dfn[from],-1,1,n,1); }inline int LCA(int from,int to){while(top[from]!=top[to]){if(deep[top[from]]<deep[top[to]]) swap(from,to);from=father[top[from]];}if(deep[from]>deep[to]) swap(from,to);return from; }inline int ask(int from,int to){int lca=LCA(from,to),ans=-query(dfn[lca],dfn[lca],1,n,1);while(top[from]!=top[to]){if(deep[top[from]]<deep[top[to]]) swap(from,to);ans+=query(dfn[top[from]],dfn[from],1,n,1);if(ans>0) return 0;from=father[top[from]];}if(deep[from]>deep[to]) swap(from,to);ans+=query(dfn[from],dfn[to],1,n,1);if(ans>0) return 0;return 1; }int main(){n=read(),m=read();for(int i=1;i<n;++i){int x=read(),y=read();add(x,y);add(y,x);}unifnd(1,1);unionn(1,1);for(int i=1;i<=m;++i){char c[10];scanf("%s",c);if(c[0]=='Q'){int x=read(),y=read();if(ask(x,y)) printf("Yes\n");else printf("No\n");}else if(c[0]=='C'){int x=read(),y=read();adds(x,y);}else{int x=read();del(x);}}return 0; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/cellular-automaton/p/8387305.html

總結(jié)

以上是生活随笔為你收集整理的【Luogu】P3950部落冲突(树链剖分)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。