AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC098-D Xor Sum 2


問題へのリンク


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("2 5 4 6");
            //5
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("9");
            WillReturn.Add("0 0 0 0 0 0 0 0 0");
            //45
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("19");
            WillReturn.Add("885 8 1 128 83 32 256 206 639 16 4 128 689 32 8 64 885 969 1");
            //37
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long UB = AArr.GetUpperBound(0);

        // 尺取法で解く
        long R = 0;
        long CurrSum = AArr[0];
        long Answer = 0;

        for (long L = 0; L <= UB; L++) {
            while (R + 1 <= UB) {
                if (CurrSum + AArr[R + 1] == (CurrSum ^ AArr[R + 1])) {
                    CurrSum += AArr[R + 1];
                    R++;
                }
                else {
                    break;
                }
            }
            Answer += R - L + 1;
            CurrSum ^= AArr[L];
        }
        Console.WriteLine(Answer);
    }
}


解説

尺取法で解いてます。