日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 雪白的扔子视频大全在线观看 | 波多野结衣在线影院 | 精品成在人线av无码免费看 | 一区精品在线 | 中文字幕在线字幕中文 | 九九黄色大片 | 成年网站在线播放 | 青草草在线观看 | 男人天堂2019 | 色综合网站 | 天天操操操 | 伊人3 | 成人免费直播 | 少妇一级淫片免费放2 | 国产精品国产自产拍高清av | 国产3p在线播放 | 中文字幕伊人 | 九九在线精品 | 国产影音先锋 | 亚洲丝袜视频 | 欧美成人a交片免费看 | 香蕉钻洞视频 | aaa亚洲 | 三级三级久久三级久久18 | 日日鲁鲁鲁夜夜爽爽狠狠视频97 | 久久曹| 91喷水视频| 亚洲一区二区国产 | 奴性女会所调教 | 青青草原亚洲视频 | 成人在线综合 | 欧美射射射| 欧美性开放视频 | av涩涩 | 欧美日韩在线免费播放 | 一区国产视频 | 射精一区二区 | 色婷婷久久 | 男人视频网站 | 日本午夜免费 | 男生把女生困困的视频 | 豆花免费跳转入口官网 | 青青草原成人网 | 国产激情视频在线观看 | 日本不卡视频一区二区三区 | 欧洲久久久 | 成人看的毛片 | 99热com| 国产精品一区二区三区高潮 | 爽爽视频在线观看 | 米奇7777狠狠狠狠视频 | 韩国av电影在线观看 | 美日韩丰满少妇在线观看 | 在线观看亚洲区 | 精品中出| 性色av网| av网址有哪些 | 国产人妖在线视频 | 欧美一区二区高清 | 日本免费久久 | 麻豆av免费| 日韩一级完整毛片 | 欧美黑人又粗又大高潮喷水 | 日本久久高清 | 日本xxx高清| 亚洲爽爽爽| 国产激情视频在线观看 | 97久草 | 日韩成人激情 | 乱视频在线观看 | 在线观看视频99 | 欧美八区 | 青青草国产 | 国内精品嫩模av私拍在线观看 | 麻豆精品在线 | 国产精品一区二区三区四区 | 亚洲一区免费在线观看 | 黄视频在线播放 | 日本不卡一 | 欧美xxxxbbbb | 麻豆福利在线观看 | xxxxx黄色片 噜噜噜噜噜色 | 天天做天天干 | 国产综合久久久久 | 亚洲欧美另类在线视频 | 欧美国产一区二区在线观看 | www日韩 | 欧美极品三级 | 欧美三级日本三级 | 亚洲国产精品一 | 狠狠爱五月婷婷 | 亚洲精品97 | 日韩一区二区免费视频 | 97超碰网| av免费精品| 秋霞午夜鲁丝一区二区老狼 | 免费网站在线观看人数在哪动漫 | 日韩精品中文字幕一区 | av鲁丝一区鲁丝二区鲁丝三区 |