4 #define REP(i,n) for (int i=1; i<=n; ++i)
6 const int maxn
= 30000 + 10;
19 return p
->ch
[0] == this ? 0 : p
->ch
[1] == this ? 1 : -1;
22 inline void sc(int d
, node
* c
) { ch
[d
] = c
; c
->p
= this; }
26 sum
= ch
[0]->sum
+ ch
[1]->sum
+ val
;
33 std::swap(ch
[0],ch
[1]);
34 ch
[0]->rev
^= 1, ch
[1]->rev
^= 1;
39 inline void rotate(int d
)
41 node
*x
= p
, *y
= x
->p
;
42 if (~x
->sgn()) y
->sc(x
->sgn(), this); else p
= y
;
43 x
->sc(d
, ch
[d
^1]), sc(d
^1,x
);
47 inline void rotate() { rotate(sgn()); }
48 inline void zig() { rotate(0); }
49 inline void zag() { rotate(1); }
60 while (~sgn()) rotate();
67 node
*x
= this, *y
= nil
;
71 x
->ch
[1] = y
, x
->update();
80 for (x
= expose(); x
->relax(), x
->ch
[0] != nil
; x
= x
->ch
[0]);
91 inline node(int v
= 0): p(nil
), val(v
)
99 if (root() == v
->root()) return false; else
107 void modify(int x
) { splay(), val
= x
; }
111 if (root() != x
->root()) return -1; else
119 if (x
->p
== nil
) return x
->ch
[1]->sum
+ x
->val
+ y
->sum
;
120 x
->ch
[1] = y
, x
->update();
125 } *v
[maxn
], *nil
= new node();
132 std::scanf("%d",&tmp
), v
[i
] = new node(tmp
);
138 std::scanf(" %s %d %d",opt
,&x
,&y
);
143 v
[x
]->link(v
[y
]) ? std::puts("yes") : std::puts("no");
149 ans
= v
[x
]->query(v
[y
]);
150 ans
== -1 ? std::puts("impossible") : std::printf("%d\n",ans
);