using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4");
WillReturn.Add("7");
WillReturn.Add("0 1 2 3");
WillReturn.Add("1 1 2 1");
WillReturn.Add("1 3 4 5");
WillReturn.Add("0 3 4 6");
WillReturn.Add("1 3 4 5");
WillReturn.Add("0 2 3 6");
WillReturn.Add("1 3 1 5");
//2
//Ambiguous
//1
//2
}
else if (InputPattern == "Input2") {
WillReturn.Add("15");
WillReturn.Add("25");
WillReturn.Add("0 11 12 41");
WillReturn.Add("0 1 2 159");
WillReturn.Add("0 14 15 121");
WillReturn.Add("0 4 5 245");
WillReturn.Add("0 12 13 157");
WillReturn.Add("0 9 10 176");
WillReturn.Add("0 6 7 170");
WillReturn.Add("0 2 3 123");
WillReturn.Add("0 7 8 167");
WillReturn.Add("0 3 4 159");
WillReturn.Add("1 12 11 33");
WillReturn.Add("0 10 11 116");
WillReturn.Add("0 8 9 161");
WillReturn.Add("1 9 12 68");
WillReturn.Add("1 12 12 33");
WillReturn.Add("1 7 12 74");
WillReturn.Add("0 5 6 290");
WillReturn.Add("1 8 9 93");
WillReturn.Add("0 13 14 127");
WillReturn.Add("1 10 12 108");
WillReturn.Add("1 14 1 3");
WillReturn.Add("1 13 8 124");
WillReturn.Add("1 12 11 33");
WillReturn.Add("1 12 10 33");
WillReturn.Add("1 5 15 194");
//8
//33
//33
//33
//68
//33
//144
//93
//8
//108
//118
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long mN;
struct QueryInfoDef
{
internal long T;
internal long X;
internal long Y;
internal long V;
}
static List<QueryInfoDef> mQueryInfoList = new List<QueryInfoDef>();
static void Main()
{
List<string> InputList = GetInputList();
mN = long.Parse(InputList[0]);
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
foreach (string EachStr in InputList.Skip(2)) {
SplitAct(EachStr);
QueryInfoDef WillAdd;
WillAdd.T = wkArr[0];
WillAdd.X = wkArr[1];
WillAdd.Y = wkArr[2];
WillAdd.V = wkArr[3];
mQueryInfoList.Add(WillAdd);
}
var InsUnionFind = new UnionFind();
for (long I = 1; I <= mN; I++) {
InsUnionFind.MakeSet(I);
}
var Ins_Fenwick_Tree_Even = new Fenwick_Tree(mN + 1);
var Ins_Fenwick_Tree_Odd = new Fenwick_Tree(mN + 1);
var sb = new System.Text.StringBuilder();
foreach (QueryInfoDef EachQueryInfo in mQueryInfoList) {
if (EachQueryInfo.T == 0) {
long Root1 = InsUnionFind.FindSet(EachQueryInfo.X);
long Root2 = InsUnionFind.FindSet(EachQueryInfo.Y);
if (Root1 != Root2) {
InsUnionFind.Unite(EachQueryInfo.X, EachQueryInfo.Y);
if (EachQueryInfo.X % 2 == 0) {
Ins_Fenwick_Tree_Even.Add(EachQueryInfo.X, EachQueryInfo.V, true);
}
else {
Ins_Fenwick_Tree_Odd.Add(EachQueryInfo.X, EachQueryInfo.V, true);
}
}
}
if (EachQueryInfo.T == 1) {
long Root1 = InsUnionFind.FindSet(EachQueryInfo.X);
long Root2 = InsUnionFind.FindSet(EachQueryInfo.Y);
if (Root1 != Root2) {
sb.AppendLine("Ambiguous");
continue;
}
// 場合01
if (EachQueryInfo.X == EachQueryInfo.Y) {
sb.Append(EachQueryInfo.V);
sb.AppendLine();
continue;
}
long RangeSta, RangeEnd;
bool OddPlus;
long Result;
// 場合02
if (EachQueryInfo.X < EachQueryInfo.Y) {
bool FirstOdd = (EachQueryInfo.X % 2) == 1;
long RangeLen = EachQueryInfo.Y - EachQueryInfo.X;
bool RangeOdd = (RangeLen % 2 == 1);
OddPlus = false;
if (FirstOdd == false && RangeOdd == false) {
OddPlus = true;
}
if (FirstOdd && RangeOdd) {
OddPlus = true;
}
Result = (RangeOdd ? -EachQueryInfo.V : EachQueryInfo.V);
RangeSta = EachQueryInfo.X;
RangeEnd = EachQueryInfo.Y - 1;
}
// 場合03
else /*if (EachQueryInfo.X > EachQueryInfo.Y)*/ {
bool FirstOdd = (EachQueryInfo.X % 2 == 1);
long RangeLen = EachQueryInfo.X - EachQueryInfo.Y;
bool RangeOdd = (RangeLen % 2 == 1);
OddPlus = false;
if (FirstOdd == false && RangeOdd) {
OddPlus = true;
}
if (FirstOdd && RangeOdd == false) {
OddPlus = true;
}
Result = (RangeOdd ? -EachQueryInfo.V : EachQueryInfo.V);
RangeSta = EachQueryInfo.Y;
RangeEnd = EachQueryInfo.X - 1;
}
long RangeSumOdd = Ins_Fenwick_Tree_Odd.GetSum(RangeSta, RangeEnd, true);
long RangeSumEven = Ins_Fenwick_Tree_Even.GetSum(RangeSta, RangeEnd, true);
if (OddPlus) {
Result += RangeSumOdd - RangeSumEven;
}
else {
Result += -RangeSumOdd + RangeSumEven;
}
sb.Append(Result);
sb.AppendLine();
}
}
Console.Write(sb.ToString());
}
}
#region UnionFind
// UnionFindクラス
internal class UnionFind
{
private class NodeInfoDef
{
internal long ParentNode;
internal long Rank;
}
private Dictionary<long, NodeInfoDef> mNodeInfoDict =
new Dictionary<long, NodeInfoDef>();
// 要素が1つである木を森に追加
internal void MakeSet(long pNode)
{
NodeInfoDef WillAdd = new NodeInfoDef();
WillAdd.ParentNode = pNode;
WillAdd.Rank = 0;
mNodeInfoDict[pNode] = WillAdd;
}
// 合併処理
internal void Unite(long pX, long pY)
{
long XNode = FindSet(pX);
long YNode = FindSet(pY);
long XRank = mNodeInfoDict[XNode].Rank;
long YRank = mNodeInfoDict[YNode].Rank;
if (XRank > YRank) {
mNodeInfoDict[YNode].ParentNode = XNode;
}
else {
mNodeInfoDict[XNode].ParentNode = YNode;
if (XRank == YRank) {
mNodeInfoDict[YNode].Rank++;
}
}
}
// ノードを引数として、木の根を取得
internal long FindSet(long pTargetNode)
{
// 根までの経路上のノードのList
var PathNodeList = new List<long>();
long CurrNode = pTargetNode;
while (CurrNode != mNodeInfoDict[CurrNode].ParentNode) {
PathNodeList.Add(CurrNode);
CurrNode = mNodeInfoDict[CurrNode].ParentNode;
}
// 経路圧縮 (親ポインタの付け替え)
foreach (long EachPathNode in PathNodeList) {
mNodeInfoDict[EachPathNode].ParentNode = CurrNode;
}
return CurrNode;
}
internal void DebugPrint()
{
foreach (var EachPair in mNodeInfoDict.OrderBy(pX => pX.Key)) {
Console.WriteLine("mNodeInfoDict[{0}].ParentNode={1}",
EachPair.Key, EachPair.Value.ParentNode);
}
}
}
#endregion
#region Fenwick_Tree
// フェニック木
internal class Fenwick_Tree
{
private long[] mBitArr;
private long mN;
// コンストラクタ
internal Fenwick_Tree(long pItemCnt)
{
mN = pItemCnt;
mBitArr = new long[pItemCnt + 1];
}
// [pSta,pEnd] のSumを返す
internal long GetSum(long pSta, long pEnd, bool pIsZeroOrigin)
{
return GetSum(pEnd, pIsZeroOrigin) - GetSum(pSta - 1, pIsZeroOrigin);
}
// [0,pEnd] のSumを返す
internal long GetSum(long pEnd, bool pIsZeroOrigin)
{
if (pIsZeroOrigin) {
pEnd++; // 1オリジンに変更
}
long Sum = 0;
while (pEnd >= 1) {
Sum += mBitArr[pEnd];
pEnd -= pEnd & -pEnd;
}
return Sum;
}
// [I] に Xを加算
internal void Add(long pI, long pX, bool pIsZeroOrigin)
{
if (pIsZeroOrigin) {
pI++; // 1オリジンに変更
}
while (pI <= mN) {
mBitArr[pI] += pX;
pI += pI & -pI;
}
}
}
#endregion