CODEVS1490 [CTSC2008]网络管理

题目描述 Description

M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门。为了让分布在世界各地的N个部门之间 协同工作,公司搭建了一个连接整个公司的通信网络。该网络的结构由N个路由器和N-1条高速光缆组成。每个部门都有一个专属的路由器,部门局域网内的所有 机器都联向这个路由器,然后再通过这个通信子网与其他部门进行通信联络。该网络结构保证网络中的任意两个路由器之间都存在一条直接或间接路径以进行通信。

 高速光缆的数据传输速度非常快,以至于利用光缆传输的延迟时间可以忽略。但是由于路由器老化,在这些路由器上进行数据交换会带来很大的延迟。而两 个路由器之间的通信延迟时间则与这两个路由器通信路径上所有路由器中最大的交换延迟时间有关。作为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 & Hint

100% 测试数据满足 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!
");
163             else printf("%d
",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