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

ABC221-D Online games


問題へのリンク


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("3");
            WillReturn.Add("1 2");
            WillReturn.Add("2 3");
            WillReturn.Add("3 1");
            //2 2 0
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2");
            WillReturn.Add("1000000000 1000000000");
            WillReturn.Add("1000000000 1000000000");
            //0 1000000000
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct ABInfoDef
    {
        internal long A;
        internal long B;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        long[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();

        var ABInfoList = new List<ABInfoDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            ABInfoDef WillAdd;
            WillAdd.A = wkArr[0];
            WillAdd.B = wkArr[1];
            ABInfoList.Add(WillAdd);
        }

        var RunSumDict = new Dictionary<long, long>();

        Action<long, long> AddAct = (pKey, pVal) =>
        {
            if (RunSumDict.ContainsKey(pKey) == false) {
                RunSumDict[pKey] = 0;
            }
            RunSumDict[pKey] += pVal;
        };
        foreach (ABInfoDef EachABCInfo in ABInfoList) {
            AddAct(EachABCInfo.A, 1);
            AddAct(EachABCInfo.A + EachABCInfo.B, -1);
        }

        // 座標圧縮する
        long[] ZaatuArr = RunSumDict.Keys.ToArray();
        Array.Sort(ZaatuArr);
        long UB = ZaatuArr.GetUpperBound(0);

        // 累積和を求める
        for (int I = 1; I <= UB; I++) {
            long PrevKey = ZaatuArr[I - 1];
            long CurrKey = ZaatuArr[I];

            RunSumDict[CurrKey] += RunSumDict[PrevKey];
        }

        var CntDict = new Dictionary<long, long>();

        for (int I = 0; I <= UB - 1; I++) {
            long CurrKey = ZaatuArr[I];
            long NextKey = ZaatuArr[I + 1];

            long Curr_Cnt = RunSumDict[CurrKey];
            if (Curr_Cnt == 0) continue;

            if (CntDict.ContainsKey(Curr_Cnt) == false) {
                CntDict[Curr_Cnt] = 0;
            }
            CntDict[Curr_Cnt] += (NextKey - CurrKey);
        }

        var AnswerList = new List<long>();
        for (long I = 1; I <= ABInfoList.Count; I++) {
            if (CntDict.ContainsKey(I)) {
                AnswerList.Add(CntDict[I]);
            }
            else {
                AnswerList.Add(0);
            }
        }
        string[] OutStr = Array.ConvertAll(AnswerList.ToArray(), pX => pX.ToString());
        Console.WriteLine(string.Join(" ", OutStr));
    }
}


解説

いもす法と座標圧縮を組み合わせてます。


類題

ABC188-D Snuke Prime