結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー Lily89164763Lily89164763
提出日時 2020-07-17 21:51:04
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 4,515 bytes
コンパイル時間 3,380 ms
コンパイル使用メモリ 105,776 KB
実行使用メモリ 41,488 KB
最終ジャッジ日時 2023-08-20 00:39:29
合計ジャッジ時間 9,713 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
22,624 KB
testcase_01 AC 61 ms
21,628 KB
testcase_02 AC 60 ms
21,596 KB
testcase_03 AC 170 ms
40,324 KB
testcase_04 AC 184 ms
39,384 KB
testcase_05 AC 191 ms
38,312 KB
testcase_06 AC 180 ms
39,792 KB
testcase_07 AC 190 ms
41,272 KB
testcase_08 AC 73 ms
24,352 KB
testcase_09 AC 128 ms
30,912 KB
testcase_10 AC 176 ms
41,380 KB
testcase_11 AC 60 ms
23,604 KB
testcase_12 AC 188 ms
39,356 KB
testcase_13 AC 188 ms
41,428 KB
testcase_14 AC 187 ms
41,488 KB
testcase_15 AC 64 ms
21,680 KB
testcase_16 AC 62 ms
21,756 KB
testcase_17 AC 62 ms
23,800 KB
testcase_18 AC 60 ms
21,632 KB
testcase_19 AC 60 ms
21,752 KB
testcase_20 AC 61 ms
23,700 KB
testcase_21 AC 60 ms
21,752 KB
testcase_22 AC 60 ms
23,668 KB
testcase_23 AC 76 ms
24,676 KB
testcase_24 AC 105 ms
26,964 KB
testcase_25 AC 159 ms
39,440 KB
testcase_26 AC 87 ms
23,104 KB
testcase_27 AC 116 ms
27,720 KB
testcase_28 AC 134 ms
26,968 KB
testcase_29 AC 162 ms
39,844 KB
testcase_30 AC 182 ms
41,088 KB
testcase_31 AC 93 ms
23,652 KB
testcase_32 AC 84 ms
24,896 KB
testcase_33 AC 165 ms
38,476 KB
testcase_34 AC 60 ms
21,660 KB
testcase_35 AC 60 ms
21,732 KB
testcase_36 AC 60 ms
23,648 KB
testcase_37 AC 62 ms
21,656 KB
testcase_38 AC 60 ms
23,648 KB
testcase_39 AC 60 ms
21,860 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using CompLib.Collections;
using CompLib.Collections.Generic;
using CompLib.Util;

public class Program
{
    // private int N;

    public void Solve()
    {
        var sc = new Scanner();
        int n = sc.NextInt();
        int[] a = sc.IntArray();
        int[] b = sc.IntArray();

        int[] indexOf = new int[n + 1];
        for (int i = 0; i < n; i++)
        {
            indexOf[b[i]] = i;
        }

        int[] tmp = new int[n];
        for (int i = 0; i < n; i++)
        {
            tmp[i] = indexOf[a[i]];
        }

        long ans = 0;
        var st = new SegmentTree<int>(n, (l, r) => l + r, 0);
        for (int i = 0; i < n; i++)
        {
            ans += st.Query(tmp[i], n);
            st[tmp[i]]++;
        }

        Console.WriteLine(ans);
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Collections.Generic
{
    using System;

    public class SegmentTree<T>
    {
        private readonly int N;
        private T[] _array;

        private T _identity;
        private Func<T, T, T> _operation;

        public SegmentTree(int size, Func<T, T, T> operation, T identity)
        {
            N = 1;
            while (N < size) N *= 2;
            _identity = identity;
            _operation = operation;
            _array = new T[N * 2];
            for (int i = 1; i < N * 2; i++)
            {
                _array[i] = _identity;
            }
        }

        /// <summary>
        /// A[i]をnに更新 O(log N)
        /// </summary>
        /// <param name="i"></param>
        /// <param name="n"></param>
        public void Update(int i, T n)
        {
            i += N;
            _array[i] = n;
            while (i > 1)
            {
                i /= 2;
                _array[i] = _operation(_array[i * 2], _array[i * 2 + 1]);
            }
        }

        private T Query(int left, int right, int k, int l, int r)
        {
            if (r <= left || right <= l)
            {
                return _identity;
            }

            if (left <= l && r <= right)
            {
                return _array[k];
            }

            return _operation(Query(left, right, k * 2, l, (l + r) / 2),
                Query(left, right, k * 2 + 1, (l + r) / 2, r));
        }

        /// <summary>
        /// A[left] op A[left+1] ... op A[right-1]を求める
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public T Query(int left, int right)
        {
            return Query(left, right, 1, 0, N);
        }

        public T this[int i]
        {
            set { Update(i, value); }
            get { return _array[i + N]; }
        }
    }
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}
0