"Bzoj 1146" [CTSC2008] network management networks

Source: Internet
Author: User
Tags net time

1146: [CTSC2008] Network management net time limit: Sec Memory Limit: 162 MB
Submit: 1938 Solved: 577
[Submit] [Status] Description

M Corporation is a very large multinational company, which has its subordinate branches or departments in many countries. In order to work together across N departments around the world, the company has built a communications network that connects the entire company. The structure of the network consists of n routers and N-1 high-speed optical cable. Each department has a dedicated router, all the machines in the departmental LAN are connected to the router, and then communicate with other departments through this communication subnet. This network structure ensures that there is a direct or indirect path between any two routers in the network for communication. High-speed optical cable data transmission is very fast, so that the use of cable transmission delay time can be ignored. However, due to the aging of routers, there is a great delay in exchanging data on these routers. The communication latency between the two routers is related to the maximum switching latency in all routers on the two routers ' communication paths. As an intern in the network division of M Company, you are now asked to write a simple program to monitor the company's network situation. The program is able to update the change information of the network condition at any time (change of delay time of the router data interchange), and according to the query give the delay time of the router on the two router communication path delay of the big K. "Task" your program reads the connection information of n routers and N-1 cables from the input file, and the initial data exchange delay time for each router is TI, and the Q Bar asks (or states change) information. And then processing the Q-bar query information, they may be: 1. A router's data exchange delay time has changed due to a newer device, or a new failure of the device. 2. Query the delay time of a router with a delay of k on a path between two routers A and B.

Input

The first behavior is two integers n and q, respectively, representing the total number of routers and queries. The second line has n integers, and the number of I represents the initial data delay time of the router with the number I. ti. Immediately following the N-1 line, each line contains two integers x and y. Indicates that there is an optical cable connecting router x and Router Y. followed by Q line, three integers per line k, a, B. If k=0, it indicates that the state of router A has changed, and its data interchange delay time is changed from TA to B. If k>0, indicates the delay time of the router that is delaying the K-large in all routers (including A and b) that are on the path from A to B. Note A can be equal to B, when there is only one router on the path.

Output

For each of the second queries (k>0), output one line. Contains an integer for the corresponding delay time. Output message "Invalid request!" if the router on the path is less than K (All lowercase characters do not contain quotation marks, and there is a space between two words).

Sample Input5 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 Output3
2
2
Invalid request!HINT

10% test data satisfies n<=8000,q<=3000,
40% test data satisfies all inquiries in 1<=k<=5. That is, the delay time of the router is not changed.

100% test data satisfies n,q<=80000, Any router satisfies a delay time of less than 10^8 at any time. For all inquiries meet 0<=k<=n.


Tree chain split + line tree set balance tree (TREAP)


The query operation is mapped to the segment tree by the tree chain, and each node of the segment tree is constructed with the treap of the weight as the key, and then the interval K can be obtained.


The method is the same as the "Bzoj 3196" two-force Balance tree (Getnum).


#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < vector> #include <cstring> #define PB push_back#define N 100005using namespace Std;int r=0,num=0,tot=0,root[20* N],top[n],siz[n],fa[n],id[n],t[n],tt[n],dep[n],son[n];   Note the root array size vector<int> v[n];struct treap{int data,l,r,fix,size,weight; Weight represents the same as the current node weight with several}a[20*n];int n,q;void Dfs1 (int x,int f,int de) {siz[x]=1;fa[x]=f;dep[x]=de;son[x]=0;for (int i= 0;i<v[x].size (); i++) if (v[x][i]!=f) {int u=v[x][i];D FS1 (u,x,de+1); Siz[x]+=siz[u];if (Siz[son[x]]<siz[u]) son[ X]=u;}} void Dfs2 (int x,int tp) {top[x]=tp;id[x]=++num;if (son[x]) DFS2 (SON[X],TP); for (int i=0;i<v[x].size (); i++) {int u=v[x ][i];if (u==fa[x]| | U==SON[X]) CONTINUE;DFS2 (u,u);}} void push_up (int x) {a[x].size=a[a[x].r].size+a[a[x].l].size+a[x].weight;} void Zag (int &x) {int o=a[x].l;a[x].l=a[o].r;a[o].r=x;a[o].size=a[x].size; PUSH_UP (x); x=o;} void Zig (int &x) {int o=a[x].r;a[x].r=a[o].l;a[o].l=x;a[o].siZe=a[x].size; PUSH_UP (x); x=o;} void New_node (int &x,int data) {X=++tot;a[x].l=a[x].r=0;a[x].size=a[x].weight=1;a[x].fix=rand (); a[x].data=data ;} void Insert (int &x,int data) {if (!x) {new_node (x,data); return;} A[x].size++;if (data==a[x].data) {A[x].weight++;return;} if (data<a[x].data) {Insert (A[x].l,data), if (A[a[x].l].fix<a[x].fix) zag (x);} else {Insert (a[x].r,data); if (A[a[x].r].fix<a[x].fix) zig (x);}} void Build (int x,int l,int r,int now) {Insert (Root[x],tt[now]), if (l==r) Return;int m= (l+r) >>1;if (now<=m) Build (X<<1,l,m,now); else Build (X<<1|1,m+1,r,now);}  void delet (int &x,int data) {if (a[x].data==data) {if (a[x].weight>1) {a[x].weight--; Size,weight to lose!! A[x].size--;return;} if (a[x].l*a[x].r==0) X=a[x].l+a[x].r;else if (a[a[x].l].fix<a[a[x].r].fix) {zag (x);D Elet (x,data);} else {Zig (x);D Elet (x,data);} return;} A[x].size--;if (A[x].data>data) delet (a[x].l,data); else Delet (a[x].r,data);} void Modify (int x,int l,int r,int p,int pre,int now) {delet (root[x],pre); Insert (Root[x],now), if (l==r) Return;int m= (l+r) >>1;if (p<=m) Modify (X<<1,l,m,p,pre,now); else Modify (X<<1|1,m+1,r,p,pre,now);} void Askrank (int x,int data) {if (!x) return;if (data==a[x].data) {R+=a[a[x].r].size;return;} if (data>a[x].data) Askrank (a[x].r,data); else {r=r+a[a[x].r].size+a[x].weight; Askrank (A[x].l,data);}} void GetRank (int x,int lx,int rx,int l,int r,int data) {if (lx>=l&&rx<=r) {Askrank (root[x],data); return;} int m= (LX+RX) >>1;if (l<=m) GetRank (X<<1,lx,m,l,r,data), if (r>m) GetRank (X<<1|1,m+1,rx,l,r, data);} void Query (int u,int v,int data) {R=1;int Tp1=top[u],tp2=top[v];while (TP1!=TP2) {if (DEP[TP1]&LT;DEP[TP2]) swap (TP1,TP2 ), swap (U,V); GetRank (1,1,n,id[tp1],id[u],data); u=fa[tp1];tp1=top[u];} if (u==v) {if (t[u]>data) R+=1;return;} if (Dep[u]<dep[v]) swap (U,V); GetRank (1,1,n,id[v],id[u],data);} int findkth (int u,int v,int k) {Query (u,v,-1), if (r<=k) Return-1;int ans,l=0,rr=100000000;while (L&LT;=RR) {int m= (l+rR) >>1; Query (u,v,m); if (r<=k) Ans=m,rr=m-1;else l=m+1;} return ans;} int main () {scanf ("%d%d", &n,&q), for (Int. i=1;i<=n;i++) scanf ("%d", &t[i]), for (int i=1;i<n;i++) {int x , y;scanf ("%d%d", &x,&y), V[X].PB (y), V[Y].PB (x);} DFS1 (1,0,1), NUM=0;DFS2 (n), for (int i=1;i<=n;i++) tt[id[i]]=t[i];for (int i=1;i<=n;i++) Build (1,1,n,i), while ( q--) {int k,x,y;scanf ("%d%d%d", &k,&x,&y), if (!k) {Modify (1,1,n,id[x],t[x],y); t[x]=y;} Else{int ans=findkth (x,y,k); if (ans==-1) printf ("Invalid request!\n"); else printf ("%d\n", ans);}} return 0;}




Sentiment:

1.RE because the root array is not open enough


2. The following error is because: there is no weight to the same weight, but directly as the right son, at the time of deletion will fall into the cycle of death (he and the right side of the son of the same value), resulting in tle. So treap must handle the same weights with weight!!!

"Bzoj 1146" [CTSC2008] network management networks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.