CODEVS1490 [CTSC2008]网络管理
M公司是一個非常龐大的跨國公司,在許多國家都設有它的下屬分支機構或部門。為了讓分布在世界各地的N個部門之間 協同工作,公司搭建了一個連接整個公司的通信網絡。該網絡的結構由N個路由器和N-1條高速光纜組成。每個部門都有一個專屬的路由器,部門局域網內的所有 機器都聯向這個路由器,然后再通過這個通信子網與其他部門進行通信聯絡。該網絡結構保證網絡中的任意兩個路由器之間都存在一條直接或間接路徑以進行通信。
?高速光纜的數據傳輸速度非???#xff0c;以至于利用光纜傳輸的延遲時間可以忽略。但是由于路由器老化,在這些路由器上進行數據交換會帶來很大的延遲。而兩 個路由器之間的通信延遲時間則與這兩個路由器通信路徑上所有路由器中最大的交換延遲時間有關。作為M公司網絡部門的一名實習員工,現在要求你編寫一個簡單 的程序來監視公司的網絡狀況。該程序能夠隨時更新網絡狀況的變化信息(路由器數據交換延遲時間的變化),并且根據詢問給出兩個路由器通信路徑上延遲第k大 的路由器的延遲時間。
你的程序從輸入文件中讀入N個路由器和N-1條光纜的連接信息,每個路由器初始的數據交換延遲時間Ti,以及Q條詢問(或狀態改變)的信息。并依次處理這Q條詢問信息,它們可能是:
1.??????? 由于更新了設備,或者設備出現新的故障,使得某個路由器的數據交換延遲時間發生了變化。
2.??????? 查詢某兩個路由器a和b之間的路徑上延遲第k大的路由器的延遲時間。
輸入描述 Input Description輸入文件network.in中的第一行為兩個整數N和Q,分別表示路由器總數和詢問的總數。
第二行有N個整數,第i個數表示編號為i的路由器初始的數據延遲時間Ti。
緊接著N-1行,每行包含兩個整數x和y。表示有一條光纜連接路由器x和路由器y。
緊接著是Q行,每行三個整數k、a、b。如果k=0,則表示路由器a的狀態發生了變化,它的數據交換延遲時間由Ta變為b。如果k>0,則表示詢問a到b的路徑上所經過的所有路由器(包括a和b)中延遲第k大的路由器的延遲時間。注意a可以等于b,此時路徑上只有一個路由器。
輸出描述 Output Description輸出文件為network.out。對于每一個第二種詢問(k>0),輸出一行。包含一個整數為相應的延遲時間。如果路徑上的路由器不足k個,則輸出信息“invalid request!”(全部小寫不包含引號,兩個單詞之間有一個空格)。
樣例輸入 Sample Input? ? ? ?5 5
?????? 5 1 2 3 4
?????? 3 1
?????? 2 1
?????? 4 3
?????? 5 3
?????? 2 4 5
?????? 0 1 2
?????? 2 2 3
?????? 2 1 4
?????? 3 3 5
樣例輸出 Sample Output? ? ? ?3
?????? 2
?????? 2
?????? invalid request!
數據范圍及提示 Data Size & Hint100% 測試數據滿足 n<=80000,q<=30000 。任意一個路由器在任何時刻都滿足延遲時間小于108。對于所有詢問滿足 。
?????? 40% 測試數據滿足所有詢問中1<=k<=5 ,且路由器的延遲時間不會發生變化。
10% 測試數據滿足n,q<=8000 。
正解:帶修改主席樹
解題報告:誰能知道二分寫多了,寫線段樹左兒子(l,mid-1)右兒子(mid+1,r)的痛。。。就是帶修改主席樹的板子,直接查詢即可
?
1 #include <iostream> 2 #include <iomanip> 3 #include <algorithm> 4 #include <string> 5 #include <cstdlib> 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <vector> 10 #include <map> 11 #define MIN(a,b) a<b?a:b 12 #define RG register 13 const int N = 200000; 14 const int M = 15000000; 15 16 using namespace std; 17 18 map<int,int>Map; 19 20 int gi(){ 21 char ch=getchar();int x=0; 22 while(ch<'0' || ch>'9') ch=getchar(); 23 while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar(); 24 return x; 25 } 26 27 struct date{ 28 int i,t; 29 }t[N]; 30 31 int cnt,n,q,id[N],dis[N],son[N],top[N],fa[N],siz[N]; 32 int rt[N],ll[M],rr[M],tr[M],nn[N][2],head[N]; 33 int u[N],o; 34 vector<int>ls,lr; 35 36 void dfs1(int xh,int ff){ 37 fa[xh]=ff,dis[xh]=dis[ff]+1; 38 for (RG int i=head[xh]; i; i=nn[i][0]){ 39 if (nn[i][1]==ff) continue; 40 dfs1(nn[i][1],xh); 41 siz[xh]+=siz[nn[i][1]]; 42 if (siz[son[xh]]<siz[nn[i][1]]) 43 son[xh]=nn[i][1]; 44 } 45 siz[xh]=1; 46 return; 47 } 48 49 void dfs2(int xh,int tp){ 50 id[xh]=++cnt,top[xh]=tp; 51 if (son[xh]) dfs2(son[xh],tp); 52 for (RG int i=head[xh]; i; i=nn[i][0]){ 53 if (nn[i][1]==son[xh] || nn[i][1]==fa[xh]) continue; 54 dfs2(nn[i][1],nn[i][1]); 55 } 56 return; 57 } 58 59 int cmp(date a,date b){ 60 return a.i<b.i; 61 } 62 63 void down(int &xh,int w,int x,int l,int r){ 64 if (xh==0) xh=++cnt; 65 tr[xh]+=x; 66 if (l==r) return; 67 int mid=(l+r)>>1; 68 if (w<=mid) down(ll[xh],w,x,l,mid); 69 else down(rr[xh],w,x,mid+1,r); 70 return; 71 } 72 73 void update(int xh,int val,int x){ 74 while(xh<=n){ 75 down(rt[xh],val,x,1,o); 76 xh+=(xh&(-xh)); 77 } 78 return; 79 } 80 81 struct dete{ 82 int k,a,b; 83 }d[N]; 84 85 void push(int xh,int x){ 86 while(xh){ 87 if (x) lr.push_back(rt[xh]); 88 else ls.push_back(rt[xh]); 89 xh-=xh&(-xh); 90 } 91 return; 92 } 93 94 void lca(int l,int r){ 95 while(top[l]!=top[r]) 96 if (dis[top[l]]>dis[top[r]]){ 97 push(id[l],1),push(id[top[l]]-1,0); 98 l=fa[top[l]]; 99 } 100 else{ 101 push(id[r],1),push(id[top[r]]-1,0); 102 r=fa[top[r]]; 103 } 104 if (dis[l]<dis[r]) push(id[r],1),push(id[l]-1,0); 105 else push(id[l],1),push(id[r]-1,0); 106 return; 107 } 108 109 int query(int l,int r,int k){ 110 ls.clear(),lr.clear(); 111 lca(l,r); 112 int z=1,y=o,ans=1,la=ls.size(),lb=lr.size(); 113 int sa=0,sb=0,mid; 114 for (RG int i=0; i<la; ++i) sa+=tr[ls[i]]; 115 for (RG int i=0; i<lb; ++i) sb+=tr[lr[i]]; 116 if (sb-sa<k) return -1; 117 while(z<y){ 118 sa=0,sb=0,mid=(z+y)>>1; 119 for (RG int i=0; i<la; ++i) sa+=tr[rr[ls[i]]]; 120 for (RG int i=0; i<lb; ++i) sb+=tr[rr[lr[i]]]; 121 if (sb-sa>=k){ 122 z=mid+1;ans=mid+1; 123 for (RG int i=0; i<la; ++i) ls[i]=rr[ls[i]]; 124 for (RG int i=0; i<lb; ++i) lr[i]=rr[lr[i]]; 125 } 126 else{ 127 y=mid;k-=(sb-sa); 128 for (RG int i=0; i<la; ++i) ls[i]=ll[ls[i]]; 129 for (RG int i=0; i<lb; ++i) lr[i]=ll[lr[i]]; 130 } 131 } 132 ans=MIN(ans,o); 133 return ans; 134 } 135 136 int main(){ 137 n=gi(),q=gi(); 138 for (RG int i=1; i<=n; ++i) t[i]=(date){i,gi()},u[++o]=t[i].t; 139 for (RG int i=1; i<n ; ++i){ 140 RG int l=gi(),r=gi(); 141 nn[++cnt][1]=l,nn[cnt][0]=head[r],head[r]=cnt; 142 nn[++cnt][1]=r,nn[cnt][0]=head[l],head[l]=cnt; 143 } 144 cnt=0; 145 dfs1(1,0),dfs2(1,1); 146 for (RG int i=1; i<=q; ++i){ 147 d[i]=(dete){gi(),gi(),gi()}; 148 if (d[i].k==0) u[++o]=d[i].b; 149 } 150 sort(u+1,u+o+1); 151 int hh=unique(u+1,u+o+1)-u-1; 152 for (RG int i=1; i<=hh; ++i) Map[u[i]]=i; 153 for (RG int i=1; i<=n; ++i) t[i].i=id[t[i].i]; 154 cnt=0; 155 for (RG int i=1; i<=n; ++i){ 156 int uu=Map[t[i].t]; 157 update(t[i].i,uu,1); 158 } 159 for (RG int i=1; i<=q; ++i) 160 if (d[i].k){ 161 int ans=query(d[i].a,d[i].b,d[i].k); 162 if (ans<0) printf("invalid request!\n"); 163 else printf("%d\n",u[ans]); 164 } 165 else{ 166 int ss=Map[t[d[i].a].t],gg=Map[d[i].b]; 167 update(id[d[i].a],ss,-1); 168 update(id[d[i].a],gg,1); 169 t[d[i].a].t=d[i].b; 170 } 171 return 0; 172 }?
轉載于:https://www.cnblogs.com/cjk2001/p/6445284.html
總結
以上是生活随笔為你收集整理的CODEVS1490 [CTSC2008]网络管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无向图的完美消除序列 判断弦图 ZOJ
- 下一篇: Codeforces Gym101257