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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

BZOJ 1036 [ZJOI2008]树的统计Count

發布時間:2025/4/16 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BZOJ 1036 [ZJOI2008]树的统计Count 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以前動態樹寫過這個題,今天嘗試樹鏈剖分解決~

模板題,就聲明一點,線段樹維護的是點權

?

View Code 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 7 #define N 50000 8 #define M 100000 9 #define INF 1e9 10 11 using namespace std; 12 13 int head[N],to[M],next[M]; 14 int sz[N],top[N],bh[N],fa[N],son[N],dep[N]; 15 int sum[N<<2],mx[N<<2],val[N],dat[N]; 16 int q[N]; 17 int n,cnt,tot,qu; 18 19 inline void add(int u,int v) 20 { 21 to[cnt]=v; next[cnt]=head[u]; head[u]=cnt++; 22 } 23 24 inline void prep() 25 { 26 int h=1,t=2,sta; 27 q[1]=1; dep[1]=1; 28 while(h<t) 29 { 30 sta=q[h++]; sz[sta]=1; 31 for(int i=head[sta];~i;i=next[i]) 32 if(to[i]!=fa[sta]) 33 { 34 fa[to[i]]=sta; 35 dep[to[i]]=dep[sta]+1; 36 q[t++]=to[i]; 37 } 38 } 39 for(int j=t-1;j>=1;j--) 40 { 41 sta=q[j]; 42 for(int i=head[sta];~i;i=next[i]) 43 if(to[i]!=fa[sta]) 44 { 45 sz[sta]+=sz[to[i]]; 46 if(sz[to[i]]>sz[son[sta]]) son[sta]=to[i]; 47 } 48 } 49 for(int i=1;i<t;i++) 50 { 51 sta=q[i]; 52 if(son[fa[sta]]==sta) top[sta]=top[fa[sta]];//不是重鏈頂部 53 else top[sta]=sta; 54 } 55 } 56 57 inline void rewrite() 58 { 59 for(int i=1;i<=n;i++) 60 if(top[i]==i) 61 for(int j=i;j;j=son[j])//每條重鏈的編號是連續的 62 { 63 bh[j]=++tot; 64 dat[tot]=val[j]; 65 } 66 } 67 68 inline void pushup(int x) 69 { 70 sum[x]=sum[x<<1]+sum[x<<1|1]; 71 mx[x]=max(mx[x<<1],mx[x<<1|1]); 72 } 73 74 inline void build(int u,int L,int R) 75 { 76 if(L==R) {sum[u]=mx[u]=dat[L];return;} 77 int MID=(L+R)>>1; 78 build(u<<1,L,MID); build(u<<1|1,MID+1,R); 79 pushup(u); 80 } 81 82 inline void read() 83 { 84 memset(head,-1,sizeof head); cnt=0; 85 scanf("%d",&n); 86 for(int i=1,a,b;i<n;i++) 87 { 88 scanf("%d%d",&a,&b); 89 add(a,b); add(b,a); 90 } 91 for(int i=1;i<=n;i++) scanf("%d",&val[i]); 92 prep(); 93 rewrite(); 94 build(1,1,tot); 95 } 96 97 inline int querysum(int u,int L,int R,int l,int r) 98 { 99 if(l<=L&&R<=r) return sum[u]; 100 int MID=(L+R)>>1,res=0; 101 if(l<=MID) res+=querysum(u<<1,L,MID,l,r); 102 if(MID<r) res+=querysum(u<<1|1,MID+1,R,l,r); 103 return res; 104 } 105 106 inline int getsum(int x,int y) 107 { 108 int res=0; 109 while(top[x]!=top[y]) 110 { 111 if(dep[top[x]]<dep[top[y]]) swap(x,y); 112 res+=querysum(1,1,tot,bh[top[x]],bh[x]); 113 x=fa[top[x]]; 114 } 115 if(bh[x]>bh[y]) swap(x,y); 116 res+=querysum(1,1,tot,bh[x],bh[y]); 117 return res; 118 } 119 120 inline int querymax(int u,int L,int R,int l,int r) 121 { 122 if(l<=L&&R<=r) return mx[u]; 123 int MID=(L+R)>>1,res=-INF; 124 if(l<=MID) res=max(res,querymax(u<<1,L,MID,l,r)); 125 if(MID<r) res=max(res,querymax(u<<1|1,MID+1,R,l,r)); 126 return res; 127 } 128 129 inline int getmax(int x,int y) 130 { 131 int res=-INF; 132 while(top[x]!=top[y]) 133 { 134 if(dep[top[x]]<dep[top[y]]) swap(x,y); 135 res=max(res,querymax(1,1,tot,bh[top[x]],bh[x])); 136 x=fa[top[x]]; 137 } 138 if(bh[x]>bh[y]) swap(x,y); 139 140 res=max(res,querymax(1,1,tot,bh[x],bh[y])); 141 return res; 142 } 143 144 inline void updata(int u,int L,int R,int pos,int sp) 145 { 146 if(L==R) {mx[u]=sum[u]=sp;return;} 147 int MID=(L+R)>>1; 148 if(pos<=MID) updata(u<<1,L,MID,pos,sp); 149 else updata(u<<1|1,MID+1,R,pos,sp); 150 pushup(u); 151 } 152 153 inline void go() 154 { 155 scanf("%d",&qu); 156 char str[10];int a,b; 157 while(qu--) 158 { 159 scanf("%s%d%d",str,&a,&b); 160 if(str[1]=='S') printf("%d\n",getsum(a,b)); 161 else if(str[1]=='M') printf("%d\n",getmax(a,b)); 162 else updata(1,1,tot,bh[a],b); 163 } 164 } 165 166 int main() 167 { 168 read(); 169 go(); 170 return 0; 171 }

?

?

轉載于:https://www.cnblogs.com/proverbs/archive/2013/01/18/2867092.html

總結

以上是生活随笔為你收集整理的BZOJ 1036 [ZJOI2008]树的统计Count的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩黄色短片 | 97超碰人人模人人人爽人人爱 | av在线影片 | 色视频在线观看 | 国产一级淫片免费 | 久久99精品久久久久久噜噜 | 女同vk| 黄网站免费观看 | 欧美在线一区二区 | 国产成人久久 | 怡红院男人天堂 | 日韩国产专区 | 国产欧美精品aaaaaa片 | 欧洲一区在线 | 男生女生羞羞网站 | av成人毛片| 国产成人免费av | 欧美三级自拍 | 12av在线| 欧美日韩综合在线观看 | 欧美xxxx8888| 国产黄色片在线 | 青青国产在线视频 | 国产视频麻豆 | 精品无码av一区二区三区 | 男人激情网 | 91国产大片 | 成人软件在线观看 | 99久久国产热无码精品免费 | 徐锦江一级淫片免费看 | 欧美一区亚洲一区 | 成人久久电影 | 久久精品女人毛片国产 | 欧美在线视频不卡 | 国产精品乱码久久久久久久久 | 久久久久久国产精品一区 | 吞精囗交69激情欧美 | 成人香蕉视频在线观看 | 中文精品视频 | 无码人妻精品一区二区三区夜夜嗨 | 极品销魂美女少妇尤物 | 日本在线观看一区二区三区 | 黄色www视频| 少妇一晚三次一区二区三区 | avwww| 成人羞羞国产免费 | 欧美特黄一级 | 色偷偷成人 | 性欧美18一19性猛交 | 国产一级二级三级 | 亚洲欧美日韩不卡 | 成人自拍视频网 | 日日骚一区二区 | 美丽姑娘免费观看在线观看 | 久久久久亚洲AV成人 | 久久99久久99精品免观看粉嫩 | 国内久久精品视频 | www亚洲成人 | 欧美日韩第一页 | 熟女一区二区三区四区 | 日韩黄色片免费看 | 久久九九免费视频 | 色哟哟网站| 黄色九九| 亚洲精品国产日韩 | www视频在线观看免费 | 久久亚洲第一 | 国产又粗又猛又爽免费视频 | 成人精品av | 精品国产中文字幕 | 丰满少妇xoxoxo视频 | 好吊妞视频这里只有精品 | 国内视频一区二区三区 | 欧美一级黄色片视频 | 精品久久五月天 | 中文国产| 成人综合色站 | 日本打白嫩屁股视频 | 爱爱小视频网站 | 思思99精品视频在线观看 | 日本中文字幕一区二区 | 久久精品无码一区二区三区免费 | 欧美人成在线视频 | av在线等| 椎名空在线 | 亚洲欧洲一区二区三区 | 久久大综合 | 黑人巨大猛交丰满少妇 | 女人下面流白浆的视频 | 亚洲欧美日韩一区在线观看 | 亚洲国产成人在线视频 | 欧美性猛交xxxx黑人 | 狠狠狠狠狠狠狠 | 玖玖视频国产 | 老妇高潮潮喷到猛进猛出 | 久久影院中文字幕 | 精久久| 亚洲综合成人网 | 日韩av无码久久 |