2014鞍山网络预选赛1006(LCT模板题)hdu5002

Tree

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 41    Accepted Submission(s): 10


Problem Description
You are given a tree with N nodes which are numbered by integers 1..N. Each node is associated with an integer as the weight.

Your task is to deal with M operations of 4 types:

1.Delete an edge (x, y) from the tree, and then add a new edge (a, b). We ensure that it still constitutes a tree after adding the new edge.

2.Given two nodes a and b in the tree, change the weights of all the nodes on the path connecting node a and b (including node a and b) to a particular value x.

3.Given two nodes a and b in the tree, increase the weights of all the nodes on the path connecting node a and b (including node a and b) by a particular value d.

4.Given two nodes a and b in the tree, compute the second largest weight on the path connecting node a and b (including node a and b), and the number of times this weight occurs on the path. Note that here we need the strict second largest weight. For instance, the strict second largest weight of {3, 5, 2, 5, 3} is 3.
 

Input
The first line contains an integer T (T<=3), which means there are T test cases in the input.

For each test case, the first line contains two integers N and M (N, M<=10^5). The second line contains N integers, and the i-th integer is the weight of the i-th node in the tree (their absolute values are not larger than 10^4).

In next N-1 lines, there are two integers a and b (1<=a, b<=N), which means there exists an edge connecting node a and b.

The next M lines describe the operations you have to deal with. In each line the first integer is c (1<=c<=4), which indicates the type of operation.

If c = 1, there are four integers x, y, a, b (1<= x, y, a, b <=N) after c.
If c = 2, there are three integers a, b, x (1<= a, b<=N, |x|<=10^4) after c.
If c = 3, there are three integers a, b, d (1<= a, b<=N, |d|<=10^4) after c.
If c = 4 (it is a query operation), there are two integers a, b (1<= a, b<=N) after c.

All these parameters have the same meaning as described in problem description.
 

Output
For each test case, first output "Case #x:"" (x means case ID) in a separate line.

For each query operation, output two values: the second largest weight and the number of times it occurs. If the weights of nodes on that path are all the same, just output "ALL SAME" (without quotes).
 

Sample Input
   
   
2 3 2 1 1 2 1 2 1 3 4 1 2 4 2 3 7 7 5 3 2 1 7 3 6 1 2 1 3 3 4 3 5 4 6 4 7 4 2 6 3 4 5 -1 4 5 7 1 3 4 2 4 4 3 6 2 3 6 5 4 3 6
 

Sample Output
   
   
Case #1: ALL SAME 1 2 Case #2: 3 2 1 1 3 2 ALL SAME

题意:RT

思路:LCT模板题,有点要注意的是向下延迟更新的时候要先更新将一条链加一个数,再更新替换成一个数

            比赛的时候就是因为把这两个操作写反了导致交了10次才过,也是醉到不行~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; #define maxn 100010 #define FOR(i,n) for(int i=0;i 
        
          y?x:y; } struct node{ int w; node *fa; node *p; node *ch[2]; int maxi[2]; int flag; int cnt; int vv; int sz; int num[2]; bool root; node(){ w=flag=cnt=root=vv=sz=0; maxi[0]=maxi[1]=-INF; num[0]=num[1]=0; } int d(){ return this==p->ch[1]; } void setchild(node*c,int d){ ch[d]=c; c->p=this; } void rev(){ flag^=1; swap(ch[0],ch[1]); } void pushup(); void add(int cn) { cnt+=cn; w+=cn; maxi[0]+=cn; if(maxi[1]!=-INF)maxi[1]+=cn; } void setva(int cn) { vv=1; w=cn; maxi[0]=cn; num[0]=sz; maxi[1]=-INF; num[1]=0; } void isroot(); }*null,*vex[maxn]; void node::isroot() { root=true; p=null; } struct nodev{ int w; int num; }; bool cmpv(nodev a,nodev b) { return a.w>b.w; } void node::pushup(){ sz=1; maxi[0]=w; maxi[1]=-INF; num[0]=0; num[1]=0; nodev vec[6]; int d=0; vec[d].w=w; vec[d++].num=1; if(ch[1]!=null){ sz+=ch[1]->sz; vec[d].w=ch[1]->maxi[0]; vec[d++].num=ch[1]->num[0]; vec[d].w=ch[1]->maxi[1]; vec[d++].num=ch[1]->num[1]; } if(ch[0]!=null){ sz+=ch[0]->sz; vec[d].w=ch[0]->maxi[0]; vec[d++].num=ch[0]->num[0]; vec[d].w=ch[0]->maxi[1]; vec[d++].num=ch[0]->num[1]; } sort(vec,vec+d,cmpv); vec[d].w=-INF-1; vec[d].num=0; int i; for(i=0;i 
         
           cnt) { FOR(i,2)if (o->ch[i] != null)o->ch[i]->add(o->cnt); o->cnt=0; } if(o->vv) { FOR(i,2)if (o->ch[i] != null)o->ch[i]->setva(o->w); o->vv=0; } if(o->flag) { FOR(i,2)if (o->ch[i] != null)o->ch[i]->rev(); o->flag=0; } } node * newnode(int w) { node *u=(node*)malloc(sizeof(node)); u->ch[0]=u->ch[1]=u->p=u->fa=null; u->w=w; u->vv=0; u->flag=0; u->num[0]=1; u->num[1]=0; u->maxi[0]=w; u->maxi[1]=-INF; u->sz=1; u->cnt=0; u->root=true; return u; } void pushto(node *o) { static node *stk[maxn]; int rear=0; while(o!=null) { stk[rear++]=o; o=o->p; } for(int i=rear-1;i>=0;i--)pushdown(stk[i]); } void rot(node*o) { node*k=o->p; if(k!=null)pushdown(k); pushdown(k); pushdown(o); int d=o->d(); k->p->setchild(o,k->d()); k->setchild(o->ch[d^1],d); o->setchild(k,d^1); k->pushup(); if(k->root) { k->root=false; o->root=true; o->fa=k->fa; k->fa=null; } } void splay(node *o) { pushto(o); while(o->p!=null) { if(o->p->p==null)rot(o); else o->p->d()==o->d()?(rot(o->p) , rot(o)):(rot(o) , rot(o)); } o->pushup(); } node* access(node *o) { node *c=null; for(;o!=null;c=o,o=o->fa) { splay(o); o->ch[1]->fa=o; o->ch[1]->isroot(); o->setchild(c,1); c->root=false; } return c; } void makeroot(node*o) { access(o); splay(o); o->rev(); } void link(node *u,node *v) { if(u==v)return; makeroot(u); access(v); splay(v); if(u->p==null)u->fa=v; } void cut(node *u,node *v) { makeroot(u); access(v); splay(v); if(u->p!=null){ v->ch[0]->isroot(); v->ch[0]->fa=null; v->ch[0]=null; v->isroot(); v->fa=null; v->pushup(); } } void setw(node *u,node *v,int w) { makeroot(u); access(v); splay(v); if(u==v||u->p!=null)v->setva(w); } void addw(node *u,node *v,int w) { makeroot(u); access(v); splay(v); if(u==v||u->p!=null)v->add(w); } void queryw(node *u,node *v) { if(u==v){ printf("ALL SAME\n"); return; } makeroot(u); access(v); splay(v); if(u->p!=null){ if(v->num[1]==0)printf("ALL SAME\n"); else printf("%d %d\n",v->maxi[1],v->num[1]); } } int head[maxn]; int next[maxn<<1]; int edge[maxn<<1]; int d; void dfs(int u,int pre) { int i; for(i=head[u];i!=-1;i=next[i])if(edge[i]!=pre){ vex[edge[i]]->fa=vex[u]; dfs(edge[i],u); } } void add(int u,int v) { edge[d]=v; next[d]=head[u]; head[u]=d++; } int main() { int i,u,v,Q; int t,ttt=0; scanf("%d",&t); int w; null=(node*)malloc(sizeof(node)); null->ch[0]=null->ch[1]=NULL; while(t--) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { scanf("%d",&w); vex[i]=newnode(w); } d=0; memset(head,-1,sizeof(head)); for(i=1;i 
           
          
         
       
      
      
     
     
    
    
   
   
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值