典型90問    次の典型90問へ    前の典型90問へ

典型90問 068 Paired Information(★5)


問題へのリンク


C#のソース

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


解説

場合01
A2 + A3 = S2
A3 + A4 = S3
A4 + A5 = S4
A5 + A6 = S5
として、A2からA6を求める場合
A3 = S2-A2
A4 = S3-S2+A2
A5 = S4-S3+S2-A2
A6 = S5-S4+S3-S2+A2
となって、正負が交互に入れ替わると分かります。

場合02
A2 + A3 = S2
A3 + A4 = S3
A4 + A5 = S4
A5 + A6 = S5
として、A6からA2を求める場合
A5 = S5-A6
A4 = S4-S5+A6
A3 = S3-S4+S5-A6
A2 = S2-S3+S4-S5+A6
となって、正負が交互に入れ替わると分かります。

なので
UnionFindで連結判定を行いつつ、
偶数用のフェニック木と
奇数用のフェニック木を使い
区間和を高速に求めるようにしてます。