AtCoder problem 068

C++ solution:
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll=long long;
#define MOD 998244353
using Graph=vector<vector<int>>;
#define INF 1000000000
#define MAX 300000

vector<int> parent;
vector<int> sign;
vector<ll> V;
int find(int x){
  int y=parent[x];
  if(y!=parent[y]){
    int pre_y=y;
    y=find(y);
    sign[x]=sign[pre_y]*sign[x]*(-1);
    V[x]=V[pre_y]+V[x]*(ll)sign[pre_y]*(-1);
  }
  return parent[x]=y;
}
void unite(int a,int b,ll v){
  int x=find(a);
  int y=find(b);
  if(x!=y){
    if(x==a&&b==y){
      sign[y]=1;
      V[y]=v;
    }else if(b==y){
      if(sign[a]==1){
        sign[b]=-1;
        V[b]=V[a]-v;
      }else{
        sign[b]=1;
        V[b]=V[a]+v;
      }
    }else if(x==a){
      if(sign[b]==1){
        sign[y]=-1;
        V[y]=v-V[b];
      }else{
        sign[y]=1;
        V[y]=v+V[b];
      }
    }else{
      if(sign[a]==1&&sign[b]==1){
        sign[y]=1;
        V[y]=V[a]-v+V[b];
      }else if(sign[a]==1&&sign[b]==-1){
        sign[y]=-1;
        V[y]=V[a]-v-V[b];
      }else if(sign[a]==-1&&sign[b]==1){
        sign[y]=-1;
        V[y]=V[a]+v-V[b];
      }else{
        sign[y]=1;
        V[y]=V[a]+v+V[b];
      }
    }
    parent[y]=x;
  }
}

int main(){
  int N,Q;
  cin>>N>>Q;
  parent.resize(N);
  sign.resize(N,0);
  V.resize(N);
  for(int i=0;i<N;i++){
    parent[i]=i;
  }
  for(int i=0;i<Q;i++){
    int t,x,y,v;
    cin>>t>>x>>y>>v;
    x--;y--;
    if(t==0){
      if(find(x)!=find(y)){
        if(x>y){
          swap(x,y);
        }
        unite(x,y,v);
      }
    }else{
      if(find(x)==find(y)){
        if(x==y){
          cout<<v<<endl;
        }else if(x==find(x)){
          if(sign[y]==1){
            cout<<V[y]-v<<endl;
          }else{
            cout<<-V[y]+v<<endl;
          }
        }else if(y==find(y)){
          if(sign[x]==1){
            cout<<V[x]-v<<endl;
          }else{
            cout<<V[x]+v<<endl;
          }
        }else{
          if(sign[x]==1&&sign[y]==1){
            cout<<V[y]-V[x]+v<<endl;
          }else if(sign[x]==-1&&sign[y]==-1){
            cout<<V[x]-V[y]+v<<endl;
          }else if(sign[x]==1&&sign[y]==-1){
            cout<<V[x]-V[y]-v<<endl;
          }else if(sign[x]==-1&&sign[y]==1){
            cout<<V[y]-V[x]-v<<endl;
          }
        }
      }else{
        cout<<"Ambiguous"<<endl;
      }
    }
  }
}






Kuin solution: func main()
	var n: int :: cui@inputInt()
	var q: int :: cui@inputInt()
	var f: []bool :: #[n]bool
	var bitFlag: @Bit :: (#@Bit).init(n)
	var bitEven: @Bit :: (#@Bit).init(n)
	var bitOdd: @Bit :: (#@Bit).init(n)
	for(1, q)
		var t: int :: cui@inputInt()
		var x: int :: cui@inputInt() - 1
		var y: int :: cui@inputInt() - 1
		var v: int :: cui@inputInt()
		if(t = 0)
			if(!f[x])
				do f[x] :: true
				do bitFlag.add(x, 1)
				if(x % 2 = 0)
					do bitEven.add(x, v)
				else
					do bitOdd.add(x, v)
				end if
			end if
		else
			if(x = y)
				do cui@print("\{v}\n")
			elif(x < y)
				if(bitFlag.query(x, y) = y - x)
					var ans: int :: 0
					if(y % 2 = 1)
						do ans :+ bitEven.query(x, y) - bitOdd.query(x, y)
					else
						do ans :- bitEven.query(x, y) - bitOdd.query(x, y)
					end if
					if((y - x) % 2 = 0)
						do ans :+ v
					else
						do ans :- v
					end if
					do cui@print("\{ans}\n")
				else
					do cui@print("Ambiguous\n")
				end if
			else
				if(bitFlag.query(y, x) = x - y)
					var ans: int :: 0
					if(y % 2 = 1)
						do ans :+ bitEven.query(x, y) - bitOdd.query(x, y)
					else
						do ans :- bitEven.query(x, y) - bitOdd.query(x, y)
					end if
					if((y - x) % 2 = 0)
						do ans :+ v
					else
						do ans :- v
					end if
					do cui@print("\{ans}\n")
				else
					do cui@print("Ambiguous\n")
				end if
			end if
		end if
	end for
end func

class Bit()
	var n: int
	var data: []int
	+func init(n: int): Bit
		do me.n :: n
		do me.data :: #[n + 1]int
		ret me
	end func
	+func add(i: int, v: int)
		var x: int :: i + 1
		while(x <= me.n)
			do me.data[x] :+ v
			do x :+ (x $ bit64).and(((-x) $ bit64)) $ int
		end while
	end func
	+func sum(i: int): int
		var x: int :: i + 1
		var res: int :: 0
		while(x > 0)
			do res :+ me.data[x]
			do x :- (x $ bit64).and(((-x) $ bit64)) $ int
		end while
		ret res
	end func
	
	; [l, r)
	+func query(l: int, r: int): int
		ret me.sum(r - 1) - me.sum(l - 1)
	end func
end class