out_of_theblue10

heavypath

Jun 12th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <algorithm>
  4. #define MAXN 100010
  5. using namespace std;
  6. vector<int>v[MAXN];
  7. int up[MAXN],sub[MAXN],lant[MAXN],poz[MAXN],val[MAXN],niv[MAXN],father[MAXN];
  8. int lanturi,Length[MAXN],Max,arb[500010],Decalaj[MAXN];
  9. int m,n;
  10. void DF(int x)
  11. {
  12.   int i;
  13.   int longest=0;
  14.   sub[x]=1;
  15.   for(i=0;i<v[x].size();i++)
  16.   if(!niv[v[x][i]])
  17.   {
  18.     father[v[x][i]]=x;
  19.     niv[v[x][i]]=niv[x]+1;
  20.     DF(v[x][i]);
  21.     sub[x]+=sub[v[x][i]];
  22.     if(sub[v[x][i]]>sub[longest]) longest=v[x][i];
  23.   }
  24.   if(v[x].size()==1 and x!=1){ lanturi++; lant[x]=lanturi; }
  25.   else lant[x]=lant[longest];
  26.   longest=lant[x];
  27.   Length[longest]++;
  28.   poz[x]=Length[longest];
  29.   up[longest]=x;
  30. }
  31. void update(int Lant,int x,int y,int nod,int l,int r)
  32. {
  33.   if(l==r)
  34.   {
  35.     arb[Lant+nod]=y;
  36.     return;
  37.   }
  38.   int m=(l+r)/2;
  39.   if(x<=m) update(Lant,x,y,2*nod,l,m);
  40.   if(x>m) update(Lant,x,y,2*nod+1,m+1,r);
  41.   arb[Lant+nod]=max(arb[Lant+2*nod],arb[Lant+2*nod+1]);
  42. }
  43. void query2(int Lant,int x,int y,int nod,int l,int r)
  44. {
  45.   if(x<=l and y>=r)
  46.   {
  47.     if(Max<arb[Lant+nod]) Max=arb[Lant+nod];
  48.     return;
  49.   }
  50.   int m=(l+r)/2;
  51.   if(x<=m) query2(Lant,x,y,2*nod,l,m);
  52.   if(y>m) query2(Lant,x,y,2*nod+1,m+1,r);
  53.  
  54. }
  55. void main_query(int x,int y)
  56. {
  57.   int a,b;
  58.   if(lant[x]==lant[y])
  59.   {
  60.     a=min(poz[x],poz[y]);
  61.     b=max(poz[x],poz[y]);
  62.     query2(Decalaj[lant[x]],a,b,1,1,Length[lant[x]]);
  63.     return;
  64.   }
  65.   if(niv[up[lant[x]]]>niv[up[lant[y]]])
  66.   {
  67.     a=1;
  68.     b=poz[x];
  69.     query2(Decalaj[lant[x]],a,b,1,1,Length[lant[x]]);
  70.     main_query(father[up[lant[x]]],y);
  71.     return;
  72.   }
  73.   if(niv[up[lant[x]]]<=niv[up[lant[y]]])
  74.   {
  75.     a=1;
  76.     b=poz[y];
  77.     query2(Decalaj[lant[y]],a,b,1,1,Length[lant[y]]);
  78.     main_query(x,father[up[lant[y]]]);
  79.   }
  80. }
  81. int main()
  82. {
  83.     int i,t,x,y;
  84.     ifstream fi("heavypath.in");
  85.     ofstream fo("heavypath.out");
  86.     fi>>n>>m;
  87.     for(i=1;i<=n;i++) fi>>val[i];
  88.     for(i=1;i<n;i++)
  89.     {
  90.       fi>>x>>y;
  91.       v[x].push_back(y);
  92.       v[y].push_back(x);
  93.     }
  94.     niv[1]=1;
  95.     DF(1);
  96.  
  97.     for(i=1;i<=lanturi;i++) Decalaj[i]=Length[i-1]+Decalaj[i-1];
  98.     for(i=1;i<=n;i++)
  99.     {
  100.       poz[i]=Length[lant[i]]-poz[i]+1;
  101.       update(Decalaj[lant[i]],poz[i],val[i],1,1,Length[lant[i]]);
  102.     }
  103.     for(i=1;i<=m;i++)
  104.     {
  105.       fi>>t>>x>>y;
  106.       if(t==0)
  107.       {
  108.         update(Decalaj[lant[x]],poz[x],y,1,1,Length[lant[x]]);
  109.       } else
  110.       {
  111.         Max=0;
  112.         main_query(x,y);
  113.         fo<<Max<<"\n";
  114.       }
  115.     }
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment