結果

問題 No.404 部分門松列
ユーザー りあんりあん
提出日時 2016-07-21 12:03:36
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,736 ms / 2,000 ms
コード長 5,704 bytes
コンパイル時間 2,922 ms
コンパイル使用メモリ 111,548 KB
実行使用メモリ 54,920 KB
最終ジャッジ日時 2023-09-11 02:18:07
合計ジャッジ時間 29,129 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
23,092 KB
testcase_01 AC 71 ms
21,140 KB
testcase_02 AC 71 ms
23,128 KB
testcase_03 AC 71 ms
23,132 KB
testcase_04 AC 79 ms
21,364 KB
testcase_05 AC 79 ms
23,320 KB
testcase_06 AC 80 ms
25,328 KB
testcase_07 AC 81 ms
23,392 KB
testcase_08 AC 81 ms
23,188 KB
testcase_09 AC 80 ms
23,344 KB
testcase_10 AC 80 ms
23,332 KB
testcase_11 AC 80 ms
23,324 KB
testcase_12 AC 80 ms
23,240 KB
testcase_13 AC 772 ms
46,364 KB
testcase_14 AC 403 ms
38,588 KB
testcase_15 AC 550 ms
36,128 KB
testcase_16 AC 982 ms
44,528 KB
testcase_17 AC 1,173 ms
48,796 KB
testcase_18 AC 498 ms
32,820 KB
testcase_19 AC 473 ms
43,032 KB
testcase_20 AC 459 ms
47,172 KB
testcase_21 AC 1,107 ms
46,952 KB
testcase_22 AC 580 ms
32,908 KB
testcase_23 AC 541 ms
47,528 KB
testcase_24 AC 956 ms
47,476 KB
testcase_25 AC 1,736 ms
54,920 KB
testcase_26 AC 1,565 ms
49,260 KB
testcase_27 AC 272 ms
29,052 KB
testcase_28 AC 858 ms
43,724 KB
testcase_29 AC 1,294 ms
47,460 KB
testcase_30 AC 664 ms
38,864 KB
testcase_31 AC 190 ms
29,596 KB
testcase_32 AC 1,126 ms
46,136 KB
testcase_33 AC 1,097 ms
48,660 KB
testcase_34 AC 772 ms
41,856 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 System.Linq;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        var sc = new Scan();
        var se = new Search();
        int n = sc.Int;
        var a = sc.IntArr;
        var b = new int[n];
        var ar = a.ToList().Distinct().ToArray();
        Array.Sort(ar);
        int lim = ar.Length;
        var zip = new SortedDictionary<int, int>();
        for (int i = 0; i < lim; i++)
            zip.Add(ar[i], i);

        var sgbef = new Segtree<long>(lim, (i, j) => i + j, 0);
        var sgaft = new Segtree<long>(lim, (i, j) => i + j, 0);
        var sgmul = new Segtree<long>(lim, (i, j) => i + j, 0);
        for (int i = 0; i < n; i++)
        {
            b[i] = zip[a[i]];
            sgaft.update(b[i], sgaft.look(b[i]) + 1);
        }
        long ans = 0;
        var anss = new long[lim];
        for (int i = 0; i < n; i++)
        {
            sgaft.update(b[i], sgaft.look(b[i]) - 1);
            sgmul.update(b[i], sgbef.look(b[i]) * sgaft.look(b[i]));
            anss[b[i]] += sgbef.run(0, b[i]) * sgaft.run(0, b[i]) - sgmul.run(0, b[i]) + sgbef.run(b[i] + 1, lim) * sgaft.run(b[i] + 1, lim) - sgmul.run(b[i] + 1, lim);
            sgbef.update(b[i], sgbef.look(b[i]) + 1);
            sgmul.update(b[i], sgbef.look(b[i]) * sgaft.look(b[i]));
        }
        var imos = new long[lim + 1];
        for (int i = 0; i < lim; i++)
        {
            imos[i + 1] = imos[i] + anss[i];
        }
        int q = sc.Int;
        for (int i = 0; i < q; i++)
        {
            var c = sc.IntArr;
            int l = se.LowerBound(ar, c[0]);
            int r = se.UpperBound(ar, c[1]);
            sw.WriteLine(imos[r] - imos[l]);
        }
        sw.Flush();
    }
}
class Scan
{
    public int Int { get { return int.Parse(Str); } }
    public long Long { get { return long.Parse(Str); } }
    public string Str { get { return Console.ReadLine().Trim(); } }
    public int[] IntArr { get { return Str.Split().Select(int.Parse).ToArray(); } }
}
class Segtree<T>
{
    int n;
    T[] tree;
    Func<T, T, T> f;
    T exnum;
    public Segtree(int m, Func<T, T, T> f, T ex)
    {
        this.f = f;
        this.exnum = ex;
        n = 1;
        while (n < m) n <<= 1;

        tree = new T[(n << 1) - 1];
        for (int i = 0; i < tree.Length; i++) tree[i] = ex;
    }
    public Segtree(int m, T ini, Func<T, T, T> f, T ex)
    {
        this.f = f;
        this.exnum = ex;
        n = 1;
        while (n < m) n <<= 1;

        tree = new T[(n << 1) - 1];
        for (int i = 0; i < tree.Length; i++) tree[i] = ini;
        for (int i = 0; i < m; ++i) update(i, ini);
    }
    public void update(int j, T x)
    {
        int i = j + n - 1;
        tree[i] = x;
        while (i > 0)
        {
            i = (i - 1) >> 1;
            tree[i] = f(tree[(i << 1) + 1], tree[(i << 1) + 2]);
        }
    }
    public T look(int i) { return tree[i + n - 1]; }

    // [s, t)
    public T run(int s, int t) { return query(s, t, 0, 0, n); }
    T query(int s, int t, int k, int l, int r)
    {
        if (r <= s || t <= l) return exnum;
        if (s <= l && r <= t) return tree[k];

        return f(query(s, t, (k << 1) + 1, l, (l + r) >> 1), query(s, t, (k + 1) << 1, (l + r) >> 1, r));
    }
}
class Search
{
    // nより大きいの項のindexのうち最小のものを返す
    // n以下の項のindexのうち最大のもの+1を返す
    // そこに挿入
    public int UpperBound<T>(List<T> a, T n) where T : IComparable
    {
        if (a.Count == 0) return 0;

        int l = 0, r = a.Count - 1;
        while (l < r)
        {
            int m = (l + r) / 2;
            if (a[m].CompareTo(n) <= 0)
            {
                if (a[m + 1].CompareTo(n) > 0) return m + 1;
                
                l = m + 1;
            }
            else r = m - 1;
        }
        if (a[l].CompareTo(n) <= 0) return l + 1;
        
        return l;
    }

    // n未満の項のindexのうち最大のもの+1を返す
    // n以上の項のindexのうち最小のものを返す
    // そこに挿入
    public int LowerBound<T>(List<T> a, T n) where T : IComparable
    {
        if (a.Count == 0) return 0;

        int l = 0, r = a.Count - 1;
        while (l < r)
        {
            int m = (l + r) / 2;
            if (a[m].CompareTo(n) < 0)
            {
                if (a[m + 1].CompareTo(n) >= 0) return m + 1;

                l = m + 1;
            }
            else r = m - 1;
        }

        if (a[l].CompareTo(n) < 0) return l + 1;

        return l;
    }
    public int LowerBound<T>(T[] a, T n) where T : IComparable
    {
        if (a.Length == 0) return 0;

        int l = 0, r = a.Length - 1;
        while (l < r)
        {
            int m = (l + r) / 2;
            if (a[m].CompareTo(n) < 0)
            {
                if (a[m + 1].CompareTo(n) >= 0) return m + 1;

                l = m + 1;
            }
            else r = m - 1;
        }

        if (a[l].CompareTo(n) < 0) return l + 1;

        return l;
    }
    public int UpperBound<T>(T[] a, T n) where T : IComparable
    {
        if (a.Length == 0) return 0;

        int l = 0, r = a.Length - 1;
        while (l < r)
        {
            int m = (l + r) / 2;
            if (a[m].CompareTo(n) <= 0)
            {
                if (a[m + 1].CompareTo(n) > 0) return m + 1;
                
                l = m + 1;
            }
            else r = m - 1;
        }
        if (a[l].CompareTo(n) <= 0) return l + 1;
        
        return l;
    }

}
0