Nice and clear explanation. I had done same.
In case some one is not able to understand something do look at this:
For the query of type 3, speed depends only on u and v.
And the sign of answer depends on whether it is connected to other vertex by odd length or even length path. Because in the connected gear system there will be alternate +ve and -ve signs of answer from given vertex.
In short you have to color the graph in two colors.
I had maintained graph and used disjoint set data structure and array for gear teeth and when you have to change the teeth value you can just change value of array.
Maintain different color for each gear and when you connect two gears just change the color of other gear.
Note: Gears will be blocked when there is odd length cycle.
for query of type 3,
Suppose you have two components in which each components may contain several connected gears.
When query of type 2 occurs and you have to connect these two components then use disjoint set data structure and change the color of smaller component according to bigger components For e.g you had used colors 2,3 for 1st component and 4,5 for other component then change color of second component (if 2nd is smaller component).
while connecting two vertices from same component just check if one of them is blocked or by connecting these vertices will there be any odd length cycle ? i.e do they have same color ? because now you cant connect them if they have same color and after connecting they must have different colors hence all the vertices from component will be blocked.
Last while answering type 3 you can check color of both vertices if they are same color : use + sign answer else negative answer.
My solution https://www.codechef.com/viewsolution/19233845