Problem:
Weighted Union Find Trees

There is a sequence $A = a_0, a_1, ..., a_{n-1}$. You are given the following information and questions.

relate$(x, y, z)$: $a_y$ is greater than $a_x$ by $z$

diff$(x, y)$: report the difference between $a_x$ and $a_y$ $(a_y - a_x)$

Input

$n \; q$
$query_1$
$query_2$
:
$query_q$

In the first line, $n$ and $q$ are given. Then, $q$ information/questions are given in the following format.

0 $x \; y\; z$

or

1 $x \; y$

where '0' of the first digit denotes the relate information and '1' denotes the diff question.

Output

For each diff question, print the difference between $a_x$ and $a_y$ $(a_y - a_x)$.

Constraints

$2 \leq n \leq 100,000$

$1 \leq q \leq 200,000$

$0 \leq x, y < n$

$x \ne y$

$0 \leq z \leq 10000$

There are no inconsistency in the given information

Sample Input

5 6
0 0 2 5
0 1 2 3
1 0 1
1 1 3
0 1 4 8
1 0 4

Sample Output

2
?
10

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Math;

namespace CSharpSpiral {
	class Program {
		static void Main(string[] args) {
			var sw = new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush=
#if DEBUG
			true
#else
			false
#endif
			};
			Console.SetOut(sw);
			new Program().Solve(new ConsoleInput(
#if DEBUG
			new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
			, ' '));
			Console.Out.Flush();
		}
		public void Solve(ConsoleInput cin) {
			var N = cin.ReadInt;
			var Q = cin.ReadInt;
			var W = new WeightedUFF(N);
			for(int j = 0; j<Q; j++) {
				var query = cin.ReadInt;
				int[] datum;
				switch(query) {
					case 0:
						datum = cin.ReadIntArray(3);
						W.Union(datum[0], datum[1], datum[2]);
						break;
					default:
						datum=cin.ReadIntArray(2);
						Console.WriteLine(W.Weight(datum[0], datum[1])?.ToString()??"?");
						break;
				}
			}
		}
	}
	static class ExtendClass {
		public static void Swap<T>(this T[] A, int x, int y) {
			var tmp = A[x];
			A[x]=A[y];
			A[y]=tmp;
		}
		public static string Join<T>(this IEnumerable<T> X, string separator = " ") => string.Join(separator, X);
		public static IEnumerable<int> Indices<T>(this T[] X) => Enumerable.Range(0, X.Length);
		public static IEnumerable<int> Indices<T>(this T[,] X, int dim)
			=> Enumerable.Range(0, X.GetLength(dim));
		public static string Join<T>(this T[,] X, string separatorD2 = ",", string separatorD1 = "\n")
			=> X.Indices(0).Select(
				i => X.Indices(1).Select(j => X[i, j]).Join(separatorD2)
			).Join(separatorD1);
	}
	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator=separator;
			this._stream=stream;
			inputStream=new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count!=0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i<tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i<N; ++i)
				ret[i]=Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i<N; ++i) ret[i]=ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i<N; ++i) ret[i]=ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i<N; ++i) ret[i]=ReadDouble; return ret; }
		public bool isEOF {
			get { return _stream.Peek()==-1; }
		}
	}
	class WeightedUFF {
		public class WNode {
			public int value { get; set; }
			public long weight { get; set; } = 0;
			public WNode parent { get; set; }
			public int rank { get; private set; }    //meanful iff root
			public void rankIncrement() => rank++;
			public WNode root {
				get {
					var path = new Queue<WNode>();
					var res = this;
					var dist = 0L;
					while(res.parent!=res) {
						path.Enqueue(res);
						dist+=res.weight;
						res=res.parent;
					}
					while(path.Any()) {
						var d = path.Dequeue();
						d.parent=res;
						dist-=d.weight;
						d.weight+=dist;
					}
					return res;
				}
			}
			public WNode(int _value) {
				value=_value;
				parent=this;
				rank=1;
			}
		}
		public WeightedUFF(int N) {
			for(int i = 0; i<N; i++) Register(i);
		}
		public readonly Dictionary<int, WNode> Registry = new Dictionary<int, WNode>();
		public void Union(int x, int y, long weight) => Union(Registry[x], Registry[y], weight);
		public void Union(WNode X, WNode Y, long weight) {
			var RX = X.root;
			var RY = Y.root;
			switch(RX.rank.CompareTo(RY.rank)) {
				case 1:
					RY.parent=RX;
					RY.weight=-(weight+Y.weight-X.weight);
					break;
				case -1:
					RX.parent=RY;
					RX.weight=weight+Y.weight-X.weight;
					break;
				default:
					RX.parent=RY;
					RX.weight=weight+Y.weight-X.weight;
					RY.rankIncrement();
					break;
			}
		}
		public void Register(int id) {
			var N = new WNode(id);
			Registry.Add(id, N);
		}
		public bool Find(int x, int y) => Find(Registry[x], Registry[y]);
		public bool Find(WNode X, WNode Y) => X.root==Y.root;
		public long? Weight(WNode From, WNode To) {
			if(!Find(From, To)) return null;
			return From.weight-To.weight;
		}
		public long? Weight(int from, int to) => Weight(Registry[from], Registry[to]);

	}
}